错误:(412) Blob 当前有租约,并且请求中未指定租约 ID

Kah*_*Kah 6 c# azure azure-blob-storage

我正在读取/写入 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));
Run Code Online (Sandbox Code Playgroud)

UploadTextAsync 抛出的异常是:

“远程服务器返回错误:(412)当前 blob 上有租约,并且请求中未指定租约 ID。”

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

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

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

谢谢!

Gau*_*tri 4

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

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