续订后立即终止Azure Blob租约

时间:2018-07-06 03:23:27

标签: azure-storage-blobs

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


        }

1 个答案:

答案 0 :(得分: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的详细documentation进行评论,这可能对您的实施有所帮助。