|
1 | 1 | ---
|
2 |
| -title: Create and manage blob or container leases with .NET |
| 2 | +title: Create and manage container leases with .NET |
3 | 3 | titleSuffix: Azure Storage
|
4 |
| -description: Learn how to manage a lock on a blob or container in your Azure Storage account using the .NET client library. |
| 4 | +description: Learn how to manage a lock on a container in your Azure Storage account using the .NET client library. |
5 | 5 | services: storage
|
6 | 6 | author: pauljewellmsft
|
7 | 7 | ms.author: pauljewell
|
8 | 8 |
|
9 | 9 | ms.service: storage
|
10 | 10 | ms.topic: how-to
|
11 |
| -ms.date: 03/28/2022 |
| 11 | +ms.date: 04/10/2023 |
12 | 12 | ms.subservice: blobs
|
13 | 13 | ms.devlang: csharp
|
14 | 14 | ms.custom: devx-track-csharp, devguide-csharp
|
15 | 15 | ---
|
16 | 16 |
|
17 |
| -# Create and manage blob or container leases with .NET |
| 17 | +# Create and manage container leases with .NET |
18 | 18 |
|
19 |
| -A lease establishes and manages a lock on a container or the blobs in a container. You can use the .NET client library to acquire, renew, release and break leases. To learn more about leasing blobs or containers, see [Lease Container](/rest/api/storageservices/lease-container) or [Lease Blobs](/rest/api/storageservices/lease-blob). |
| 19 | +This article shows how to create and manage container leases using the [Azure Storage client library for .NET](/dotnet/api/overview/azure/storage). You can use the .NET client library to acquire, renew, release, and break container leases. |
| 20 | + |
| 21 | +## About container leases |
| 22 | + |
| 23 | +[!INCLUDE [storage-dev-guide-about-container-lease](../../../includes/storage-dev-guides/storage-dev-guide-about-container-lease.md)] |
| 24 | + |
| 25 | +To learn more about container leases using the client library, see [Create and manage blob leases with .NET](storage-blob-lease.md). |
20 | 26 |
|
21 | 27 | ## Acquire a lease
|
22 | 28 |
|
23 |
| -When you acquire a lease, you'll obtain a lease ID that your code can use to operate on the blob or container. To acquire a lease, create an instance of the [BlobLeaseClient](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient) class, and then use either of these methods: |
| 29 | +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](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient) class, and then use one of the following methods: |
24 | 30 |
|
25 | 31 | - [Acquire](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient.acquire)
|
26 | 32 | - [AcquireAsync](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient.acquireasync)
|
27 | 33 |
|
28 |
| -The following example acquires a 30 second lease for a container. |
29 |
| - |
30 |
| -```csharp |
31 |
| -public static async Task AcquireLease(BlobContainerClient containerClient) |
32 |
| -{ |
33 |
| - BlobLeaseClient blobLeaseClient = containerClient.GetBlobLeaseClient(); |
| 34 | +The following example acquires a 30-second lease for a container: |
34 | 35 |
|
35 |
| - TimeSpan ts = new TimeSpan(0, 0, 0, 30); |
36 |
| - Response<BlobLease> blobLeaseResponse = await blobLeaseClient.AcquireAsync(ts); |
37 |
| - |
38 |
| - Console.WriteLine("Blob Lease Id:" + blobLeaseResponse.Value.LeaseId); |
39 |
| - Console.WriteLine("Remaining Lease Time: " + blobLeaseResponse.Value.LeaseTime); |
40 |
| -} |
41 |
| -``` |
| 36 | +:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/LeaseContainer.cs" id="Snippet_AcquireContainerLease"::: |
42 | 37 |
|
43 | 38 | ## Renew a lease
|
44 | 39 |
|
45 |
| -If your lease expires, you can renew it. To renew a lease, use either of the following methods of the [BlobLeaseClient](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient) class: |
| 40 | +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. |
| 41 | + |
| 42 | +To renew a lease, use one of the following methods on a [BlobLeaseClient](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient) instance: |
46 | 43 |
|
47 | 44 | - [Renew](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient.renew)
|
48 | 45 | - [RenewAsync](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient.renewasync)
|
49 | 46 |
|
50 |
| -Specify the lease ID by setting the [IfMatch](/dotnet/api/azure.matchconditions.ifmatch) property of a [RequestConditions](/dotnet/api/azure.requestconditions) instance. |
| 47 | +You can specify the lease ID by setting the [IfMatch](/dotnet/api/azure.matchconditions.ifmatch) property of a [RequestConditions](/dotnet/api/azure.requestconditions) instance. |
51 | 48 |
|
52 |
| -The following example renews a lease for a blob. |
| 49 | +The following example renews a container lease: |
53 | 50 |
|
54 |
| -```csharp |
55 |
| -public static async Task RenewLease(BlobClient blobClient, string leaseID) |
56 |
| -{ |
57 |
| - BlobLeaseClient blobLeaseClient = blobClient.GetBlobLeaseClient(); |
58 |
| - RequestConditions requestConditions = new RequestConditions(); |
59 |
| - requestConditions.IfMatch = new ETag(leaseID); |
60 |
| - await blobLeaseClient.RenewAsync(); |
61 |
| -} |
62 |
| -``` |
| 51 | +:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/LeaseContainer.cs" id="Snippet_RenewContainerLease"::: |
63 | 52 |
|
64 | 53 | ## Release a lease
|
65 | 54 |
|
66 |
| -You can either wait for a lease to expire or explicitly release it. When you release a lease, other clients can obtain a lease. You can release a lease by using either of these methods of the [BlobLeaseClient](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient) class. |
| 55 | +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. |
| 56 | + |
| 57 | +You can release a lease using one of the following methods on a [BlobLeaseClient](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient) instance: |
67 | 58 |
|
68 | 59 | - [Release](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient.release)
|
69 | 60 | - [ReleaseAsync](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient.releaseasync)
|
70 | 61 |
|
71 |
| -The following example releases the lease on a container. |
| 62 | +The following example releases a lease on a container: |
72 | 63 |
|
73 |
| -```csharp |
74 |
| -public static async Task ReleaseLease(BlobContainerClient containerClient) |
75 |
| -{ |
76 |
| - BlobLeaseClient blobLeaseClient = containerClient.GetBlobLeaseClient(); |
77 |
| - await blobLeaseClient.ReleaseAsync(); |
78 |
| -} |
79 |
| -``` |
| 64 | +:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/LeaseContainer.cs" id="Snippet_ReleaseContainerLease"::: |
80 | 65 |
|
81 | 66 | ## Break a lease
|
82 | 67 |
|
83 |
| -When you break a lease, the lease ends, but other clients can't acquire a lease until the lease period expires. You can break a lease by using either of these methods: |
| 68 | +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. |
| 69 | + |
| 70 | +You can break a lease using one of the following methods on a [BlobLeaseClient](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient) instance: |
84 | 71 |
|
85 | 72 | - [Break](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient.break)
|
86 | 73 | - [BreakAsync](/dotnet/api/azure.storage.blobs.specialized.blobleaseclient.breakasync);
|
87 | 74 |
|
88 |
| -The following example breaks the lease on a blob. |
| 75 | +The following example breaks a lease on a container: |
89 | 76 |
|
90 |
| -```csharp |
91 |
| -public static async Task BreakLease(BlobClient blobClient) |
92 |
| -{ |
93 |
| - BlobLeaseClient blobLeaseClient = blobClient.GetBlobLeaseClient(); |
94 |
| - await blobLeaseClient.BreakAsync(); |
95 |
| -} |
96 |
| -``` |
| 77 | +:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/LeaseContainer.cs" id="Snippet_BreakContainerLease"::: |
97 | 78 |
|
98 | 79 | ## See also
|
99 | 80 |
|
|
0 commit comments