List blob containers with Go
When you list the containers in an Azure Storage account from your code, you can specify several options to manage how results are returned from Azure Storage. This article shows how to list containers using the Azure Storage client module for Go.
Prerequisites
- Azure subscription - create one for free
- 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 list blob containers. 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 List Containers (REST API).
About container listing options
When listing containers from your code, you can specify options to manage how results are returned from Azure Storage. You can specify the number of results to return in each set of results, and then retrieve the subsequent sets. You can also filter the results by a prefix, and return container metadata with the results. These options are described in the following sections.
To list containers in a storage account, call the following method:
This method returns a Pager, which allows your app to process one page of results at a time. Containers are ordered lexicographically by name.
You can specify options for listing containers by using the ListContainersOptions struct. This struct includes fields for managing the number of results, filtering by prefix, and including container information using ListContainersInclude.
Manage how many results are returned
By default, a listing operation returns up to 5,000 results at a time. To return a smaller set of results, provide a nonzero value for the MaxResults
field in the ListContainersOptions struct.
Filter results with a prefix
To filter the list of containers, specify a string or character for the Prefix
field in ListContainersOptions. The prefix string can include one or more characters. Azure Storage then returns only the containers whose names start with that prefix.
Include container metadata
To include container metadata with the results, set the Metadata
field to true
as part of ListContainersInclude. Azure Storage includes metadata with each container returned, so you don't need to fetch the container metadata separately.
Include deleted containers
To include soft-deleted containers with the results, set the Deleted
field to true
as part of ListContainersInclude.
Code examples
The following example lists all containers and metadata:
func listContainers(client *azblob.Client) {
// List the containers in the storage account and include metadata
pager := client.NewListContainersPager(&azblob.ListContainersOptions{
Include: azblob.ListContainersInclude{Metadata: true},
})
for pager.More() {
resp, err := pager.NextPage(context.TODO())
handleError(err)
for _, container := range resp.ContainerItems {
fmt.Println(*container.Name)
for k, v := range container.Metadata {
fmt.Printf("%v: %v\n", k, *v)
}
}
}
}
The following example lists only containers that begin with the specified prefix:
func listContainersWithPrefix(client *azblob.Client, prefix string) {
// List the containers in the storage account with a prefix
pager := client.NewListContainersPager(&azblob.ListContainersOptions{
Prefix: &prefix,
})
for pager.More() {
resp, err := pager.NextPage(context.TODO())
handleError(err)
for _, container := range resp.ContainerItems {
fmt.Println(*container.Name)
}
}
}
You can also specify a limit for the number of results per page. This example passes in a value for MaxResults
and paginates the results:
func listContainersWithMaxResults(client *azblob.Client, maxResults int32) {
// List the containers in the storage account with a maximum number of results
pager := client.NewListContainersPager(&azblob.ListContainersOptions{
MaxResults: &maxResults,
})
i := 0
for pager.More() {
resp, err := pager.NextPage(context.TODO())
handleError(err)
// Show page number to demonstrate pagination with max results
i++
fmt.Printf("Page %d:\n", i)
for _, container := range resp.ContainerItems {
fmt.Println(*container.Name)
}
}
}
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 listing containers 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 listing containers use the following REST API operation:
- List Containers (REST API)
Client module resources
See also
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.