Question

Difficulty: MediumPerform Blob and Container Operations using Azure Storage SDKs

You are developing a C# console application using the Azure.Storage.Blobs SDK (v12). The application needs to update a blob named report.pdf. To prevent other processes from modifying the blob during the update, you must acquire a 30-second lease on the blob, upload the new content from a stream named contentStream, and then release the lease.

Which two code segments should you use to perform these operations? (Select two.)

  1. BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient();
    var response = await leaseClient.AcquireAsync(TimeSpan.FromSeconds(30));
    string leaseId = response.Value.LeaseId;
    Answer
  2. var options = new BlobUploadOptions
    {
    Conditions = new BlobRequestConditions { LeaseId = leaseId }
    };
    await blobClient.UploadAsync(contentStream, options);
    Answer
  3. C
    BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient();
    var response = await leaseClient.AcquireAsync(TimeSpan.FromSeconds(5));
    string leaseId = response.Value.LeaseId;
  4. D
    var options = new BlobUploadOptions
    {
    HttpHeaders = new BlobHttpHeaders { CacheControl = leaseId }
    };
    await blobClient.UploadAsync(contentStream, options);

Answer

To perform a lease-protected update on a blob, you must first obtain a BlobLeaseClient and call AcquireAsync with a duration between 15 and 60 seconds (such as 30 seconds), then pass the acquired LeaseId in the request Conditions property of the BlobUploadOptions when calling UploadAsync.
To safely modify a leased blob, you must acquire a lease and supply the acquired lease ID in the write request. The step of calling AcquireAsync with a duration of 30 seconds correctly obtains the lease because the duration lies within the required range of 15 to 60 seconds. The step of initializing BlobUploadOptions with BlobRequestConditions containing the LeaseId correctly authorizes the write operation during the upload call.

Step-by-Step Solution

1
Acquire the lease on the blob with a valid duration using BlobLeaseClient.
A lease is successfully acquired and a unique lease ID is returned.
Lease durations must be between 15 and 60 seconds, or -1 for infinite. A duration of 30 seconds is valid.
2
Create BlobUploadOptions and set the LeaseId inside BlobRequestConditions.
The upload request includes the lease ID as a precondition.
Writing to a leased blob requires the lease ID to be passed in the request conditions to authorize the modification.
3
Call the UploadAsync method on the BlobClient passing the content stream and the upload options.
The blob is updated with the new content, and the lease remains active until released or expired.
The Storage Service validates the lease ID and allows the write operation to succeed.

Key Concept

Performing Blob lease operations and using lease request conditions to write to leased blobs.
Rate this question