我正在读写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传递到何处。
谁能告诉我我需要做些什么:
谢谢!
答案 0 :(得分:1)
请在Lease Id
方法的AccessCondition
参数中包含blob的UploadTextAsync
。因此,您的代码将类似于:
await blob.UploadTextAsync("Some Content", Encoding.UTF8, new AccessCondition()
{
LeaseId = leaseResult
}, new BlobRequestOptions(), new OperationContext());