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?
- approver = properties.metadata.get("approveremail")
if approver:
metadata = properties.metadata
metadata["status"] = "Approved"
blob_client.set_blob_metadata(metadata)Answer - Bapprover = properties.metadata.get("ApproverEmail")
if approver:
metadata = properties.metadata
metadata["Status"] = "Approved"
blob_client.set_blob_metadata(metadata) - Capprover = properties.metadata.get("x-ms-meta-approveremail")
if approver:
metadata = properties.metadata
metadata["x-ms-meta-status"] = "Approved"
blob_client.set_blob_metadata(metadata) - Dapprover = properties.metadata.get("approveremail")
if approver:
blob_client.set_blob_metadata(metadata={"status": "Approved"})
Answer
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.
Step-by-Step Solution
Key Concept
Handling casing and prefixing when reading and writing blob metadata using the Azure Storage SDK.