| title | Create and manage container leases with JavaScript or TypeScript |
|---|---|
| titleSuffix | Azure Storage |
| description | Learn how to manage a lock on a container in your Azure Storage account using the JavaScript client library. |
| services | storage |
| author | stevenmatthew |
| ms.author | shaas |
| ms.service | azure-blob-storage |
| ms.topic | how-to |
| ms.date | 10/28/2024 |
| ms.devlang | javascript |
| ms.custom | devx-track-js, devguide-js, devx-track-ts, devguide-ts |
[!INCLUDE storage-dev-guide-selector-lease-container]
This article shows how to create and manage container leases using the Azure Storage client library for JavaScript. You can use the client library to acquire, renew, release, and break container leases.
- The examples in this article assume you already have a project set up to work with the Azure Blob Storage client library for JavaScript. To learn about setting up your project, including package installation, importing modules, and creating an authorized client object to work with data resources, see Get started with Azure Blob Storage and JavaScript.
- The authorization mechanism must have permissions to work with a container lease. To learn more, see the authorization guidance for the following REST API operation:
[!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 JavaScript.
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="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/lease-container.js" id="Snippet_AcquireContainerLease":::
:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/lease-container.ts" id="Snippet_AcquireContainerLease":::
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 expires, 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="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/lease-container.js" id="Snippet_RenewContainerLease":::
:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/lease-container.ts" id="Snippet_RenewContainerLease":::
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="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/lease-container.js" id="Snippet_ReleaseContainerLease":::
:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/lease-container.ts" id="Snippet_ReleaseContainerLease":::
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 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="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/lease-container.js" id="Snippet_BreakContainerLease":::
:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/lease-container.ts" id="Snippet_BreakContainerLease":::
[!INCLUDE storage-dev-guide-container-lease]
To learn more about managing container leases using the Azure Blob Storage client library for JavaScript, see the following resources.
- View JavaScript and TypeScript code samples from this article (GitHub)
The Azure SDK for JavaScript contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar JavaScript paradigms. The client library methods for managing container leases use the following REST API operation:
[!INCLUDE storage-dev-guide-resources-javascript]
[!INCLUDE storage-dev-guide-next-steps-javascript]