Skip to content

Latest commit

 

History

History
110 lines (63 loc) · 6.28 KB

storage-blob-container-lease.md

File metadata and controls

110 lines (63 loc) · 6.28 KB
title titleSuffix description services author ms.author ms.service ms.topic ms.date ms.devlang ms.custom
Create and manage container leases with .NET
Azure Storage
Learn how to manage a lock on a container in your Azure Storage account using the .NET client library.
storage
pauljewellmsft
pauljewell
azure-blob-storage
how-to
04/10/2023
csharp
devx-track-csharp, devguide-csharp, devx-track-dotnet

Create and manage container leases with .NET

[!INCLUDE storage-dev-guide-selector-lease-container]

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.

[!INCLUDE storage-dev-guide-prereqs-dotnet]

Set up your environment

[!INCLUDE storage-dev-guide-project-setup-dotnet]

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

[!INCLUDE storage-dev-guide-about-container-lease]

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:

:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/LeaseContainer.cs" id="Snippet_AcquireContainerLease":::

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:

:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/LeaseContainer.cs" id="Snippet_RenewContainerLease":::

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:

:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/LeaseContainer.cs" id="Snippet_ReleaseContainerLease":::

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:

:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/LeaseContainer.cs" id="Snippet_BreakContainerLease":::

[!INCLUDE storage-dev-guide-container-lease]

Resources

To learn more about managing container 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 container leases use the following REST API operation:

Code samples

[!INCLUDE storage-dev-guide-resources-dotnet]

See also