You are developing a C# service that periodically updates a shared configuration file stored as a block blob in Azure Blob Storage. To prevent concurrent writes, another process has acquired an active lease on the blob, and the lease ID is stored in a string variable named `currentLeaseId`. Which of the following code segments must you use to successfully overwrite the blob with new data while respecting the active lease?
- var options = new BlobUploadOptions
{
Conditions = new BlobRequestConditions { LeaseId = currentLeaseId }
};
await blobClient.UploadAsync(dataStream, options);Answer - Bvar accessCondition = AccessCondition.GenerateLeaseCondition(currentLeaseId);
await blobClient.UploadAsync(dataStream, accessCondition); - Cawait blobClient.UploadAsync(dataStream, currentLeaseId);
- Dvar options = new BlobUploadOptions
{
HttpHeaders = new BlobHttpHeaders { LeaseId = currentLeaseId }
};
await blobClient.UploadAsync(dataStream, options);
Answer
Use BlobUploadOptions with its Conditions property set to a new BlobRequestConditions object containing the active LeaseId, and pass it to BlobClient.UploadAsync.
To perform operations on a leased blob, you must provide the active lease ID as part of the request conditions. In the modern Azure.Storage.Blobs SDK (v12) for C#, this is achieved by creating a new `BlobUploadOptions` object, initializing its `Conditions` property with a `BlobRequestConditions` instance, and setting the `LeaseId` property of that instance to the active lease ID. This options object is then passed as the second parameter to `BlobClient.UploadAsync`.
Step-by-Step Solution
Key Concept
Handling active leases when performing write operations on block blobs using the Azure.Storage.Blobs SDK in C#.
Estimated Time:1m 30s