संपादित करें

इसके माध्यम से साझा किया गया


Create and manage blob leases with Java

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.

Prerequisites

Set up your environment

If you don't have an existing project, this section shows you how to set up a project to work with the Azure Blob Storage client library for Java. For more information, see Get started with Azure Blob Storage and Java.

To work with the code examples in this article, follow these steps to set up your project.

Note

This article uses the Maven build tool to build and run the example code. Other build tools, such as Gradle, also work with the Azure SDK for Java.

Install packages

Open the pom.xml file in your text editor. Install the packages by including the BOM file, or including a direct dependency.

Add import statements

Add the following import statements:

import com.azure.storage.blob.*;
import com.azure.storage.blob.specialized.*;

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).

Create a client object

To connect an app to Blob Storage, create an instance of BlobServiceClient.

The following example uses BlobServiceClientBuilder to build a BlobServiceClient object using DefaultAzureCredential, and shows how to create container and blob clients, if needed:

// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
        .endpoint("https://<storage-account-name>.blob.core.windows.net/")
        .credential(new DefaultAzureCredentialBuilder().build())
        .buildClient();

// If needed, you can create a BlobContainerClient object from the BlobServiceClient
BlobContainerClient containerClient = blobServiceClient
        .getBlobContainerClient("<container-name>");

// If needed, you can create a BlobClient object from the BlobContainerClient
BlobClient blobClient = containerClient
        .getBlobClient("<blob-name>");

To learn more about creating and managing client objects, see Create and manage client objects that interact with data resources.

About blob leases

A lease creates and manages a lock on a blob for write and delete operations. The lock duration can be 15 to 60 seconds, or can be infinite. A lease on a blob provides exclusive write and delete access to the blob. To write to a blob with an active lease, a client must include the active lease ID with the write request.

To learn more about lease states and when you can perform a given action on a lease, see Lease states and actions.

All container operations are permitted on a container that includes blobs with an active lease, including Delete Container. Therefore, a container may be deleted even if blobs within it have active leases. Use the Lease Container operation to control rights to delete a container.

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.

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 the following method:

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

public BlobLeaseClient acquireBlobLease(BlobClient blob) {
    // Create the lease client
    BlobLeaseClient leaseClient = new BlobLeaseClientBuilder()
            .blobClient(blob)
            .buildClient();

    // Acquire the lease - specify duration between 15 and 60 seconds, or -1 for
    // infinite duration
    String leaseID = leaseClient.acquireLease(30);
    System.out.printf("Acquired lease ID: %s%n", leaseID);

    return leaseClient;
}

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 an existing lease, use the following method:

The following example renews a lease for a blob:

public void renewBlobLease(BlobLeaseClient leaseClient) {
    leaseClient.renewLease();
}

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 by using the following method:

The following example releases the lease on a blob:

public void releaseBlobLease(BlobLeaseClient leaseClient) {
    leaseClient.releaseLease();
    System.out.println("Release lease operation completed");
}

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 by using the following method:

The following example breaks the lease on a blob:

public void breakBlobLease(BlobLeaseClient leaseClient) {
    leaseClient.breakLease();
}

Lease states and actions

The following diagram shows the five states of a lease, and the commands or events that cause lease state changes.

A diagram showing blob lease states and state change triggers.

The following table lists the five lease states, gives a brief description of each, and lists the lease actions allowed in a given state. These lease actions cause state transitions, as shown in the diagram.

Lease state Description Lease actions allowed
Available The lease is unlocked and can be acquired. acquire
Leased The lease is locked. acquire (same lease ID only), renew, change, release, and break
Expired The lease duration has expired. acquire, renew, release, and break
Breaking The lease has been broken, but the lease will continue to be locked until the break period has expired. release and break
Broken The lease has been broken, and the break period has expired. acquire, release, and break

When a lease expires, the lease ID is maintained by the Blob service until the blob is modified or leased again. A client may attempt to renew or release the lease using the expired lease ID. If this operation is successful, the client knows that the blob hasn't been changed since the lease ID was last valid. If the request fails, the client knows that the blob was modified, or the blob was leased again since the lease was last active. The client must then acquire a new lease on the blob.

If a lease expires rather than being explicitly released, a client may need to wait up to one minute before a new lease can be acquired for the blob. However, the client can renew the lease with their lease ID immediately if the blob hasn't been modified.

A lease can't be granted for a blob snapshot, since snapshots are read-only. Requesting a lease against a snapshot results in status code 400 (Bad Request).

Resources

To learn more about managing blob leases using the Azure Blob Storage client library for Java, see the following resources.

Code samples

REST API operations

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:

Client library resources

See also

  • This article is part of the Blob Storage developer guide for Java. To learn more, see the full list of developer guide articles at Build your Java app.