Soru

Zorluk: ZorPerform Blob and Container Operations using Azure Storage SDKs

You are developing a Python application using the azure-storage-blob (v12) SDK to audit media uploads in an Azure Blob Storage container named 'images'. The application must retrieve a blob's properties and inspect its custom metadata for a key named 'ApproverEmail'. If this key exists, the application must append a new custom metadata key-value pair of 'Status: Approved' to the blob, while preserving all other existing metadata. Which of the following code segments should you use to retrieve the email and update the metadata?

  1. approver = properties.metadata.get("approveremail")
    if approver:
    metadata = properties.metadata
    metadata["status"] = "Approved"
    blob_client.set_blob_metadata(metadata)
    Cevap
  2. B
    approver = properties.metadata.get("ApproverEmail")
    if approver:
    metadata = properties.metadata
    metadata["Status"] = "Approved"
    blob_client.set_blob_metadata(metadata)
  3. C
    approver = properties.metadata.get("x-ms-meta-approveremail")
    if approver:
    metadata = properties.metadata
    metadata["x-ms-meta-status"] = "Approved"
    blob_client.set_blob_metadata(metadata)
  4. D
    approver = properties.metadata.get("approveremail")
    if approver:
    blob_client.set_blob_metadata(metadata={"status": "Approved"})

Cevap

Retrieve the metadata using the lowercased key 'approveremail', mutate the retrieved dictionary to append 'status', and call set_blob_metadata with the mutated dictionary.
The correct answer retrieves the metadata using the lowercased key 'approveremail', appends the new key-value pair to the existing metadata dictionary, and updates the blob's metadata using set_blob_metadata. This correctly accounts for the SDK's lowercase key normalization and avoids replacing the entire metadata collection with only the new key.

Adım Adım Çözüm

1
Retrieve the existing metadata dictionary from the blob properties, noting that the SDK lowercases all returned metadata keys.
The metadata dictionary is accessed, and the key 'approveremail' is looked up using lowercase characters.
The SDK normalizes header keys returned from the REST API to lowercase in Python dictionaries, so case-sensitive lookups for the original casing will fail.
2
Add the new metadata key-value pair directly to the retrieved dictionary to preserve existing metadata.
The dictionary now contains all original metadata keys along with the new key.
Calling set_blob_metadata completely replaces existing metadata on the blob, so mutating the retrieved dictionary is required to prevent data loss.
3
Call the set_blob_metadata method on the BlobClient passing the mutated dictionary, without adding any 'x-ms-meta-' prefixes.
The blob's metadata is successfully updated in Azure Blob Storage.
The SDK handles the 'x-ms-meta-' header prefixing automatically, so custom prefixing should not be done in application code.

Anahtar Kavram

Handling casing and prefixing when reading and writing blob metadata using the Azure Storage SDK.
Bu soruyu puanla