Question

Difficulty: MediumPerform Blob and Container Operations using Azure Storage SDKs

You are developing a Python application that uses the `azure-storage-blob` SDK (v12). You write the following code to upload a blob and assign custom metadata:

python
from azure.storage.blob import BlobServiceClient

service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = service_client.get_blob_client(container="reports", blob="annual_report.pdf")

blob_client.upload_blob(data=pdf_data, metadata={"ProjectName": "Delta"}, overwrite=True)

Later, you need to read this metadata value from the blob. Which code segment should you use to retrieve the value of the `ProjectName` metadata?

  1. A
    python
    properties = blob_client.get_blob_properties()
    project_name = properties.metadata.get("ProjectName")
  2. B
    python
    properties = blob_client.get_blob_properties()
    project_name = properties.metadata.get("x-ms-meta-projectname")
  3. python
    properties = blob_client.get_blob_properties()
    project_name = properties.metadata.get("projectname")
    Answer
  4. D
    python
    properties = blob_client.get_blob_properties()
    project_name = properties.metadata.get("x-ms-meta-ProjectName")

Answer

Retrieve the blob properties and access the metadata dictionary using the lowercase key 'projectname' without any 'x-ms-meta-' prefix.
The correct option retrieves the metadata using the key in lowercase ('projectname'). This is because the Azure Storage SDK for Python normalizes all metadata keys to lowercase and strips the 'x-ms-meta-' prefix.

Step-by-Step Solution

1
Call `blob_client.get_blob_properties()`.
A `BlobProperties` object is returned containing the blob metadata, properties, and system-defined attributes.
This SDK call is required to pull the latest properties and metadata of the blob from the Azure Storage service.
2
Access the `metadata` dictionary on the returned properties object.
A Python dictionary containing the parsed user-defined metadata.
User-defined metadata is stored in the `metadata` property of the `BlobProperties` instance.
3
Use the lowercase key `'projectname'` to look up the value.
The metadata value `'Delta'` is successfully returned.
The Azure Storage SDK for Python strips the HTTP prefix `'x-ms-meta-'` and normalizes all dictionary keys to lowercase.

Key Concept

Azure Blob metadata keys are case-insensitive HTTP headers under the hood; the Python SDK handles this by stripping the 'x-ms-meta-' prefix and exposing all keys in lowercase.
Rate this question