首页 > 解决方案 > 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;


        }

标签: azure-blob-storage

解决方案


我建议包括 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 的详细文档,这可能有助于您的实施。


推荐阅读