Question

Difficulty: HardPerform Blob and Container Operations using Azure Storage SDKs

You are developing a backend synchronization service in C# using the Azure.Storage.Blobs SDK (v12). The service needs to update the metadata of a critical blob. However, another worker instance crashed while holding an active lease on the blob, blocking updates. To recover, your service must immediately break the existing lease, secure a new lease to prevent other instances from writing, update the blob's metadata, and clean up. Arrange the steps in the correct order to perform this workflow.

  1. 1Instantiate a BlobClient using the connection string, container name, and blob name.
  2. 2Instantiate a BlobLeaseClient by passing the BlobClient to its constructor.
  3. 3Call BreakAsync on the BlobLeaseClient to immediately terminate the active lease held by the crashed worker.
  4. 4Call AcquireAsync on the BlobLeaseClient to obtain a new exclusive lease.
  5. 5Call SetMetadataAsync on the BlobClient with the updated metadata dictionary, passing the new lease ID within a BlobRequestConditions object.
  6. 6Call ReleaseAsync on the BlobLeaseClient to release the lease.

Answer

First, instantiate a BlobClient, then instantiate a BlobLeaseClient. Next, break the existing lease, acquire a new lease, set the metadata using the new lease ID in the request conditions, and finally release the lease.
The correct sequence starts by obtaining a BlobClient, which is used to construct a BlobLeaseClient. Since the blob is currently locked by a crashed worker's lease, the lease must first be broken using BreakAsync. After the lease is broken, the current process must call AcquireAsync to get a new lease for itself. The metadata is then updated using SetMetadataAsync, passing the new lease ID in the request conditions to authorize the write operation on the locked blob. Finally, ReleaseAsync is called to free the lock.

Step-by-Step Solution

1
Instantiate BlobClient.
An instance of BlobClient pointing to the target blob is created.
A blob client is required to reference the target storage resource.
2
Instantiate BlobLeaseClient.
A lease client associated with the blob client is created.
The lease client is used to perform lease actions like breaking, acquiring, and releasing.
3
Call BreakAsync on the lease client.
The crashed worker's lease is broken.
A lease must be broken before another client can acquire a lease on the same blob immediately.
4
Call AcquireAsync on the lease client.
A new lease is acquired and a lease ID is generated.
Acquiring a new lease secures exclusive access for the current thread of execution.
5
Call SetMetadataAsync on the blob client.
The blob's metadata is updated.
The metadata must be written along with the lease ID inside BlobRequestConditions to satisfy the lease requirement.
6
Call ReleaseAsync on the lease client.
The lease is released.
Releasing the lease cleans up the lock and allows other workers to acquire it.

Key Concept

Managing blob leases and performing write operations under an active lease using the Azure.Storage.Blobs SDK.
Rate this question