| title | Manage properties and metadata for a blob with Go |
|---|---|
| titleSuffix | Azure Storage |
| description | Learn how to set and retrieve system properties and store custom metadata on blobs in your Azure Storage account using the Go client library. |
| services | storage |
| author | stevenmatthew |
| ms.author | shaas |
| ms.date | 08/05/2024 |
| ms.service | azure-blob-storage |
| ms.topic | how-to |
| ms.devlang | golang |
| ms.custom | devx-track-go, devguide-go |
[!INCLUDE storage-dev-guide-selector-manage-properties-blob]
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.
[!INCLUDE storage-dev-guide-prereqs-go]
[!INCLUDE storage-dev-guide-project-setup-go]
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).
-
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).
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:
:::code language="go" source="~/blob-devguide-go/cmd/blob-properties-metadata/blob_properties_metadata.go" id="snippet_set_blob_properties":::
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:
:::code language="go" source="~/blob-devguide-go/cmd/blob-properties-metadata/blob_properties_metadata.go" id="snippet_get_blob_properties":::
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:
:::code language="go" source="~/blob-devguide-go/cmd/blob-properties-metadata/blob_properties_metadata.go" id="snippet_set_blob_metadata":::
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:
:::code language="go" source="~/blob-devguide-go/cmd/blob-properties-metadata/blob_properties_metadata.go" id="snippet_get_blob_metadata":::
[!INCLUDE storage-dev-guide-code-samples-note-go]
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.
- View code samples from this article (GitHub)
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:
- Set Blob Properties (REST API)
- Get Blob Properties (REST API)
- Set Blob Metadata (REST API)
- Get Blob Metadata (REST API)
[!INCLUDE storage-dev-guide-resources-go]
[!INCLUDE storage-dev-guide-next-steps-go]