Author : MD TAREQ HASSAN | Updated : 2020/11/12
What is leasing?
- The Lease Blob operation creates and manages a lock on a blob for write and delete operations
- The lock duration can be 15 to 60 seconds, or can be infinite
- When you lease a blob, you acquire an exclusive lock on that blob
- As long as the blob is leased, no one other than lease holder can modify or delete the blob
- Leasing it just a mechanism which provides an exclusive lock to the blob storage
Lease and update a blob
Notes:
- Nuget package and API might be changed
- Check microsoft docs for latest updates and implement accordingly
Infinite lease
CloudBlobContainer cbc = Client.GetContainerReference("test-append-blob-container");
await cbc.CreateIfNotExistsAsync();
CloudAppendBlob blob = cbc.GetAppendBlobReference("test-append-blob-withlease2.data");
var blobExists = await blob.ExistsAsync();
if (!blobExists)
await blob.CreateOrReplaceAsync();
string data = string.Empty.PadLeft(64 * 1024, '*');
string leaseIdGuid = Guid.NewGuid().ToString();
var oc = new OperationContext();
var ac = new AccessCondition();
var leaseID = await blob.AcquireLeaseAsync(null, leaseIdGuid, ac, null, oc);
ac.LeaseId = leaseID;
try
{
for (int ix = 0; ix < 10; ix++)
{
await blob.AppendTextAsync(data, null, ac, null, oc);
}
}
finally
{
await blob.ReleaseLeaseAsync(ac, null, oc);
}