| title | Create and manage blob leases with Java |
|---|---|
| titleSuffix | Azure Storage |
| description | Learn how to manage a lock on a blob in your Azure Storage account using the Java 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 | java |
| ms.custom | devx-track-java, devguide-java, devx-track-extended-java |
[!INCLUDE storage-dev-guide-selector-lease-blob]
This article shows how to create and manage blob leases using the Azure Storage client library for Java. You can use the client library to acquire, renew, release, and break blob leases.
[!INCLUDE storage-dev-guide-prereqs-java]
[!INCLUDE storage-dev-guide-project-setup-java]
Add the following import statements:
:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-devguide/blob-devguide-blobs/src/main/java/com/blobs/devguide/blobs/BlobLease.java" id="Snippet_Imports":::
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).
[!INCLUDE storage-dev-guide-create-client-java]
[!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 Java.
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 the following method:
The following example acquires a 30-second lease for a blob:
:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-devguide/blob-devguide-blobs/src/main/java/com/blobs/devguide/blobs/BlobLease.java" id="Snippet_AcquireLease":::
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 an existing lease, use the following method:
The following example renews a lease for a blob:
:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-devguide/blob-devguide-blobs/src/main/java/com/blobs/devguide/blobs/BlobLease.java" id="Snippet_RenewLease":::
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 by using the following method:
The following example releases the lease on a blob:
:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-devguide/blob-devguide-blobs/src/main/java/com/blobs/devguide/blobs/BlobLease.java" id="Snippet_ReleaseLease":::
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 by using the following method:
The following example breaks the lease on a blob:
:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-devguide/blob-devguide-blobs/src/main/java/com/blobs/devguide/blobs/BlobLease.java" id="Snippet_BreakLease":::
[!INCLUDE storage-dev-guide-blob-lease]
To learn more about managing blob leases using the Azure Blob Storage client library for Java, see the following resources.
The Azure SDK for Java contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Java paradigms. The client library methods for managing blob leases use the following REST API operation:
[!INCLUDE storage-dev-guide-resources-java]
[!INCLUDE storage-dev-guide-next-steps-java]