Azure Blob Storage module for Go
Service Version: 2023-11-03
Azure Blob Storage is Microsoft's object storage solution for the cloud. Blob
Storage is optimized for storing massive amounts of unstructured data - data that does not adhere to a particular data model or
definition, such as text or binary data. For more information, see Introduction to Azure Blob Storage.
Use the Azure Blob Storage client module github.com/Azure/azure-sdk-for-go/sdk/storage/azblob
to:
- Authenticate clients with Azure Blob Storage
- Manipulate containers and blobs in an Azure storage account
Key links:
Source code | API reference documentation | REST API documentation | Product documentation | Samples
Getting started
Prerequisites
az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location westus --sku Standard_LRS
Install the package
Install the Azure Blob Storage client module for Go with go get:
go get github.com/Azure/azure-sdk-for-go/sdk/storage/azblob
If you plan to authenticate with Azure Active Directory (recommended), also install the azidentity module.
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
Authenticate the client
To interact with the Azure Blob Storage service, you'll need to create an instance of the azblob.Client
type. The azidentity module makes it easy to add Azure Active Directory support for authenticating Azure SDK clients with their corresponding Azure services.
cred, err := azidentity.NewDefaultAzureCredential(nil)
client, err := azblob.NewClient("https://MYSTORAGEACCOUNT.blob.core.windows.net/", cred, nil)
Learn more about enabling Azure Active Directory for authentication with Azure Storage:
Other options for authentication include connection strings, shared key, shared access signatures (SAS), and anonymous public access. Use the appropriate client constructor function for the authentication mechanism you wish to use. For examples, see:
Key concepts
Blob Storage is designed for:
- Serving images or documents directly to a browser.
- Storing files for distributed access.
- Streaming video and audio.
- Writing to log files.
- Storing data for backup and restore, disaster recovery, and archiving.
- Storing data for analysis by an on-premises or Azure-hosted service.
Blob Storage offers three types of resources:
- The storage account
- One or more containers in a storage account
- One or more blobs in a container
Instances of the azblob.Client
type provide methods for manipulating containers and blobs within a storage account.
The storage account is specified when the azblob.Client
is constructed.
Specialized clients
The Azure Blob Storage client module for Go also provides specialized clients in various subpackages. Use these clients when you need to interact with a specific kind of blob. Learn more about block blobs, append blobs, and page blobs.
The blob package contains APIs common to all blob types. This includes APIs for deleting and undeleting a blob, setting metadata, and more.
The lease package contains clients for managing leases on blobs and containers. See the REST API reference for general information on leases.
The container package contains APIs specific to containers. This includes APIs for setting access policies or properties, and more.
The service package contains APIs specific to the Blob service. This includes APIs for manipulating containers, retrieving account information, and more.
The sas package contains utilities to aid in the creation and manipulation of shared access signature (SAS) tokens.
See the package's documentation for more information.
Goroutine safety
We guarantee that all client instance methods are goroutine-safe and independent of each other (see guideline). This ensures that the recommendation to reuse client instances is always safe, even across goroutines.
Blob metadata
Blob 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.
Additional concepts
Client options |
Accessing the response |
Handling failures |
Logging
Examples
Upload a blob
const (
account = "https://MYSTORAGEACCOUNT.blob.core.windows.net/"
containerName = "sample-container"
blobName = "sample-blob"
sampleFile = "path/to/sample/file"
)
cred, err := azidentity.NewDefaultAzureCredential(nil)
client, err := azblob.NewClient(account, cred, nil)
file, err := os.OpenFile(sampleFile, os.O_RDONLY, 0)
defer file.Close()
_, err = client.UploadFile(context.TODO(), containerName, blobName, file, nil)
Download a blob
client, err := azblob.NewClientWithNoCredential("https://azurestoragesamples.blob.core.windows.net/", nil)
file, err := os.Create("cloud.jpg")
defer file.Close()
_, err = client.DownloadFile(context.TODO(), "samples", "cloud.jpg", file, nil)
Enumerate blobs
const (
account = "https://MYSTORAGEACCOUNT.blob.core.windows.net/"
containerName = "sample-container"
)
cred, err := azidentity.NewDefaultAzureCredential(nil)
client, err := azblob.NewClient(account, cred, nil)
pager := client.NewListBlobsFlatPager(containerName, nil)
for pager.More() {
page, err := pager.NextPage(context.TODO())
for _, blob := range page.Segment.BlobItems {
fmt.Println(*blob.Name)
}
}
Troubleshooting
All Blob service operations will return an
*azcore.ResponseError on failure with a
populated ErrorCode
field. Many of these errors are recoverable.
The bloberror package provides the possible Storage error codes
along with helper facilities for error handling.
const (
connectionString = "<connection_string>"
containerName = "sample-container"
)
client, err := azblob.NewClientFromConnectionString(connectionString, nil)
_, err = client.DeleteContainer(context.TODO(), containerName, nil)
if bloberror.HasCode(err, bloberror.ContainerBeingDeleted, bloberror.ContainerNotFound) {
} else if err != nil {
}
Next steps
Get started with our Blob samples. They contain complete examples of the above snippets and more.
Contributing
See the Storage CONTRIBUTING.md for details on building,
testing, and contributing to this library.
This project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution. For
details, visit cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct.
For more information see the Code of Conduct FAQ
or contact opencode@microsoft.com with any
additional questions or comments.