【问题标题】:Azure Blob lease expired right after renewalAzure Blob 租约在续订后立即到期
【发布时间】:2018-12-14 14:27:38
【问题描述】:

我正在使用 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


【解决方案1】:

我建议包含 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 的以下详细 documentation,这可能有助于您的实施。

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 2018-03-02
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2022-01-19
    • 2020-03-21
    相关资源
    最近更新 更多