Soru

Zorluk: OrtaPerform Blob and Container Operations using Azure Storage SDKs

You are developing a Python background utility to safely update a shared configuration file stored in Azure Blob Storage. The application uses the azure-storage-blob SDK (v12).

To prevent concurrent writes, the application first acquires a lease on the blob using the following code:

python
from azure.storage.blob import BlobServiceClient, BlobLeaseClient

connection_string = "UseDevelopmentStorage=true"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(container="configs", blob="appsettings.json")

# Acquire a 30-second lease on the blob
lease_client = BlobLeaseClient(blob_client)
lease_client.acquire(lease_duration=30)

# Modify the configuration data
updated_config_data = b'{"status": "ready", "version": "2.0"}'

Which of the following code statements must you use to upload the updated configuration to the leased blob?

  1. blob_client.upload_blob(updated_config_data, overwrite=True, lease=lease_client)Cevap
  2. B
    blob_client.upload_blob(updated_config_data, overwrite=True)
  3. C
    blob_client.upload_blob(updated_config_data, overwrite=True, lease_id=lease_client.lease_id)
  4. D
    blob_client.upload_blob(updated_config_data, overwrite=True, headers={"x-ms-lease-id": lease_client.lease_id})

Cevap

blob_client.upload_blob(updated_config_data, overwrite=True, lease=lease_client)
The correct statement is the option using the lease parameter set to the lease_client object. In the Azure Storage SDK for Python (v12), you authorize modifications to a leased blob by passing either the BlobLeaseClient instance or its lease_id string to the lease parameter of the write operation (such as upload_blob).

Adım Adım Çözüm

1
Initialize the lease client and acquire the lease
The blob is leased for 30 seconds, preventing other clients from writing or deleting it without providing the lease ID.
Secures the blob for safe updates.
2
Call upload_blob and pass the lease argument
The write operation is sent to Azure Storage with the lease identifier included in the request headers automatically by the SDK.
Allows the write operation to bypass the lease block since the caller owns the lease.

Anahtar Kavram

Writing to leased blobs using the Azure Storage Python SDK v12
Bu soruyu puanla