ערוך

שתף באמצעות


Manage blob properties and metadata with Go

In addition to the data they contain, blobs support system properties and user-defined metadata. This article shows how to manage system properties and user-defined metadata using the Azure Storage client module for Go.

Prerequisites

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"
)

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 Set Blob Properties (REST API), Get Blob Properties (REST API), Set Blob Metadata (REST API), or Get Blob 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. Under the covers, 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.

Note

Blob index tags also provide the ability to store arbitrary user-defined key/value attributes alongside an Azure Blob storage resource. While similar to metadata, only blob index tags are automatically indexed and made searchable by the native blob service. Metadata cannot be indexed and queried unless you utilize a separate service such as Azure Search.

To learn more about this feature, see Manage and find data on Azure Blob storage with blob index (preview).

Set and retrieve properties

To set properties on a blob, call the following method from a blob client object:

Any properties not explicitly set are cleared. To preserve any existing properties, you can first retrieve the blob properties, then use them to populate the headers that aren't being updated.

The following code example sets the BlobContentType and BlobContentLanguage system properties on a blob, while preserving the existing properties:

func setBlobProperties(client *azblob.Client, containerName string, blobName string) {
    // Reference the blob as a client object
    blobClient := client.ServiceClient().NewContainerClient(containerName).NewBlobClient(blobName)

    // Get the existing blob properties
    resp, err := blobClient.GetProperties(context.TODO(), nil)
    handleError(err)

    // Set the new blob properties and include existing properties
    _, err = blobClient.SetHTTPHeaders(context.TODO(), blob.HTTPHeaders{
        BlobContentType:        to.Ptr("text/plain"),
        BlobContentLanguage:    to.Ptr("en-us"),
        BlobContentEncoding:    resp.ContentEncoding,
        BlobContentDisposition: resp.ContentDisposition,
        BlobCacheControl:       resp.CacheControl,
    }, nil)
    handleError(err)
}

To retrieve properties on a blob, call the following method from a blob client object:

The following code example gets a blob's system properties and displays some of the values:

func getBlobProperties(client *azblob.Client, containerName string, blobName string) {
    // Reference the blob as a client object
    blobClient := client.ServiceClient().NewContainerClient(containerName).NewBlobClient(blobName)

    // Get the blob properties
    resp, err := blobClient.GetProperties(context.TODO(), nil)
    handleError(err)

    // Print the blob properties
    fmt.Printf("Content type: %v\n", *resp.ContentType)
    fmt.Printf("Content language: %v\n", *resp.ContentLanguage)
}

Set and retrieve metadata

You can specify metadata as one or more name-value pairs on a blob or container resource. To set metadata, send a map containing name-value pairs using the following method from a blob client object:

The following code example sets metadata on a blob:

func setBlobMetadata(client *azblob.Client, containerName string, blobName string) {
    // Reference the blob as a client object
    blobClient := client.ServiceClient().NewContainerClient(containerName).NewBlobClient(blobName)

    // Set the blob metadata
    var metadata = make(map[string]*string)
    metadata["key1"] = to.Ptr("value1")
    metadata["key2"] = to.Ptr("value2")

    _, err := blobClient.SetMetadata(context.TODO(), metadata, nil)
    handleError(err)
}

To retrieve metadata, call the GetProperties method from a blob client object, and access the Metadata field in the response. The GetProperties method retrieves blob properties and metadata by calling both the Get Blob Properties operation and the Get Blob Metadata operation.

The following code example reads metadata on a blob and prints each key/value pair:

func getBlobMetadata(client *azblob.Client, containerName string, blobName string) {
    // Reference the blob as a client object
    blobClient := client.ServiceClient().NewContainerClient(containerName).NewBlobClient(blobName)

    // Get the blob properties, which includes metadata
    resp, err := blobClient.GetProperties(context.TODO(), nil)
    handleError(err)

    // Print the blob 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 how to manage system properties and user-defined metadata using the Azure Blob Storage client module for Go, see the following resources.

Code samples

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 managing system properties and user-defined metadata use the following REST API operations:

Client module resources