azure-blob-storage - Azure Blob 租约在续订后立即到期
问题描述
我正在使用 Azure Blob,代码一直在工作,但现在不行。我收到错误 412,说明包含租约 ID,但租约已过期。我在上传文件之前更新了租约,所以我不确定它为什么会过期。该文件大小为 3-4 MB。可能是什么原因造成的,我该如何解决。
using (var memoryStream = new System.IO.MemoryStream())
{
AccessCondition acc = new AccessCondition();
acc.LeaseId = blobLease;
blockBlob.RenewLease(acc);
dataSet1.WriteXml(memoryStream);
string newDataHash = GetHash(memoryStream);
System.Diagnostics.Debug.Print("Old Data Hash {0}",originalDataHash);
System.Diagnostics.Debug.Print("New Data Hash {0}",newDataHash);
if (newDataHash != originalDataHash)
{
memoryStream.Position = 0;
try
{
blockBlob.UploadFromStream(memoryStream, acc);
}
catch (StorageException ex)
{
System.Diagnostics.Debug.Print(ex.Message);
count = 15;
timer1_Tick(this, e);
}
originalDataHash = GetHash(memoryStream);
}
else
{
//if data hasn't changed in 15 minutes let lease expire so someone else can use the blob, try to obtain new lease when the user comes back
if (count >= 15)
{
count = 0;
timer1.Stop();
System.Diagnostics.Debug.Print("Attempting to renew Expired Lease");
awaitNewLease(this, e);
}
}
count = 0;
}
解决方案
我建议包括 FetchAttributes 方法来获取有关租约的详细信息,这些应该让您知道问题所在:
//cloudBlockBlob is a reference to a CloudBlockBlob
TimeSpan? leaseTime = TimeSpan.FromSeconds(15);
string leaseID = cloudBlockBlob.AcquireLease(leaseTime, null);
//wait until the lease has expired before continuing
AccessCondition acc = new AccessCondition();
acc.LeaseId = leaseID;
cloudBlockBlob.RenewLease(acc);
cloudBlockBlob.FetchAttributes();
System.Diagnostics.Debug.Print("LeaseDuration = {0}", cloudBlockBlob.Properties.LeaseDuration);
System.Diagnostics.Debug.Print("LeaseState = {0}", cloudBlockBlob.Properties.LeaseState);
System.Diagnostics.Debug.Print("LeaseStatus = {0}", cloudBlockBlob.Properties.LeaseStatus);
我还建议查看以下有关租赁 blob 的详细文档,这可能有助于您的实施。
推荐阅读
- excel - 按名称查找列并将其转换为 vba
- object-detection - 尝试生成 TFrecord 但我收到此错误“FileNotFoundError: [Errno 2] No such file or directory”
- flutter - Flutter:容器的动画边框颜色
- c# - 将编译时间字符串传递给类
- reactjs - 测试输入文本更改事件
- mysql - 如何从mysql表中的每个id中选择第一行?
- javascript - 在 React (Hooks) 中达到新的状态
- amazon-web-services - AWS API 网关和 S3
- css - 制作视频元素的固定高度
- python - keras, sparse_categorical_crossentropy 标签 Y 维度和取值范围