Manage container properties and metadata with Go
Blob containers support system properties and user-defined metadata, in addition to the data they contain. This article shows how to manage system properties and user-defined metadata with the Azure Storage client module for Go.
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 work with container properties or metadata. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Reader or higher for the get operations, and Storage Blob Data Contributor or higher for the set operations. To learn more, see the authorization guidance for Get Container Properties (REST API), Set Container Metadata (REST API), or Get Container Metadata (REST API).
About properties and metadata
System properties: System properties exist on each Blob Storage resource. Some of them can be read or set, while others are read-only. Behind the scenes, some system properties correspond to certain standard HTTP headers. The Azure Storage client library for Go maintains these properties for you.
User-defined metadata: User-defined metadata consists of one or more name-value pairs that you specify for a Blob storage resource. You can use metadata to store additional values with the resource. Metadata values are for your own purposes only, and don't affect how the resource behaves.
Metadata name/value pairs are valid HTTP headers and should adhere to all restrictions governing HTTP headers. For more information about metadata naming requirements, see Metadata names.
Retrieve container properties
To retrieve container properties, call the following method from a container client object:
The following code example fetches a container's system properties and writes some of the property values to a console window:
func getContainerProperties(client *azblob.Client, containerName string) {
// Reference the container as a client object
containerClient := client.ServiceClient().NewContainerClient(containerName)
// Get the container properties
resp, err := containerClient.GetProperties(context.TODO(), nil)
handleError(err)
// Print the container properties
fmt.Printf("Blob public access: %v\n", *resp.BlobPublicAccess)
fmt.Printf("Lease status: %v\n", *resp.LeaseStatus)
fmt.Printf("Lease state: %v\n", *resp.LeaseState)
fmt.Printf("Has immutability policy: %v\n", *resp.HasImmutabilityPolicy)
}
Set and retrieve metadata
You can specify metadata as one or more name-value pairs on a blob or container resource. To set metadata, call the following method from a container client object:
Setting container metadata overwrites all existing metadata associated with the container. It's not possible to modify an individual name-value pair.
The following code example sets metadata on a container:
func setContainerMetadata(client *azblob.Client, containerName string) {
// Reference the container as a client object
containerClient := client.ServiceClient().NewContainerClient(containerName)
// Set the container metadata
var metadata = make(map[string]*string)
metadata["key1"] = to.Ptr("value1")
metadata["key2"] = to.Ptr("value2")
_, err := containerClient.SetMetadata(context.TODO(), nil)
handleError(err)
}
To retrieve metadata, call the following method from a container client object:
Metadata is included in the response from GetProperties
. The following example reads in metadata values and writes them to a console window:
func getContainerMetadata(client *azblob.Client, containerName string) {
// Reference the container as a client object
containerClient := client.ServiceClient().NewContainerClient(containerName)
// Get the container properties, which includes metadata
resp, err := containerClient.GetProperties(context.TODO(), nil)
handleError(err)
// Print the container metadata
for k, v := range resp.Metadata {
fmt.Printf("%v: %v\n", k, *v)
}
}
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 setting and retrieving container properties and metadata 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 setting and retrieving properties and metadata use the following REST API operations:
- Get Container Properties (REST API)
- Set Container Metadata (REST API)
- Get Container Metadata (REST API)
The get_container_properties
method retrieves container properties and metadata by calling both the Get Container Properties operation and the Get Container Metadata operation.
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.