Renew the blob lease if blob.upload takes more than 60 seconds
-
05-07-2021 - |
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
La 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();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow