Question

I know that we can acquire a lease on a blob for 60 seconds or infinite.For the following code:

  var account = CloudStorageAccount.DevelopmentStorageAccount;
  var blob = account.CreateCloudBlobClient().GetBlobReference("container/blob");
  var leaseId = blob.AcquireLease();
  blob.UploadByteArray(bytes);
  blob.ReleaseLease(leaseId);

If uploadBtyeArray time varies depending on the file size.How do i keep renewing the lease until upload is completed.

Regards, Vivek

Was it helpful?

Solution

After calling AcquireLease you can spin up a new thread that constantly renews the lease just before it expires. Once the upload is complete, you should simply abort the thread which does the auto-renew.

You might want to look at the WazStorageExtensions by Steve Marx, which comes with an AutoRenewLease class (look at the DoOnce method). Internally, the AutoRenewLease class uses such a thread to a keep the lease:

            renewalThread = new Thread(() =>
            {
                while (true)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(40));
                    blob.RenewLease(leaseId);
                }
            });
            renewalThread.Start();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top