Question

Difficulty: MediumPerform Blob and Container Operations using Azure Storage SDKs

You are developing a C# application using the Azure.Storage.Blobs SDK (v12). The application needs to perform a concurrency-safe metadata update on an existing blob named config.json. You must acquire a lease, apply the metadata dictionary, and release the lease.

Which sequence of code actions should you perform to complete this operation? Arrange the actions in the correct order.

  1. 1BlobClient blobClient = containerClient.GetBlobClient("config.json");
  2. 2BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient();
  3. 3BlobLease lease = await leaseClient.AcquireAsync(TimeSpan.FromSeconds(30));
  4. 4await blobClient.SetMetadataAsync(metadata, new BlobRequestConditions { LeaseId = lease.LeaseId });
  5. 5await leaseClient.ReleaseAsync();

Answer

To perform a concurrency-safe metadata update using the Azure.Storage.Blobs SDK, you must first get the BlobClient instance, initialize the BlobLeaseClient, acquire the lease to obtain a LeaseId, set the metadata while passing the LeaseId in the BlobRequestConditions, and finally release the lease.
The correct sequence begins by locating the target blob, initializing the lease manager, locking the blob to secure a lease ID, applying the metadata change with the required request conditions, and finally freeing the resource for other tasks.

Step-by-Step Solution

1
Retrieve the BlobClient instance for config.json.
A reference to the target blob is established.
All subsequent lease and metadata operations require a valid BlobClient reference.
2
Initialize the BlobLeaseClient.
A lease client is mapped to the target BlobClient.
The Azure.Storage.Blobs.Specialized namespace uses the BlobLeaseClient class to manage locks on blobs.
3
Acquire the lease for a specified duration.
A BlobLease object is returned containing the active LeaseId.
You must establish the lease lock first to secure the resource before attempting to write changes.
4
Call SetMetadataAsync with the request conditions.
The metadata dictionary is updated on the Azure Storage blob.
Azure Storage rejects write requests on leased blobs unless the correct LeaseId is provided in the headers via BlobRequestConditions.
5
Release the lease lock.
The write lock is removed from the blob.
Releasing the lease immediately frees the resource for other operations instead of waiting for the lease duration to expire.

Key Concept

Concurrency management and metadata updates using BlobLeaseClient in Azure Storage SDK (v12).
Rate this question