Skip to content

Latest commit

 

History

History
113 lines (65 loc) · 6.42 KB

File metadata and controls

113 lines (65 loc) · 6.42 KB
title Create and manage blob leases with .NET
titleSuffix Azure Storage
description Learn how to manage a lock on a blob in your Azure Storage account using the .NET client library.
services storage
author stevenmatthew
ms.author shaas
ms.service azure-blob-storage
ms.topic how-to
ms.date 08/05/2024
ms.devlang csharp
ms.custom devx-track-csharp, devguide-csharp, devx-track-dotnet

Create and manage blob leases with .NET

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

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.

[!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 blob 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 Blob (REST API).

About blob leases

[!INCLUDE storage-dev-guide-about-blob-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 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:

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

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

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:

The following example renews a lease for a blob:

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

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:

The following example releases a lease on a blob:

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

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:

The following example breaks a lease on a blob:

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

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

Resources

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

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

See also

[!INCLUDE storage-dev-guide-next-steps-dotnet]