Kah
Kah

Reputation: 647

error: (412) There is currently a lease on the blob and no lease ID was specified in the request

I am working with reading / writing a file in blob storage and am running into an issue with AcquireLeaseAsync and BreakLeaseAsync. I think it is how I am calling AcquireLeaseAsync but am not sure why.

Here is my code

CloudBlockBlob blob = // get reference to blob

// I want to acquire a lease on the blob until I am done, 
//the parameter null in this case means infinite until breaking
string leaseResult = await blob.AcquireLeaseAsync(null, Guid.NewGuid().ToString());

// get content
string previousContent = await blob.DownloadTextAsync();

// do other stuff

// write new content, **but exception is thrown here**
await blob.UploadTextAsync("new content");

// break a lease on the blob as soon as possible, code never gets here
this.blob.BreakLeaseAsync(TimeSpan.FromMilliseconds(1));

The exception thrown at UploadTextAsync is:

"The remote server returned an error: (412) There is currently a lease on the blob and no lease ID was specified in the request.."

Looking at the documentation for UploadTextAsync, I can't see where to pass the lease ID to allow this.

Can anyone tell me what I need to do to accomplish:

  1. acquiring a lease on the blob so the current context can only write to it
  2. downloading text from the blob
  3. uploading text on the blob
  4. breaking the lease on the blob so another operation can now write to it

Thanks!

Upvotes: 6

Views: 4321

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136146

Please include Lease Id of the blob in the AccessCondition parameter of UploadTextAsync method. So your code would be something like:

        await blob.UploadTextAsync("Some Content", Encoding.UTF8, new AccessCondition()
        {
            LeaseId = leaseResult
        }, new BlobRequestOptions(), new OperationContext());

Upvotes: 4

Related Questions