Create and manage container leases with .NET

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

Prerequisites

Set up your environment

If you don't have an existing project, this section shows you how to set up a project to work with the Azure Blob Storage client library for .NET. The steps include package installation, adding using directives, and creating an authorized client object. For details, see Get started with Azure Blob Storage and .NET.

Install packages

From your project directory, install packages for the Azure Blob Storage and Azure Identity client libraries using the dotnet add package command. The Azure.Identity package is needed for passwordless connections to Azure services.

dotnet add package Azure.Storage.Blobs
dotnet add package Azure.Identity

Add using directives

Add these using directives to the top of your code file:

using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;

Some code examples in this article might require additional using directives.

Create a client object

To connect an app to Blob Storage, create an instance of BlobServiceClient. The following example shows how to create a client object using DefaultAzureCredential for authorization:

public BlobServiceClient GetBlobServiceClient(string accountName)
{
    BlobServiceClient client = new(
        new Uri($"https://{accountName}.blob.core.windows.net"),
        new DefaultAzureCredential());

    return client;
}

You can register a service client for dependency injection in a .NET app.

You can also create client objects for specific containers or blobs. To learn more about creating and managing client objects, see Create and manage client objects that interact with data resources.

Authorization

The authorization mechanism must have the necessary permissions to work with a container lease. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Lease Container (REST API).

About container leases

A lease establishes and manages a lock on a container for delete operations. The lock duration can be 15 to 60 seconds, or can be infinite. A lease on a container provides exclusive delete access to the container. A container lease only controls the ability to delete the container using the Delete Container REST API operation. To delete a container with an active lease, a client must include the active lease ID with the delete request. All other container operations succeed on a leased container without the lease ID. If you've enabled container soft delete, you can restore deleted containers.

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

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

Acquire a lease

When you acquire a container lease, you obtain a lease ID that your code can use to operate on the container. If the container 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:

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

public static async Task<BlobLeaseClient> AcquireContainerLeaseAsync(
    BlobContainerClient containerClient)
{
    // Get a BlobLeaseClient object to work with a container lease
    BlobLeaseClient leaseClient = containerClient.GetBlobLeaseClient();

    Response<BlobLease> response =
        await leaseClient.AcquireAsync(duration: TimeSpan.FromSeconds(30));

    // Use response.Value to get information about the container lease

    return leaseClient;
}

Renew a lease

You can renew a container lease if the lease ID specified on the request matches the lease ID associated with the container. The lease can be renewed even if it has expired, as long as the container hasn't been 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:

The following example renews a container lease:

public static async Task RenewContainerLeaseAsync(
    BlobContainerClient containerClient,
    string leaseID)
{
    // Get a BlobLeaseClient object to work with a container lease
    BlobLeaseClient leaseClient = containerClient.GetBlobLeaseClient(leaseID);

    await leaseClient.RenewAsync();
}

Release a lease

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

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

The following example releases a lease on a container:

public static async Task ReleaseContainerLeaseAsync(
    BlobContainerClient containerClient,
    string leaseID)
{
    // Get a BlobLeaseClient object to work with a container lease
    BlobLeaseClient leaseClient = containerClient.GetBlobLeaseClient(leaseID);

    await leaseClient.ReleaseAsync();
}

Break a lease

You can break a container lease if the container 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:

The following example breaks a lease on a container:

public static async Task BreakContainerLeaseAsync(
    BlobContainerClient containerClient)
{
    // Get a BlobLeaseClient object to work with a container lease
    BlobLeaseClient leaseClient = containerClient.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.

A diagram showing container lease states and state change triggers.

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 state Description Lease actions allowed
Available The lease is unlocked and can be acquired. acquire
Leased The lease is locked. acquire (same lease ID only), renew, change, release, and break
Expired The lease duration has expired. acquire, renew, release, and break
Breaking The lease has been broken, but the lease will continue to be locked until the break period has expired. release and break
Broken The 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 container is modified or leased again. A client may attempt to renew or release the lease using the expired lease ID. If the request fails, the client knows that the container was leased again, or the container was deleted since the lease was last active.

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 container. However, the client can renew the lease with the expired lease ID immediately.

Resources

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

Code samples

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 container leases use the following REST API operation:

Client library resources

See also

  • This article is part of the Blob Storage developer guide for .NET. To learn more, see the full list of developer guide articles at Build your .NET app.