Implementing Azure blob leasing using .NET SDK

Implementing Azure blob leasing using .NET SDK

Do you know what do you mean when you lease an apartment ? Obviously, you know what it means. You get a key to apartment and which means you have exclusive access.

Blob leasing is the similar concept. If you start a blob lease, then it means nobody except you can modify the blob.

Let’s have a look at how to request a lease and other lease management operations.

The Lease Container Operation

The lease container operation creates and manages a lock on blob for write and delete operations. The lock duration can be 15 seconds, 60 seconds, or it can be infinite.

If you have followed one of my previous blog article to create the blob container, then you can also enable blob leasing using Azure Portal.

Lease using Azure portal

Login to Azure Portal and navigate to the storage account.

Once you click on storage account, find the option “Containers” under blob service in the left side panel as shown in the snapshot below. Then click on the far right three dots (…) menu and it will open a popup as shown in below snapshot.

Then you can click on acquire lease to request the lease. When you acquire the lease, you will be shown a new panel to notify you that you need to use lease ID with every operation/request, as the blob is leased. Also if you try to again acquire the lease, you will not be able to do so because the menu option is grayed out.

You can click on “Break” lease though, which means you are breaking the lease, you are moving out of apartment, so anybody else who wants can use it.

A closer look at Lease operations

If you refer the documentation, there are 4 different types of operations that can be called:

  • 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 a lease against the container.
  • Break, to end the lease but ensure that another client cannot acquire a new lease until the current lease period has expired.

The below diagram shows how the states transition. The blue rectangles show the API calls, while the green rectangles show the states after API call. Please note that the first “Available” node and last “Available” nodes are same, but just to understand complete lifecycle, they are shown different.

Lease Using .NET SDK

We will use .NET NuGet package Azure.Storage.Blobs (v12). Once this NuGet package reference is added to your solution, you can use the APIs to lease the container. The subsequent part of section explains the sample code which leases the container.

Prepare Blob Lease Client

The below code snippet tries to get an existing blob container of provided name. Here we are using samplecontainer name. Please make sure you have a blob container of this name existing.

Also replace {connectionString} with the actual connection string of the storage account.

On last line, blob lease client object is created which we are going to use for leasing the container.

string connectionString = "{connectionString}";
string containerName = "samplecontainer";

// BlobServiceClient which will be used to create a container client
var blobServiceClient = new BlobServiceClient(connectionString);

// Create the container and return a container client 
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

BlobLeaseClient blobLeaseClientInstance = containerClient.GetBlobLeaseClient();

Acquire

Next, you will use the blobLeaseClientInstance to acquire the lease. After acquiring the lease, you get a LeaseId, which you can use for subsequent operations on the container.

In below code, we have acquired lease only for 30 seconds. If you go to Azure portal and have a look at the container, its Lease state, it would be leased. If the timespan expires, then the lease state changes to expired.

You will get an exclusive lock to perform operations on the specified blob container. If the lease expires, you lose this lock. If the lease is broken, then also you lose the lock. Or you can releae the lease proactively and lose this exclusive lock.

TimeSpan ts = new TimeSpan(0,0, 0,30);
Response<BlobLease> blobLeaseResponse = blobLeaseClientInstance.Acquire(ts);
Console.WriteLine("Blob Lease Id:" + blobLeaseResponse.Value.LeaseId);
Console.WriteLine("Remaining Lease Time: " + blobLeaseResponse.Value.LeaseTime);
Console.WriteLine("Acquired lease, Press ENTER to continue");
Console.ReadLine();

Renew

If the lease is expired, then you can renew the lease by using the renew API. You will have to provide the existing lease Id in order to renew it.

Please note that the renew is possible if lease is expired and the lease was not obtained by any other party after its expiry.

It means that if the lease was released or broken, then you cannot renew it. Also, if the lease was expired and somebody else got the lease on the container, then you cannot renew it.

RequestConditions requestConditionsInstance = new RequestConditions();
requestConditionsInstance.IfMatch = new ETag(blobLeaseResponse.Value.LeaseId);
blobLeaseClientInstance.Renew();
Console.WriteLine("Renewed lease, Press ENTER to continue");
Console.ReadLine();

Release

If you are done with your work, you can proactively release the exclusive lock by calling the releae API.

If you go to Azure portal and have a look at the container, its Lease state, it would be Available.

Response<ReleasedObjectInfo> releaseObjectInfo = blobLeaseClientInstance.Release();
Console.WriteLine("Released lease, Press ENTER to continue");
Console.ReadLine();

Break

This is to break the lease. You lose the exclusive lock on the container. But then why release and break are two different APIs ?

They are different APIs because if you call break, you can specify the timespan called as break period. When a lease is broken, the lease break breakPeriod is allowed to elapse, during which time no lease operation except Break and Release can be performed on the blob or container. 

If you go to Azure portal and have a look at the container, its Lease state, it would be broken.

TimeSpan breakSpan = new TimeSpan(0,0,1,0);
blobLeaseClientInstance.Break(breakSpan);
Console.WriteLine("Broken lease, Press ENTER to continue");
Console.ReadLine();

Please note that there are async variants of these APIs available and you can use them depending on nature and design of your application.

I hope this article helps you to know more about the blob containers leasing. Let me know your thoughts.

Leave a Reply