|
| 1 | +# ADLS Gen2 Storage SDK for Go |
| 2 | + |
| 3 | +> Service Version: 2020-10-02 |
| 4 | +
|
| 5 | +Azure Data Lake Storage Gen2 (ADLS Gen2) is Microsoft's hierarchical object storage solution for the cloud with converged capabilities with Azure Blob Storage. |
| 6 | +For example, Data Lake Storage Gen2 provides file system semantics, file-level security, and scale. |
| 7 | +Because these capabilities are built on Blob storage, you also get low-cost, tiered storage, with high availability/disaster recovery capabilities. |
| 8 | +ADLS Gen2 makes Azure Storage the foundation for building enterprise data lakes on Azure. |
| 9 | +Designed from the start to service multiple petabytes of information while sustaining hundreds of gigabits of throughput, ADLS Gen2 allows you to easily manage massive amounts of data. |
| 10 | + |
| 11 | +[Source code][source] | [API reference documentation][docs] | [REST API documentation][rest_docs] |
| 12 | + |
| 13 | +## Getting started |
| 14 | + |
| 15 | +### Install the package |
| 16 | + |
| 17 | +Install the ADLS Gen2 Storage SDK for Go with [go get][goget]: |
| 18 | + |
| 19 | +```Powershell |
| 20 | +go get github.com/Azure/azure-sdk-for-go/sdk/storage/azdatalake |
| 21 | +``` |
| 22 | + |
| 23 | +If you're going to authenticate with Azure Active Directory (recommended), install the [azidentity][azidentity] module. |
| 24 | +```Powershell |
| 25 | +go get github.com/Azure/azure-sdk-for-go/sdk/azidentity |
| 26 | +``` |
| 27 | + |
| 28 | +### Prerequisites |
| 29 | + |
| 30 | +A supported [Go][godevdl] version (the Azure SDK supports the two most recent Go releases). |
| 31 | + |
| 32 | +You need an [Azure subscription][azure_sub] and a |
| 33 | +[Storage Account][storage_account_docs] to use this package. |
| 34 | + |
| 35 | +To create a new Storage Account, you can use the [Azure Portal][storage_account_create_portal], |
| 36 | +[Azure PowerShell][storage_account_create_ps], or the [Azure CLI][storage_account_create_cli]. |
| 37 | +Here's an example using the Azure CLI: |
| 38 | + |
| 39 | +```Powershell |
| 40 | +az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location westus --sku Standard_LRS |
| 41 | +``` |
| 42 | + |
| 43 | +### Authenticate the client |
| 44 | + |
| 45 | +In order to interact with the ADLS Gen2 Storage service, you'll need to create an instance of the `Client` type. The [azidentity][azidentity] module makes it easy to add Azure Active Directory support for authenticating Azure SDK clients with their corresponding Azure services. |
| 46 | + |
| 47 | +```go |
| 48 | +// create a credential for authenticating with Azure Active Directory |
| 49 | +cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 50 | +// TODO: handle err |
| 51 | + |
| 52 | +// create a service.Client for the specified storage account that uses the above credential |
| 53 | +client, err := service.NewClient("https://MYSTORAGEACCOUNT.dfs.core.windows.net/", cred, nil) |
| 54 | +// TODO: handle err |
| 55 | +// you can also create filesystem, file and directory clients |
| 56 | +``` |
| 57 | + |
| 58 | +Learn more about enabling Azure Active Directory for authentication with Azure Storage in [our documentation][storage_ad] and [our samples](#next-steps). |
| 59 | + |
| 60 | +## Key concepts |
| 61 | + |
| 62 | +ADLS Gen2 provides: |
| 63 | +- Hadoop-compatible access |
| 64 | +- Hierarchical directory structure |
| 65 | +- Optimized cost and performance |
| 66 | +- Finer grain security model |
| 67 | +- Massive scalability |
| 68 | + |
| 69 | +ADLS Gen2 storage is designed for: |
| 70 | + |
| 71 | +- Serving images or documents directly to a browser. |
| 72 | +- Storing files for distributed access. |
| 73 | +- Streaming video and audio. |
| 74 | +- Writing to log files. |
| 75 | +- Storing data for backup and restore, disaster recovery, and archiving. |
| 76 | +- Storing data for analysis by an on-premises or Azure-hosted service. |
| 77 | + |
| 78 | +ADLS Gen2 storage offers three types of resources: |
| 79 | + |
| 80 | +- The _storage account_ |
| 81 | +- One or more _filesystems_ in a storage account |
| 82 | +- One or more _files_ or _directories_ in a filesystem |
| 83 | + |
| 84 | +Instances of the `Client` type provide methods for manipulating filesystems and paths within a storage account. |
| 85 | +The storage account is specified when the `Client` is constructed. The clients available are referenced below. |
| 86 | +Use the appropriate client constructor function for the authentication mechanism you wish to use. |
| 87 | + |
| 88 | +### Goroutine safety |
| 89 | +We guarantee that all client instance methods are goroutine-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/golang_introduction.html#thread-safety)). This ensures that the recommendation of reusing client instances is always safe, even across goroutines. |
| 90 | + |
| 91 | +### About metadata |
| 92 | +ADLS Gen2 metadata name/value pairs are valid HTTP headers and should adhere to all restrictions governing HTTP headers. Metadata names must be valid HTTP header names, may contain only ASCII characters, and should be treated as case-insensitive. Base64-encode or URL-encode metadata values containing non-ASCII characters. |
| 93 | + |
| 94 | +### Additional concepts |
| 95 | +<!-- CLIENT COMMON BAR --> |
| 96 | +[Client options](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/policy#ClientOptions) | |
| 97 | +[Accessing the response](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime#WithCaptureResponse) | |
| 98 | +[Handling failures](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore#ResponseError) | |
| 99 | +[Logging](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/log) |
| 100 | +<!-- CLIENT COMMON BAR --> |
| 101 | + |
| 102 | +## Examples |
| 103 | + |
| 104 | +### Creating and uploading a file (assuming filesystem exists) |
| 105 | + |
| 106 | +```go |
| 107 | +const ( |
| 108 | + path = "https://MYSTORAGEACCOUNT.dfs.core.windows.net/sample-fs/sample-file" |
| 109 | +) |
| 110 | + |
| 111 | +// authenticate with Azure Active Directory |
| 112 | +cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 113 | +// TODO: handle error |
| 114 | + |
| 115 | +// create a client for the specified storage account |
| 116 | +client, err := file.NewClient(path, cred, nil) |
| 117 | +// TODO: handle error |
| 118 | + |
| 119 | +_, err = client.Create(context.TODO(), nil) |
| 120 | +// TODO: handle error |
| 121 | + |
| 122 | +// open the file for reading |
| 123 | +fh, err := os.OpenFile(sampleFile, os.O_RDONLY, 0) |
| 124 | +// TODO: handle error |
| 125 | +defer fh.Close() |
| 126 | + |
| 127 | +// upload the file to the specified filesystem with the specified file name |
| 128 | +_, err = client.UploadFile(context.TODO(), fh, nil) |
| 129 | +// TODO: handle error |
| 130 | +``` |
| 131 | + |
| 132 | +### Downloading a file |
| 133 | + |
| 134 | +```go |
| 135 | +const ( |
| 136 | + path = "https://MYSTORAGEACCOUNT.dfs.core.windows.net/sample-fs/cloud.jpg" |
| 137 | +) |
| 138 | + |
| 139 | +// authenticate with Azure Active Directory |
| 140 | +cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 141 | +// TODO: handle error |
| 142 | + |
| 143 | +// create a client for the specified storage account |
| 144 | +client, err := file.NewClient(path, cred, nil) |
| 145 | +// TODO: handle error |
| 146 | + |
| 147 | +// create or open a local file where we can download the file |
| 148 | +file, err := os.Create("cloud.jpg") |
| 149 | +// TODO: handle error |
| 150 | +defer file.Close() |
| 151 | + |
| 152 | +// download the file |
| 153 | +_, err = client.DownloadFile(context.TODO(), file, nil) |
| 154 | +// TODO: handle error |
| 155 | +``` |
| 156 | + |
| 157 | +### Creating and deleting a filesystem |
| 158 | + |
| 159 | +```go |
| 160 | +const ( |
| 161 | + fs = "https://MYSTORAGEACCOUNT.dfs.core.windows.net/sample-fs" |
| 162 | +) |
| 163 | + |
| 164 | +// authenticate with Azure Active Directory |
| 165 | +cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 166 | +// TODO: handle error |
| 167 | + |
| 168 | +// create a client for the specified storage account |
| 169 | +client, err := filesystem.NewClient(fs, cred, nil) |
| 170 | +// TODO: handle error |
| 171 | + |
| 172 | +_, err = client.Create(context.TODO(), nil) |
| 173 | +// TODO: handle error |
| 174 | + |
| 175 | +_, err = client.Delete(context.TODO(), nil) |
| 176 | +// TODO: handle error |
| 177 | +``` |
| 178 | + |
| 179 | +### Enumerating paths (assuming filesystem exists) |
| 180 | + |
| 181 | +```go |
| 182 | +const ( |
| 183 | + fs = "https://MYSTORAGEACCOUNT.dfs.core.windows.net/sample-fs" |
| 184 | +) |
| 185 | + |
| 186 | +// authenticate with Azure Active Directory |
| 187 | +cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 188 | +// TODO: handle error |
| 189 | + |
| 190 | +// create a filesystem client for the specified storage account |
| 191 | +client, err := filesystem.NewClient(fs, cred, nil) |
| 192 | +// TODO: handle error |
| 193 | + |
| 194 | +// path listings are returned across multiple pages |
| 195 | +pager := client.NewListPathsPager(true, nil) |
| 196 | + |
| 197 | +// continue fetching pages until no more remain |
| 198 | +for pager.More() { |
| 199 | + // advance to the next page |
| 200 | + page, err := pager.NextPage(context.TODO()) |
| 201 | + // TODO: handle error |
| 202 | + |
| 203 | + // print the path names for this page |
| 204 | + for _, path := range page.PathList.Paths { |
| 205 | + fmt.Println(*path.Name) |
| 206 | + fmt.Println(*path.IsDirectory) |
| 207 | + } |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +## Troubleshooting |
| 212 | + |
| 213 | +All Datalake service operations will return an |
| 214 | +[*azcore.ResponseError][azcore_response_error] on failure with a |
| 215 | +populated `ErrorCode` field. Many of these errors are recoverable. |
| 216 | +The [datalakeerror][datalake_error] package provides the possible Storage error codes |
| 217 | +along with various helper facilities for error handling. |
| 218 | + |
| 219 | + |
| 220 | +### Specialized clients |
| 221 | + |
| 222 | +The ADLS Gen2 Storage SDK for Go provides specialized clients in various subpackages. |
| 223 | + |
| 224 | +The [file][file] package contains APIs related to file path types. |
| 225 | + |
| 226 | +The [directory][directory] package contains APIs related to directory path types. |
| 227 | + |
| 228 | +The [lease][lease] package contains clients for managing leases on paths (paths represent both directory and file paths) and filesystems. Please see the [reference docs](https://docs.microsoft.com/rest/api/storageservices/lease-blob#remarks) for general information on leases. |
| 229 | + |
| 230 | +The [filesystem][filesystem] package contains APIs specific to filesystems. This includes APIs setting access policies or properties, and more. |
| 231 | + |
| 232 | +The [service][service] package contains APIs specific to Datalake service. This includes APIs for manipulating filesystems, retrieving account information, and more. |
| 233 | + |
| 234 | +The [sas][sas] package contains utilities to aid in the creation and manipulation of Shared Access Signature tokens. |
| 235 | +See the package's documentation for more information. |
| 236 | + |
| 237 | + |
| 238 | +You can find additional context and examples in our samples for each subpackage (named examples_test.go). |
| 239 | + |
| 240 | +## Contributing |
| 241 | + |
| 242 | +See the [Storage CONTRIBUTING.md][storage_contrib] for details on building, |
| 243 | +testing, and contributing to this library. |
| 244 | + |
| 245 | +This project welcomes contributions and suggestions. Most contributions require |
| 246 | +you to agree to a Contributor License Agreement (CLA) declaring that you have |
| 247 | +the right to, and actually do, grant us the rights to use your contribution. For |
| 248 | +details, visit [cla.microsoft.com][cla]. |
| 249 | + |
| 250 | +This project has adopted the [Microsoft Open Source Code of Conduct][coc]. |
| 251 | +For more information see the [Code of Conduct FAQ][coc_faq] |
| 252 | +or contact [[email protected]][coc_contact] with any |
| 253 | +additional questions or comments. |
| 254 | + |
| 255 | +<!-- LINKS --> |
| 256 | +[source]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azdatalake |
| 257 | +[docs]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azdatalake |
| 258 | +[rest_docs]: https://docs.microsoft.com/rest/api/storageservices/data-lake-storage-gen2 |
| 259 | +[godevdl]: https://go.dev/dl/ |
| 260 | +[goget]: https://pkg.go.dev/cmd/go#hdr-Add_dependencies_to_current_module_and_install_them |
| 261 | +[storage_account_docs]: https://docs.microsoft.com/azure/storage/common/storage-account-overview |
| 262 | +[storage_account_create_ps]: https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-powershell |
| 263 | +[storage_account_create_cli]: https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-cli |
| 264 | +[storage_account_create_portal]: https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-portal |
| 265 | +[azure_sub]: https://azure.microsoft.com/free/ |
| 266 | +[azidentity]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity |
| 267 | +[storage_ad]: https://docs.microsoft.com/azure/storage/common/storage-auth-aad |
| 268 | +[azcore_response_error]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore#ResponseError |
| 269 | +[datalake_error]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azdatalake/datalakeerror/error_codes.go |
| 270 | +[filesystem]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azdatalake/filesystem/client.go |
| 271 | +[lease]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azdatalake/lease |
| 272 | +[file]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azdatalake/file/client.go |
| 273 | +[directory]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azdatalake/directory/client.go |
| 274 | +[sas]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azdatalake/sas |
| 275 | +[service]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azdatalake/service/client.go |
| 276 | +[storage_contrib]: https://github.com/Azure/azure-sdk-for-go/blob/main/CONTRIBUTING.md |
| 277 | +[cla]: https://cla.microsoft.com |
| 278 | +[coc]: https://opensource.microsoft.com/codeofconduct/ |
| 279 | +[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ |
| 280 | +[coc_contact]: mailto:[email protected] |
0 commit comments