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?
- 1Call AcquireAsync on the BlobLeaseClient to obtain an exclusive-write lease on the blob.
- 2Call GetPropertiesAsync on the BlobClient to retrieve the current metadata dictionary.
- 3Modify the retrieved metadata dictionary by adding the new key-value pair.
- 4Call SetMetadataAsync on the BlobClient, passing the updated dictionary and a BlobRequestConditions object containing the active LeaseId.
- 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
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.