Question

Difficulty: MediumPerform Blob and Container Operations using Azure Storage SDKs

You are developing a Python backend application that uses the azure-storage-blob SDK (v12). The application needs to overwrite the content of a blob named config.json that has an active lease. The lease ID is 5b8f673d-8d2a-4f5a-9b4e-8c6e2b1a3c5d.

Which of the following code snippets should you use to successfully perform this operation?

  1. blob_client.upload_blob(data, overwrite=True, lease="5b8f673d-8d2a-4f5a-9b4e-8c6e2b1a3c5d")Answer
  2. B
    blob_client.upload_blob(data, overwrite=True, lease_id="5b8f673d-8d2a-4f5a-9b4e-8c6e2b1a3c5d")
  3. C
    lease_client = BlobLeaseClient(blob_client, lease_id="5b8f673d-8d2a-4f5a-9b4e-8c6e2b1a3c5d")
    lease_client.upload_blob(data, overwrite=True)
  4. D
    blob_client.upload_blob(data, overwrite=True, metadata={"lease-id": "5b8f673d-8d2a-4f5a-9b4e-8c6e2b1a3c5d"})

Answer

blob_client.upload_blob(data, overwrite=True, lease="5b8f673d-8d2a-4f5a-9b4e-8c6e2b1a3c5d")
The correct option correctly uses the 'lease' keyword argument of the 'upload_blob' method, passing the active lease ID. This enables the Azure Storage SDK to include the necessary lease validation headers, allowing the write operation to succeed on the leased blob.

Step-by-Step Solution

1
Identify the destination BlobClient and the lease ID associated with the active lease.
Destination blob_client is targeted at config.json, and the lease ID string is identified.
An active lease blocks any modifications to the blob unless the correct lease ID is supplied to authorize the operation.
2
Construct the upload_blob method call on BlobClient, passing the lease ID via the lease parameter.
The SDK serializes this parameter to the x-ms-lease-id HTTP request header.
The Azure Storage REST API matches this header against the active lease lock on the blob.
3
Execute the upload_blob call with overwrite=True.
The blob content is overwritten successfully.
Since the correct lease condition is validated, the service accepts the write action.

Key Concept

To perform modifications on an actively leased blob using the Azure Storage SDK for Python, the lease ID must be passed directly to the upload_blob method using the lease keyword argument.
Rate this question