Create a blob container with Go
This article shows how to create containers with the Azure Storage client module for Go. Blobs in Azure Storage are organized into containers. Before you can upload a blob, you must first create a container.
Prerequisites
- Azure subscription - create one for trial
- Azure storage account - create a storage account
- Go 1.18+
Set up your environment
If you don't have an existing project, this section shows how to set up a project to work with the Azure Blob Storage client module for Go. The steps include module installation, adding import
paths, and creating an authorized client object. For details, see Get started with Azure Blob Storage and Go.
Install modules
Install the azblob module using the following command:
go get github.com/Azure/azure-sdk-for-go/sdk/storage/azblob
To authenticate with Microsoft Entra ID (recommended), install the azidentity
module using the following command:
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
Add import paths
In your code file, add the following import paths:
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
These import paths represent the minimum needed to get started. Some code examples in this article might require additional import paths. For specific details and example usage, see Code samples.
Create a client object
To connect an app to Blob Storage, create a client object using azblob.NewClient. The following example shows how to create a client object using DefaultAzureCredential
for authorization:
func getServiceClientTokenCredential(accountURL string) *azblob.Client {
// Create a new service client with token credential
credential, err := azidentity.NewDefaultAzureCredential(nil)
handleError(err)
client, err := azblob.NewClient(accountURL, credential, nil)
handleError(err)
return client
}
Authorization
The authorization mechanism must have the necessary permissions to create a container. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Create Container (REST API).
About container naming
A container name must be a valid DNS name, as it forms part of the unique URI used to address the container or its blobs. Follow these rules when naming a container:
- Container names can be between 3 and 63 characters long.
- Container names must start with a letter or number, and can contain only lowercase letters, numbers, and the dash (-) character.
- Consecutive dash characters aren't permitted in container names.
The URI for a container resource is formatted as follows:
https://my-account-name.blob.core.chinacloudapi.cn/my-container-name
Create a container
To create a container, call the following method:
Containers are created immediately beneath the storage account. It's not possible to nest one container beneath another. An exception is thrown if a container with the same name already exists.
The following example shows how to create a container:
func createContainer(client *azblob.Client, containerName string) {
// Create a container
_, err := client.CreateContainer(context.TODO(), containerName, nil)
handleError(err)
}
Create the root container
A root container serves as a default container for your storage account. Each storage account can have one root container, which must be named $root. The root container must be explicitly created or deleted.
You can reference a blob stored in the root container without including the root container name. The root container enables you to reference a blob at the top level of the storage account hierarchy. For example, you can reference a blob in the root container as follows:
https://<storage-account-name>.blob.core.chinacloudapi.cn/default.html
The following example creates the container if it doesn't already exist in the storage account:
func createRootContainer(client *azblob.Client) {
// Create root container
_, err := client.CreateContainer(context.TODO(), "$root", nil)
handleError(err)
}
Note
The code samples in this guide are intended to help you get started with Azure Blob Storage and Go. You should modify error handling and Context
values to meet the needs of your application.
Resources
To learn more about creating a container using the Azure Blob Storage client module for Go, see the following resources.
Code samples
- View code samples from this article (GitHub)
REST API operations
The Azure SDK for Go contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Go paradigms. The client library methods for creating a container use the following REST API operation:
- Create Container (REST API)
Client module resources
Related content
- This article is part of the Blob Storage developer guide for Go. To learn more, see the full list of developer guide articles at Build your Go app.