首页 > 解决方案 > 错误:(412)当前在 blob 上存在租约,请求中未指定租约 ID

问题描述

我正在读取/写入 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#azureazure-blob-storage

解决方案


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

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

推荐阅读