【问题标题】:error: (412) There is currently a lease on the blob and no lease ID was specified in the request错误:(412)当前在 blob 上存在租约,请求中未指定租约 ID
【发布时间】:2023-03-14 00:46:01
【问题描述】:

我正在读取/写入 blob 存储中的文件,并且遇到了 AcquireLeaseAsync 和 BreakLeaseAsync 的问题。我认为这就是我调用 AcquireLeaseAsync 的方式,但不知道为什么。

这是我的代码

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

UploadTextAsync 抛出的异常是:

“远程服务器返回错误:(412) 当前存在对 blob 的租约,请求中未指定租约 ID。”

查看UploadTextAsync 的文档,我看不到在哪里传递租约ID 以允许这样做。

谁能告诉我我需要做什么才能完成:

  1. 获取 blob 的租约,以便当前上下文只能写入它
  2. 从 blob 下载文本
  3. 在 blob 上上传文本
  4. 打破对 blob 的租约,以便另一个操作现在可以写入它

谢谢!

【问题讨论】:

    标签: c# azure azure-blob-storage


    【解决方案1】:

    请在UploadTextAsync 方法的AccessCondition 参数中包含blob 的Lease Id。所以你的代码会是这样的:

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

    【讨论】:

      猜你喜欢
      • 2022-11-05
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 2018-09-02
      • 2018-12-14
      • 2021-06-19
      相关资源
      最近更新 更多