Question

Difficulty: Very hardPerform Blob and Container Operations using Azure Storage SDKs

You are developing a C# background service that processes large video files stored in Azure Blob Storage using the Azure.Storage.Blobs SDK (v12). To prevent multiple instances of the service from processing the same video simultaneously, each instance first acquires an exclusive lease on the target blob. Once processing is complete, the service must perform the following tasks in a thread-safe manner that maintains the concurrency lock until all changes are committed:

1. Write a custom metadata tag to the blob with the key "Status" and the value "Processed".
2. Release the lease immediately afterward to allow other services to access the blob.

The helper method signature is defined as follows:

csharp
public async Task CompleteProcessingAsync(BlobClient blobClient, BlobLeaseClient leaseClient, string leaseId)
{
// Implementation
}

Which of the following code blocks should you use to implement this method?

  1. A
    csharp
    var metadata = new Dictionary<string, string> { { "Status", "Processed" } };
    await leaseClient.ReleaseAsync();
    await blobClient.SetMetadataAsync(metadata);
  2. B
    csharp
    var metadata = new Dictionary<string, string> { { "x-ms-meta-Status", "Processed" } };
    var conditions = new BlobRequestConditions { LeaseId = leaseId };
    await blobClient.SetMetadataAsync(metadata, conditions);
    await leaseClient.ReleaseAsync();
  3. csharp
    var metadata = new Dictionary<string, string> { { "Status", "Processed" } };
    var conditions = new BlobRequestConditions { LeaseId = leaseId };
    await blobClient.SetMetadataAsync(metadata, conditions);
    await leaseClient.ReleaseAsync();
    Answer
  4. D
    csharp
    var metadata = new Dictionary<string, string> { { "Status", "Processed" } };
    await blobClient.SetMetadataAsync(metadata);
    await leaseClient.ReleaseAsync();

Answer

The correct implementation creates a metadata dictionary with the key 'Status', applies it using a BlobRequestConditions object initialized with the active lease ID, and then releases the lease.
The correct implementation updates the metadata first while holding the lease lock, by passing the lease ID within a BlobRequestConditions object to SetMetadataAsync. This prevents concurrent modifications. Once the metadata is successfully updated, ReleaseAsync is called to safely release the lease. In the modern Azure.Storage.Blobs SDK (v12), custom metadata dictionary keys do not require the 'x-ms-meta-' prefix as the SDK manages HTTP header conversions automatically.

Step-by-Step Solution

1
Configure the metadata dictionary keys.
Use 'Status' as the dictionary key without the 'x-ms-meta-' prefix.
The Azure.Storage.Blobs SDK automatically handles the 'x-ms-meta-' HTTP header prefix wrapper during request serialization.
2
Enforce concurrency during the metadata update.
Instantiate a BlobRequestConditions object and assign the LeaseId property to the active leaseId string.
Azure Blob Storage requires the active lease ID to modify any blob or container that currently holds an active exclusive lease lock.
3
Sequence the operations safely.
Invoke SetMetadataAsync with the request conditions first, and then invoke ReleaseAsync on the BlobLeaseClient.
Releasing the lease first exposes the blob to concurrent modifications by other processes before the final state is committed.

Key Concept

Blob Lease Concurrency and SDK Metadata Handling

Alternative Method

Alternatively, you can perform the metadata update directly on the BlobLeaseClient instance if using specific SDK versions, but using BlobRequestConditions on the BlobClient is the standard method for granular condition control.
Estimated Time:3m 0s
Rate this question