# README
Azure Blob Storage SDK for Go
Server Version: 2020-10-02
Azure Blob storage is Microsoft's object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that does not adhere to a particular data model or definition, such as text or binary data.
Source code | API reference documentation | REST API documentation | Product documentation
Getting started
Install the package
Install the Azure Blob Storage SDK for Go with go get:
go get github.com/Azure/azure-sdk-for-go/sdk/storage/azblob
If you're going to authenticate with Azure Active Directory (recommended), install the azidentity module.
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
Prerequisites
A supported Go version (the Azure SDK supports the two most recent Go releases).
You need an Azure subscription and a Storage Account to use this package.
To create a new Storage Account, you can use the Azure Portal, Azure PowerShell, or the Azure CLI. Here's an example using the Azure CLI:
az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location westus --sku Standard_LRS
Authenticate the client
In order 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.
// create a credential for authenticating with Azure Active Directory
cred, err := azidentity.NewDefaultAzureCredential(nil)
// TODO: handle err
// create an azblob.Client for the specified storage account that uses the above credential
client, err := azblob.NewClient("https://MYSTORAGEACCOUNT.blob.core.windows.net/", cred, nil)
// TODO: handle err
Learn more about enabling Azure Active Directory for authentication with Azure Storage in our documentation and our samples.
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 ore 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.
Use the appropriate client constructor function for the authentication mechanism you wish to use.
Learn more about options for authentication (including Connection Strings, Shared Key, Shared Access Signatures (SAS), Azure Active Directory (AAD), and anonymous public access) in our examples.
Goroutine safety
We guarantee that all client instance methods are goroutine-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across goroutines.
About 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
Uploading a blob
const (
account = "https://MYSTORAGEACCOUNT.blob.core.windows.net/"
containerName = "sample-container"
blobName = "sample-blob"
sampleFile = "path/to/sample/file"
)
// authenticate with Azure Active Directory
cred, err := azidentity.NewDefaultAzureCredential(nil)
// TODO: handle error
// create a client for the specified storage account
client, err := azblob.NewClient(account, cred, nil)
// TODO: handle error
// open the file for reading
file, err := os.OpenFile(sampleFile, os.O_RDONLY, 0)
// TODO: handle error
defer file.Close()
// upload the file to the specified container with the specified blob name
_, err = client.UploadFile(context.TODO(), containerName, blobName, file, nil)
// TODO: handle error
Downloading a blob
// this example accesses a public blob via anonymous access, so no credentials are required
client, err := azblob.NewClientWithNoCredential("https://azurestoragesamples.blob.core.windows.net/", nil)
// TODO: handle error
// create or open a local file where we can download the blob
file, err := os.Create("cloud.jpg")
// TODO: handle error
defer file.Close()
// download the blob
_, err = client.DownloadFile(context.TODO(), "samples", "cloud.jpg", file, nil)
// TODO: handle error
Enumerating blobs
const (
account = "https://MYSTORAGEACCOUNT.blob.core.windows.net/"
containerName = "sample-container"
)
// authenticate with Azure Active Directory
cred, err := azidentity.NewDefaultAzureCredential(nil)
// TODO: handle error
// create a client for the specified storage account
client, err := azblob.NewClient(account, cred, nil)
// TODO: handle error
// blob listings are returned across multiple pages
pager := client.NewListBlobsFlatPager(containerName, nil)
// continue fetching pages until no more remain
for pager.More() {
// advance to the next page
page, err := pager.NextPage(context.TODO())
// TODO: handle error
// print the blob names for this page
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 various helper facilities for error handling.
const (
connectionString = "<connection_string>"
containerName = "sample-container"
)
// create a client with the provided connection string
client, err := azblob.NewClientFromConnectionString(connectionString, nil)
// TODO: handle error
// try to delete the container, avoiding any potential race conditions with an in-progress or completed deletion
_, err = client.DeleteContainer(context.TODO(), containerName, nil)
if bloberror.HasCode(err, bloberror.ContainerBeingDeleted, bloberror.ContainerNotFound) {
// ignore any errors if the container is being deleted or already has been deleted
} else if err != nil {
// TODO: some other error
}
Next steps
Get started with our Blob samples. They contain complete examples of the above snippets and more.
Specialized clients
The Azure Blob Storage SDK 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 the various types of blobs from the following links.
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. Please see the reference docs for general information on leases.
The container package contains APIs specific to containers. This includes APIs setting access policies or properties, and more.
The service package contains APIs specific to 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 tokens. See the package's documentation for more information.
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 [email protected] with any additional questions or comments.