Question

Difficulty: MediumPerform Blob and Container Operations using Azure Storage SDKs

You need to update a specific metadata tag on an existing Azure Blob Storage blob using the Azure.Storage.Blobs SDK (v12) for C# while ensuring that all other existing metadata key-value pairs on the blob are preserved. Which sequence of steps should you perform?

  1. 1Instantiate a BlobClient referencing the target blob using an authorized connection string.
  2. 2Call GetPropertiesAsync() on the client and access the Metadata dictionary from the returned properties.
  3. 3Add or modify the target key-value pairs directly within the retrieved metadata dictionary without prefixing keys with x-ms-meta-.
  4. 4Call SetMetadataAsync() on the client, passing the updated metadata dictionary as the argument.

Answer

To update a specific metadata key on a blob while preserving existing metadata using the Azure.Storage.Blobs SDK (v12) for C#, you must first instantiate a BlobClient, call GetPropertiesAsync() to retrieve the current Metadata dictionary, modify or add the key-value pairs in memory, and then call SetMetadataAsync() with the updated dictionary.
The correct order requires first initializing the BlobClient to communicate with the service, then calling GetPropertiesAsync() to fetch the existing metadata so it is not lost. Next, the metadata dictionary is modified in memory, and finally, SetMetadataAsync() is called to upload the entire updated dictionary.

Step-by-Step Solution

1
Instantiate BlobClient
A client object targeting the specific blob is created.
All SDK operations on the blob require a configured client instance.
2
Call GetPropertiesAsync()
The current blob properties, including the existing metadata dictionary, are retrieved.
Since the SDK's metadata update operation is a full overwrite, you must retrieve existing values first to avoid losing them.
3
Modify the metadata dictionary
The target key-value pairs are added or updated in the dictionary in memory.
Modifying the dictionary in memory prepares the complete payload for the update. Keys do not need the HTTP prefix.
4
Call SetMetadataAsync()
The updated metadata dictionary is sent to Azure Storage and applied to the blob.
This persists the updated collection of metadata on the blob.

Key Concept

Preserving metadata during updates with Azure.Storage.Blobs SDK
Rate this question