简体   繁体   中英

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?

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM