Question

Difficulty: HardPerform Blob and Container Operations using Azure Storage SDKs

You are developing a C# application using the Azure.Storage.Blobs SDK (v12) to process documents in Azure Blob Storage. The application must retrieve a custom metadata property named ComplianceStatus from a blob, update its value to Approved, and save it back to the blob while preserving all other existing metadata.

You write the following method:

csharp
public static async Task UpdateComplianceStatusAsync(BlobClient blobClient)
{
BlobProperties properties = await blobClient.GetPropertiesAsync();

// Retrieve the existing compliance status
string currentStatus = [CODE_BLOCK_1];

if (currentStatus != "Approved")
{
// Update the compliance status
[CODE_BLOCK_2]
}
}

Which combination of code segments should you use to complete the method?

  1. A
    [CODE_BLOCK_1]: properties.Metadata["x-ms-meta-ComplianceStatus"]
    [CODE_BLOCK_2]:
    properties.Metadata["x-ms-meta-ComplianceStatus"] = "Approved";
    await blobClient.SetMetadataAsync(properties.Metadata);
  2. [CODE_BLOCK_1]: properties.Metadata["ComplianceStatus"]
    [CODE_BLOCK_2]:
    properties.Metadata["ComplianceStatus"] = "Approved";
    await blobClient.SetMetadataAsync(properties.Metadata);
    Answer
  3. C
    [CODE_BLOCK_1]: properties.Metadata["ComplianceStatus"]
    [CODE_BLOCK_2]:
    var metadata = new Dictionary<string, string> { { "ComplianceStatus", "Approved" } };
    await blobClient.SetMetadataAsync(metadata);
  4. D
    [CODE_BLOCK_1]: properties.Metadata["x-ms-meta-ComplianceStatus"]
    [CODE_BLOCK_2]:
    var metadata = new Dictionary<string, string> { { "x-ms-meta-ComplianceStatus", "Approved" } };
    await blobClient.SetMetadataAsync(metadata);

Answer

Retrieve the existing metadata value using properties.Metadata["ComplianceStatus"], assign the new value to the key in the same dictionary, and save it using await blobClient.SetMetadataAsync(properties.Metadata).
The correct option correctly references the ComplianceStatus metadata key without the x-ms-meta- prefix. Because the Azure.Storage.Blobs SDK handles the header translation internally, the dictionary keys in properties.Metadata map directly to the custom metadata names. Furthermore, the correct option updates the key directly on the retrieved properties.Metadata dictionary and passes it to SetMetadataAsync, which correctly preserves all other existing metadata key-value pairs.

Step-by-Step Solution

1
Retrieve existing blob properties
The properties object contains a Metadata dictionary that has the x-ms-meta- prefix stripped from all keys.
Before updating, we must retrieve the current metadata to avoid overwriting or losing other existing metadata keys.
2
Access and modify the specific key in the retrieved metadata dictionary
The value of the ComplianceStatus key is updated to Approved in properties.Metadata, leaving other keys unchanged.
By modifying the existing dictionary, we ensure that other existing metadata elements are preserved.
3
Call SetMetadataAsync with the modified dictionary
The blob's metadata is updated in Azure Storage with the changes.
Calling SetMetadataAsync replaces the target blob's metadata with the dictionary passed, so passing the modified dictionary successfully applies the update while preserving the rest of the metadata.

Key Concept

Azure Storage Blobs SDK v12 metadata operations require referencing dictionary keys without the x-ms-meta- prefix, and updates replace the entire metadata collection on the target resource.
Rate this question