Understanding Blob Storage - Part Nine - Implementing Blob Leasing

The Lease Blob operation establishes and manages a lock on a blob for write and delete operations. The lock duration can be 15 to 60 seconds or it can be infinite. Once you have the lease, you can update the Blob or delete the Blob without worrying about another process changing it underneath you. When a Blob is leased, other processes can still read it but any attempt to update it will fail. You can also update Blobs without taking a lease first, but you do run the chance of another process also attempting to modify it at the same time.

The Lease Blob operation can be called in one of the five modes.

  • Acquire - to request a new lease.
  • Renew - to renew an existing lease.
  • Change - to change the ID of an existing lease.
  • Release - to free the lease if it is no longer needed so that another client may immediately acquire
  • Break - to end the lease but another client cannot acquire a new lease until the current lease period has expired.

Step 1

First, we will see how to acquire a lease through the portal. Select any BlockBlob from your container. Right-clicking on it will give you an option to Acquire Lease and clicking on the same will give you Lease ID which is needed to perform any write to the blob, renew, change or release the lease. While doing it through the portal, it is having an infinite lease on the blob.


You can see the Delete button is disabled until the lease is expired or released.

Step 2

Now, let’s see how to implement the same with the C# application. Copy the following code of the main method of the application that we were working.
The code below requests a new lease on a Blob. This request will return a string containing a GUID as the Lease Id. If you’d rather, you can create your own Lease Id and pass it in with the request to acquire a lease and it will use your value as the Lease Id. This will only work if there is not currently a lease on the Blob.
  1. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnection"));  
  2. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
  3. CloudBlobContainer container = blobClient.GetContainerReference("images");  
  4. CloudBlockBlob blockBlob = container.GetBlockBlobReference("img2.jpeg");  
  5. TimeSpan ? leaseTime = TimeSpan.FromSeconds(60);  
  6. string leaseID = blockBlob.AcquireLease(leaseTime, null);  
  7. blockBlob.FetchAttributes();  
  8. Console.WriteLine(" Lease ID = {0} \n LeaseDuration = {1} \n LeaseState = {2} \n LeaseStatus = {3}", leaseID, blockBlob.Properties.LeaseDuration, blockBlob.Properties.LeaseState, blockBlob.Properties.LeaseStatus);  
  9. Console.ReadKey();  

Step 3

Run your application and you can see the status of your lease with the properties we asked for.


Step 4

This code will lease the resource for a timespan of 60 seconds. After the time period by running your application with commenting AcquireLease code and removing LeaseID from your code, you can see the lease has been released.

  1. // TimeSpan? leaseTime = TimeSpan.FromSeconds(60);    
  2. //string leaseID = blockBlob.AcquireLease(leaseTime, null);    
  3. blockBlob.FetchAttributes();  
  4. Console.WriteLine(" LeaseDuration = {0} \n LeaseState = {1} \n LeaseStatus = {2}", blockBlob.Properties.LeaseDuration, blockBlob.Properties.LeaseState, blockBlob.Properties.LeaseStatus);