Question

Difficulty: Very hardManage Properties, Metadata, and Access Tiers for Azure Blob Storage

You are developing a .NET application using the Azure.Storage.Blobs SDK to update the custom metadata of an existing block blob. To prevent concurrent writes and ensure that no existing metadata is lost, you must acquire an exclusive-write lease, retrieve the current metadata, append a new key-value pair, save the changes, and release the lease. Which sequence of steps should you perform to complete this process securely and without data loss?

  1. 1Call AcquireAsync on the BlobLeaseClient to obtain an exclusive-write lease on the blob.
  2. 2Call GetPropertiesAsync on the BlobClient to retrieve the current metadata dictionary.
  3. 3Modify the retrieved metadata dictionary by adding the new key-value pair.
  4. 4Call SetMetadataAsync on the BlobClient, passing the updated dictionary and a BlobRequestConditions object containing the active LeaseId.
  5. 5Call ReleaseAsync on the BlobLeaseClient to release the lease.

Answer

The correct sequence is: first acquire the lease using the lease client, then retrieve the properties using the blob client, modify the metadata dictionary, upload the updated metadata by passing the lease ID in the request conditions, and finally release the lease.
The correct sequence ensures that the blob is locked before any reads or writes occur, preventing dirty reads or lost updates. By acquiring the lease first, we guarantee that the metadata we retrieve is current and cannot be modified by any other client. Modifying the dictionary in memory preserves the existing keys because SetMetadataAsync replaces the entire metadata collection. Finally, passing the lease ID during the set operation is mandatory for leased blobs, and releasing the lease makes the blob available to others.

Step-by-Step Solution

1
Acquire the lease.
The blob is locked for exclusive-write access by this client.
Acquiring the lease first prevents race conditions by locking the resource before any read or write operation is initiated.
2
Retrieve current metadata.
The current metadata dictionary is read from the leased blob.
Reading the metadata while the lease is active guarantees that the metadata retrieved is the latest and cannot be changed by other processes before we write our updates.
3
Modify the metadata dictionary.
The local metadata dictionary is updated with the new key-value pair.
Since SetMetadataAsync overwrites all existing metadata on the blob, the modification must be done locally on the retrieved dictionary to preserve existing entries.
4
Write the updated metadata back to the blob.
The metadata on Azure Blob Storage is updated.
The write operation must include the lease ID in BlobRequestConditions to authorize the modification on the locked blob.
5
Release the lease.
The blob is unlocked.
The lease must be explicitly released so that other clients and processes can perform modifications on the blob.

Key Concept

To safely modify a leased blob's metadata without data loss or race conditions, you must acquire the lease, retrieve properties, modify the dictionary, write the metadata back using the lease ID, and then release the lease.
Rate this question