如何使用 BlobLeaseClient 租用 Azure 存储容器中的单个 blob?

Lia*_*iam 3 c# azure azure-blob-storage

与此相关的文档似乎非常混乱。看来使用现在已弃用的Microsoft.Azure.Storage.Blobnuget 包使用该CloudBlobContainer.AcquireLease方法是可能的。

更新后的BlobLeaseClient方法Acquire()指出:

Acquire(TimeSpan, RequestConditions, CancellationToken) 操作获取blob或容器的租约。

但尚不清楚如何使用它。尽管我找到了已弃用的软件包的示例,但我找不到任何这样的示例。

目前我有这个:

//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));
Run Code Online (Sandbox Code Playgroud)

但当我运行这个时,我只是得到:

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
Run Code Online (Sandbox Code Playgroud)

我也不确定这是否会租用 bob,因为我从未指定要租用哪个 blob。

有谁知道如何在这种情况下正确使用 BlobLeaseClient ?

Pet*_*ons 6

首先,您需要获取对 blob 的引用:

blobContainerClient = blobServiceClient.GetBlobContainerClient("containerName");
var blobClient = blobContainerClient.GetBlobClient("my.blob");
Run Code Online (Sandbox Code Playgroud)

然后创建一个租赁客户端并获取租赁:

var blc = blobClient.GetBlobLeaseClient();
var bl = await blc.AcquireAsync(TimeSpan.FromSeconds(30))
Run Code Online (Sandbox Code Playgroud)

  • 因此,一个重要的问题似乎是您必须[指定 15-60 秒之间的时间段,否则您会收到错误](https://github.com/Azure/azure-sdk-for-net/issues/ 15555#issuecomment-702299375)。这是有记录的,但我不知道为什么这个[帮助程序类允许您指定其他时间段(如果这是限制因素)](https://github.com/Azure/azure-sdk-for-net/issues/15555#issuecomment- 702587939)。 (4认同)
  • 我可以解决这个问题。令人沮丧的是,这条消息是如此不透明。只是想添加评论,以防其他人遇到此问题并需要此信息。 (2认同)