Question

Difficulty: HardPerform Blob and Container Operations using Azure Storage SDKs

An enterprise data ingestion workflow in C# uses the Azure.Storage.Blobs SDK (v12) to process telemetry payloads. To prevent concurrent write conflicts on a shared blob named `active_logs.json`, the workflow must acquire an exclusive 30-second write lock (lease), perform the upload, and subsequently update the blob's metadata. Consider the following code skeleton:

csharp
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;

public class LogProcessor
{ public static async Task UploadLogWithLeaseAsync(BlobClient blobClient, Stream logStream)
{
BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient();
BlobLease lease = await leaseClient.AcquireAsync(TimeSpan.FromSeconds(30));

// Configure upload options
BlobUploadOptions uploadOptions = new BlobUploadOptions();

// Execute upload
await blobClient.UploadAsync(logStream, uploadOptions);

// Update metadata
var metadata = new Dictionary<string, string>
{ { "Status", "Processed" }
};

await blobClient.SetMetadataAsync(metadata);
}
}

If you execute this code, the write operations will fail because the active lease ID is not supplied to the operations. Which of the following changes must you implement to ensure both the upload and metadata update operations succeed under the active lease? (Select TWO options.)

  1. Assign a new `BlobRequestConditions` object to `uploadOptions.Conditions` with its `LeaseId` property set to `lease.LeaseId`.Answer
  2. Pass a new `BlobRequestConditions` object with its `LeaseId` property set to `lease.LeaseId` as the second parameter (`conditions`) in the `SetMetadataAsync` call.Answer
  3. C
    Add a key-value pair of `x-ms-lease-id` and `lease.LeaseId` directly to the `metadata` dictionary before calling `SetMetadataAsync`.
  4. D
    Set `uploadOptions.HttpHeaders` to a new `BlobHttpHeaders` instance and add a custom header named `x-ms-meta-lease-id` containing `lease.LeaseId`.

Answer

Configure both write operations using `BlobRequestConditions` with the `LeaseId` property set to the active lease ID. For the upload, assign this to `uploadOptions.Conditions`. For the metadata update, pass it as the second argument to `SetMetadataAsync`.
The correct configurations require passing the acquired lease ID inside a `BlobRequestConditions` object. For the blob upload operation, this object must be assigned to the `Conditions` property of `BlobUploadOptions`. For the metadata update operation, the request conditions must be passed as the second parameter to `SetMetadataAsync`. This ensures that both write actions are authenticated against the active lease.

Step-by-Step Solution

1
Identify the SDK class used to enforce access conditions such as leases.
The `BlobRequestConditions` class is identified as the mechanism for passing lease IDs in the Azure.Storage.Blobs SDK (v12).
Azure Storage requires lease validation via request headers which are configured using request condition parameters in the SDK.
2
Configure the upload options with the lease details.
The `Conditions` property of `BlobUploadOptions` is assigned a `BlobRequestConditions` instance containing the active lease ID.
This prevents write conflicts by ensuring the upload only succeeds if the lease is valid.
3
Configure the metadata update call with the lease details.
The `SetMetadataAsync` method is called with a `BlobRequestConditions` parameter containing the active lease ID.
Like write operations, metadata modifications on a leased blob require the lease ID to be supplied in the request conditions.

Key Concept

Configuring lease conditions for blob upload and metadata operations using the Azure.Storage.Blobs SDK
Rate this question