Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I lease an individual blob in an Azure storage container using BlobLeaseClient?

The documentation around this seems very confusing. It seems that this was possible using the now deprecated Microsoft.Azure.Storage.Blob nuget package using the CloudBlobContainer.AcquireLease method.

The updated BlobLeaseClient has an Acquire() method which states:

The Acquire(TimeSpan, RequestConditions, CancellationToken) operation acquires a lease on the blob or container.

But it's not clear how this is supposed to be used. I can't find any examples of this, though I have found examples of the deprecated packages.

At the moment I have this:

//injected using AddBlobServiceClient, etc. Uses DefaultAzureCredential
private BlobContainerClient blobContainerClient;

blobContainerClient = blobServiceClient.GetBlobContainerClient("containerName");



BlobLeaseClient blc = blobContainerClient.GetBlobLeaseClient();

//throws exception here
BlobLease bl = await blc.AcquireAsync(new TimeSpan(0, 0, 30));

but when I run this I just get:

The value for one of the HTTP headers is not in the correct format.
RequestId:<guid>
Time:2020-09-30T10:28:47.8781946Z
Status: 400 (The value for one of the HTTP headers is not in the correct format.)
ErrorCode: InvalidHeaderValue

Headers:
Server: Windows-Azure-Blob/1.0,Microsoft-HTTPAPI/2.0
x-ms-request-id: <guid>
x-ms-client-request-id: <guid>
x-ms-version: 2019-12-12
x-ms-error-code: InvalidHeaderValue
Date: Wed, 30 Sep 2020 10:28:47 GMT
Content-Length: 328
Content-Type: application/xml

I'm also not sure this is going to lease the bob as at no point have I specified what blob I want to lease.

Does anyone know how to use the BlobLeaseClient correctly for this situration?

like image 367
Liam Avatar asked Nov 25 '25 09:11

Liam


1 Answers

First you need to get a reference to a blob:

blobContainerClient = blobServiceClient.GetBlobContainerClient("containerName");
var blobClient = blobContainerClient.GetBlobClient("my.blob");

then create a lease client and acquire a lease:

var blc = blobClient.GetBlobLeaseClient();
var bl = await blc.AcquireAsync(TimeSpan.FromSeconds(30))
like image 62
Peter Bons Avatar answered Nov 27 '25 23:11

Peter Bons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!