Author : MD TAREQ HASSAN | Updated : 2020/11/12

What is leasing?

Lease and update a blob

Notes:

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);
}