Create and manage blob leases with .NET - Azure Storage (2024)

  • Article
  • .NET
  • Java
  • JavaScript
  • TypeScript
  • Python

This article shows how to create and manage blob leases using the Azure Storage client library for .NET. You can use the client library to acquire, renew, release, and break blob leases.

Prerequisites

  • This article assumes you already have a project set up to work with the Azure Blob Storage client library for .NET. To learn about setting up your project, including package installation, adding using directives, and creating an authorized client object, see Get started with Azure Blob Storage and .NET.
  • The authorization mechanism must have permissions to work with a blob lease. To learn more, see the authorization guidance for the following REST API operation:
    • Lease Blob

About blob leases

A lease 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. A lease on a blob provides exclusive write and delete access to the blob. To write to a blob with an active lease, a client must include the active lease ID with the write request.

To learn more about lease states and when you can perform a given action on a lease, see Lease states and actions.

All container operations are permitted on a container that includes blobs with an active lease, including Delete Container. Therefore, a container may be deleted even if blobs within it have active leases. Use the Lease Container operation to control rights to delete a container.

Lease operations are handled by the BlobLeaseClient class, which provides a client containing all lease operations for blobs and containers. To learn more about container leases using the client library, see Create and manage container leases with .NET.

Acquire a lease

When you acquire a blob lease, you obtain a lease ID that your code can use to operate on the blob. If the blob already has an active lease, you can only request a new lease by using the active lease ID. However, you can specify a new lease duration.

To acquire a lease, create an instance of the BlobLeaseClient class, and then use one of the following methods:

  • Acquire
  • AcquireAsync

The following example acquires a 30-second lease for a blob:

public static async Task<BlobLeaseClient> AcquireBlobLeaseAsync( BlobClient blobClient){ // Get a BlobLeaseClient object to work with a blob lease BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient(); Response<BlobLease> response = await leaseClient.AcquireAsync(duration: TimeSpan.FromSeconds(30)); // Use response.Value to get information about the blob lease return leaseClient;}

Renew a lease

You can renew a blob lease if the lease ID specified on the request matches the lease ID associated with the blob. The lease can be renewed even if it has expired, as long as the blob hasn't been modified or leased again since the expiration of that lease. When you renew a lease, the duration of the lease resets.

To renew a lease, use one of the following methods on a BlobLeaseClient instance:

  • Renew
  • RenewAsync

The following example renews a lease for a blob:

public static async Task RenewBlobLeaseAsync( BlobClient blobClient, string leaseID){ // Get a BlobLeaseClient object to work with a blob lease BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient(leaseID); await leaseClient.RenewAsync();}

Release a lease

You can release a blob lease if the lease ID specified on the request matches the lease ID associated with the blob. Releasing a lease allows another client to acquire a lease for the blob immediately after the release is complete.

You can release a lease using one of the following methods on a BlobLeaseClient instance:

  • Release
  • ReleaseAsync

The following example releases a lease on a blob:

public static async Task ReleaseBlobLeaseAsync( BlobClient blobClient, string leaseID){ // Get a BlobLeaseClient object to work with a blob lease BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient(leaseID); await leaseClient.ReleaseAsync();}

Break a lease

You can break a blob lease if the blob has an active lease. Any authorized request can break the lease; the request isn't required to specify a matching lease ID. A lease can't be renewed after it's broken, and breaking a lease prevents a new lease from being acquired for a period of time until the original lease expires or is released.

You can break a lease using one of the following methods on a BlobLeaseClient instance:

  • Break
  • BreakAsync

The following example breaks a lease on a blob:

public static async Task BreakBlobLeaseAsync( BlobClient blobClient){ // Get a BlobLeaseClient object to work with a blob lease BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient(); await leaseClient.BreakAsync();}

Lease states and actions

The following diagram shows the five states of a lease, and the commands or events that cause lease state changes.

The following table lists the five lease states, gives a brief description of each, and lists the lease actions allowed in a given state. These lease actions cause state transitions, as shown in the diagram.

Lease stateDescriptionLease actions allowed
AvailableThe lease is unlocked and can be acquired.acquire
LeasedThe lease is locked.acquire (same lease ID only), renew, change, release, and break
ExpiredThe lease duration has expired.acquire, renew, release, and break
BreakingThe lease has been broken, but the lease will continue to be locked until the break period has expired.release and break
BrokenThe lease has been broken, and the break period has expired.acquire, release, and break

When a lease expires, the lease ID is maintained by the Blob service until the blob is modified or leased again. A client may attempt to renew or release the lease using the expired lease ID. If this operation is successful, the client knows that the blob hasn't been changed since the lease ID was last valid. If the request fails, the client knows that the blob was modified, or the blob was leased again since the lease was last active. The client must then acquire a new lease on the blob.

If a lease expires rather than being explicitly released, a client may need to wait up to one minute before a new lease can be acquired for the blob. However, the client can renew the lease with their lease ID immediately if the blob hasn't been modified.

A lease can't be granted for a blob snapshot, since snapshots are read-only. Requesting a lease against a snapshot results in status code 400 (Bad Request).

Resources

To learn more about managing blob leases using the Azure Blob Storage client library for .NET, see the following resources.

REST API operations

The Azure SDK for .NET contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar .NET paradigms. The client library methods for managing blob leases use the following REST API operation:

  • Lease Blob

Code samples

Client library resources

See also

  • Managing Concurrency in Blob storage
Create and manage blob leases with .NET - Azure Storage (2024)

FAQs

How do I deploy .NET application in Azure Blob Storage? ›

Setting up
  1. Create the project. Create a . NET console app using either the . ...
  2. Install the package. To interact with Azure Blob Storage, install the Azure Blob Storage client library for . NET. ...
  3. Set up the app code. Replace the starting code in the Program.
Feb 6, 2024

How to create Azure Blob Storage in Azure? ›

Follow the instruction, step by step:
  1. In the left menu for the storage account, scroll to the Blob service section, then select Blobs.
  2. Then click on Container button.
  3. Enter a name for your new container. ...
  4. Select the level of public access to the container. ...
  5. Click OK to create the container.

How many blob lifecycle management rules can you create in an Azure storage account? ›

I would like to inform you that you can only define up to 100 rules in a policy for Azure storage Blob Lifecycle management.

How do I manage Azure blob? ›

Manage Access Policies for a blob container

Expand the storage account's Blob Containers. Select the desired blob container, and - from the context menu - select Manage Access Policies. The Access Policies dialog will list any access policies already created for the selected blob container.

Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6072

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.