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 ?
首先,您需要获取对 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)
| 归档时间: |
|
| 查看次数: |
3707 次 |
| 最近记录: |