com.azure.storage.blob.specialized.BlobLeaseAsyncClient Maven / Gradle / Ivy
Show all versions of azure-storage-blob Show documentation
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.storage.blob.specialized;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.RequestConditions;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.logging.ClientLogger;
import com.azure.storage.blob.BlobAsyncClient;
import com.azure.storage.blob.BlobContainerAsyncClient;
import com.azure.storage.blob.implementation.AzureBlobStorageImpl;
import com.azure.storage.blob.implementation.AzureBlobStorageImplBuilder;
import com.azure.storage.blob.implementation.util.ModelHelper;
import com.azure.storage.blob.models.BlobLeaseRequestConditions;
import com.azure.storage.blob.options.BlobAcquireLeaseOptions;
import com.azure.storage.blob.options.BlobBreakLeaseOptions;
import com.azure.storage.blob.options.BlobChangeLeaseOptions;
import com.azure.storage.blob.options.BlobReleaseLeaseOptions;
import com.azure.storage.blob.options.BlobRenewLeaseOptions;
import com.azure.storage.common.implementation.StorageImplUtils;
import reactor.core.publisher.Mono;
import java.net.URL;
import java.time.Duration;
import static com.azure.core.util.FluxUtil.monoError;
import static com.azure.core.util.FluxUtil.withContext;
/**
* This class provides a client that contains all the leasing operations for {@link BlobContainerAsyncClient containers}
* and {@link BlobAsyncClient blobs}. This client acts as a supplement to those clients and only handles leasing
* operations.
*
* Instantiating a BlobLeaseAsyncClient
*
*
*
* BlobLeaseAsyncClient blobLeaseAsyncClient = new BlobLeaseClientBuilder()
* .blobAsyncClient(blobAsyncClient)
* .buildAsyncClient();
*
*
*
*
*
* BlobLeaseAsyncClient blobLeaseAsyncClient = new BlobLeaseClientBuilder()
* .containerAsyncClient(blobContainerAsyncClient)
* .buildAsyncClient();
*
*
*
* View {@link BlobLeaseClientBuilder this} for additional ways to construct the client.
*
* For more information about leasing see the
* container leasing or
* blob leasing documentation.
*
* @see BlobLeaseClientBuilder
*/
@ServiceClient(builder = BlobLeaseClientBuilder.class, isAsync = true)
public final class BlobLeaseAsyncClient {
private static final ClientLogger LOGGER = new ClientLogger(BlobLeaseAsyncClient.class);
private final String containerName;
private final String blobName;
private final boolean isBlob;
private final AzureBlobStorageImpl client;
private final String accountName;
private volatile String leaseId;
BlobLeaseAsyncClient(HttpPipeline pipeline, String url, String containerName, String blobName, String leaseId,
boolean isBlob, String accountName, String serviceVersion) {
this.isBlob = isBlob;
this.leaseId = leaseId;
this.client = new AzureBlobStorageImplBuilder()
.pipeline(pipeline)
.url(url)
.version(serviceVersion)
.buildClient();
this.accountName = accountName;
this.containerName = containerName;
this.blobName = blobName;
}
/**
* Gets the {@link URL} of the lease client.
*
* The lease will either be a container or blob URL depending on which the lease client is associated.
*
* @return URL of the lease client.
*/
public String getResourceUrl() {
if (this.isBlob) {
return this.client.getUrl() + "/" + containerName + "/" + blobName;
} else {
return this.client.getUrl() + "/" + containerName;
}
}
/**
* Get the lease ID for this lease.
*
* @return the lease ID.
*/
public String getLeaseId() {
return leaseId;
}
/**
* Acquires a lease for write and delete operations. The lease duration must be between 15 and 60 seconds or -1 for
* an infinite duration.
*
* Code Samples
*
*
*
* client.acquireLease(60).subscribe(response -> System.out.printf("Lease ID is %s%n", response));
*
*
*
* @param durationInSeconds The duration of the lease between 15 and 60 seconds or -1 for an infinite duration.
* @return A reactive response containing the lease ID.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono acquireLease(int durationInSeconds) {
return acquireLeaseWithResponse(durationInSeconds, null).flatMap(FluxUtil::toMono);
}
/**
* Acquires a lease for write and delete operations. The lease duration must be between 15 and 60 seconds, or -1 for
* an infinite duration.
*
* Code Samples
*
*
*
* RequestConditions modifiedRequestConditions = new RequestConditions()
* .setIfModifiedSince(OffsetDateTime.now().minusDays(3));
*
* client.acquireLeaseWithResponse(60, modifiedRequestConditions).subscribe(response ->
* System.out.printf("Lease ID is %s%n", response.getValue()));
*
*
*
* @param durationInSeconds The duration of the lease between 15 and 60 seconds or -1 for an infinite duration.
* @param modifiedRequestConditions Standard HTTP Access conditions related to the modification of data. ETag and
* LastModifiedTime are used to construct conditions related to when the resource was changed relative to the given
* request. The request will fail if the specified condition is not satisfied.
* @return A reactive response containing the lease ID.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> acquireLeaseWithResponse(int durationInSeconds, RequestConditions modifiedRequestConditions) {
return acquireLeaseWithResponse(new BlobAcquireLeaseOptions(durationInSeconds)
.setRequestConditions(ModelHelper.populateBlobLeaseRequestConditions(modifiedRequestConditions)));
}
/**
* Acquires a lease for write and delete operations. The lease duration must be between 15 and 60 seconds, or -1 for
* an infinite duration.
*
* Code Samples
*
*
*
* BlobLeaseRequestConditions requestConditions = new BlobLeaseRequestConditions()
* .setIfModifiedSince(OffsetDateTime.now().minusDays(3));
*
* BlobAcquireLeaseOptions options = new BlobAcquireLeaseOptions(60)
* .setRequestConditions(requestConditions);
*
* client.acquireLeaseWithResponse(options).subscribe(response ->
* System.out.printf("Lease ID is %s%n", response.getValue()));
*
*
*
* @param options {@link BlobAcquireLeaseOptions}
* @return A reactive response containing the lease ID.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> acquireLeaseWithResponse(BlobAcquireLeaseOptions options) {
try {
return withContext(context -> acquireLeaseWithResponse(options, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> acquireLeaseWithResponse(BlobAcquireLeaseOptions options,
Context context) {
StorageImplUtils.assertNotNull("options", options);
BlobLeaseRequestConditions requestConditions = (options.getRequestConditions() == null)
? new BlobLeaseRequestConditions() : options.getRequestConditions();
context = context == null ? Context.NONE : context;
Mono> response;
if (this.isBlob) {
response = this.client.getBlobs().acquireLeaseWithResponseAsync(containerName, blobName, null,
options.getDuration(), this.leaseId, requestConditions.getIfModifiedSince(),
requestConditions.getIfUnmodifiedSince(), requestConditions.getIfMatch(),
requestConditions.getIfNoneMatch(), requestConditions.getTagsConditions(), null,
context)
.map(rb -> new SimpleResponse<>(rb, rb.getDeserializedHeaders().getXMsLeaseId()));
} else {
response = this.client.getContainers().acquireLeaseWithResponseAsync(containerName, null,
options.getDuration(), this.leaseId, requestConditions.getIfModifiedSince(),
requestConditions.getIfUnmodifiedSince(), null, context)
.map(rb -> new SimpleResponse<>(rb, rb.getDeserializedHeaders().getXMsLeaseId()));
}
response = response.doOnSuccess(r -> this.leaseId = r.getValue());
return response;
}
/**
* Renews the previously acquired lease.
*
* Code Samples
*
*
*
* client.renewLease().subscribe(response -> System.out.printf("Renewed lease ID is %s%n", response));
*
*
*
* @return A reactive response containing the renewed lease ID.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono renewLease() {
return renewLeaseWithResponse((RequestConditions) null).flatMap(FluxUtil::toMono);
}
/**
* Renews the previously acquired lease.
*
* Code Samples
*
*
*
* RequestConditions modifiedRequestConditions = new RequestConditions()
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* client.renewLeaseWithResponse(modifiedRequestConditions).subscribe(response ->
* System.out.printf("Renewed lease ID is %s%n", response.getValue()));
*
*
*
* @param modifiedRequestConditions Standard HTTP Access conditions related to the modification of data. ETag and
* LastModifiedTime are used to construct conditions related to when the resource was changed relative to the given
* request. The request will fail if the specified condition is not satisfied.
* @return A reactive response containing the renewed lease ID.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> renewLeaseWithResponse(RequestConditions modifiedRequestConditions) {
return renewLeaseWithResponse(new BlobRenewLeaseOptions()
.setRequestConditions(ModelHelper.populateBlobLeaseRequestConditions(modifiedRequestConditions)));
}
/**
* Renews the previously acquired lease.
*
* Code Samples
*
*
*
* BlobLeaseRequestConditions requestConditions = new BlobLeaseRequestConditions()
* .setIfModifiedSince(OffsetDateTime.now().minusDays(3));
*
* BlobRenewLeaseOptions options = new BlobRenewLeaseOptions()
* .setRequestConditions(requestConditions);
*
* client.renewLeaseWithResponse(options).subscribe(response ->
* System.out.printf("Lease ID is %s%n", response.getValue()));
*
*
*
* @param options {@link BlobRenewLeaseOptions}
* @return A reactive response containing the renewed lease ID.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> renewLeaseWithResponse(BlobRenewLeaseOptions options) {
try {
return withContext(context -> renewLeaseWithResponse(options, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> renewLeaseWithResponse(BlobRenewLeaseOptions options, Context context) {
options = (options == null) ? new BlobRenewLeaseOptions() : options;
BlobLeaseRequestConditions requestConditions = (options.getRequestConditions() == null)
? new BlobLeaseRequestConditions() : options.getRequestConditions();
context = context == null ? Context.NONE : context;
Mono> response;
if (this.isBlob) {
response = this.client.getBlobs().renewLeaseWithResponseAsync(containerName, blobName, this.leaseId, null,
requestConditions.getIfModifiedSince(), requestConditions.getIfUnmodifiedSince(),
requestConditions.getIfMatch(), requestConditions.getIfNoneMatch(),
requestConditions.getTagsConditions(), null,
context)
.map(rb -> new SimpleResponse<>(rb, rb.getDeserializedHeaders().getXMsLeaseId()));
} else {
response = this.client.getContainers().renewLeaseWithResponseAsync(containerName, this.leaseId, null,
requestConditions.getIfModifiedSince(), requestConditions.getIfUnmodifiedSince(),
null, context)
.map(rb -> new SimpleResponse<>(rb, rb.getDeserializedHeaders().getXMsLeaseId()));
}
response = response.doOnSuccess(r -> this.leaseId = r.getValue());
return response;
}
/**
* Releases the previously acquired lease.
*
* Code Samples
*
*
*
* client.releaseLease().subscribe(response -> System.out.println("Completed release lease"));
*
*
*
* @return A reactive response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono releaseLease() {
return releaseLeaseWithResponse((RequestConditions) null).flatMap(FluxUtil::toMono);
}
/**
* Releases the previously acquired lease.
*
* Code Samples
*
*
*
* RequestConditions modifiedRequestConditions = new RequestConditions()
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* client.releaseLeaseWithResponse(modifiedRequestConditions).subscribe(response ->
* System.out.printf("Release lease completed with status %d%n", response.getStatusCode()));
*
*
*
* @param modifiedRequestConditions Standard HTTP Access conditions related to the modification of data. ETag and
* LastModifiedTime are used to construct conditions related to when the resource was changed relative to the given
* request. The request will fail if the specified condition is not satisfied.
* @return A reactive response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> releaseLeaseWithResponse(RequestConditions modifiedRequestConditions) {
return releaseLeaseWithResponse(new BlobReleaseLeaseOptions()
.setRequestConditions(ModelHelper.populateBlobLeaseRequestConditions(modifiedRequestConditions)));
}
/**
* Releases the previously acquired lease.
*
* Code Samples
*
*
*
* BlobLeaseRequestConditions requestConditions = new BlobLeaseRequestConditions()
* .setIfModifiedSince(OffsetDateTime.now().minusDays(3));
*
* BlobReleaseLeaseOptions options = new BlobReleaseLeaseOptions()
* .setRequestConditions(requestConditions);
*
* client.releaseLeaseWithResponse(options).subscribe(response ->
* System.out.printf("Release lease completed with status %d%n", response.getStatusCode()));
*
*
*
* @param options {@link BlobReleaseLeaseOptions}
* @return A reactive response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> releaseLeaseWithResponse(BlobReleaseLeaseOptions options) {
try {
return withContext(context -> releaseLeaseWithResponse(options, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> releaseLeaseWithResponse(BlobReleaseLeaseOptions options, Context context) {
options = (options == null) ? new BlobReleaseLeaseOptions() : options;
BlobLeaseRequestConditions requestConditions = (options.getRequestConditions() == null)
? new BlobLeaseRequestConditions() : options.getRequestConditions();
context = context == null ? Context.NONE : context;
if (this.isBlob) {
return this.client.getBlobs().releaseLeaseWithResponseAsync(containerName, blobName, this.leaseId, null,
requestConditions.getIfModifiedSince(), requestConditions.getIfUnmodifiedSince(),
requestConditions.getIfMatch(), requestConditions.getIfNoneMatch(),
requestConditions.getTagsConditions(), null,
context)
.map(response -> new SimpleResponse<>(response, null));
} else {
return this.client.getContainers().releaseLeaseWithResponseAsync(containerName, this.leaseId, null,
requestConditions.getIfModifiedSince(), requestConditions.getIfUnmodifiedSince(),
null, context)
.map(response -> new SimpleResponse<>(response, null));
}
}
/**
* Breaks the previously acquired lease, if it exists.
*
* Code Samples
*
*
*
* client.breakLease().subscribe(response ->
* System.out.printf("The broken lease has %d seconds remaining on the lease", response));
*
*
*
* @return A reactive response containing the remaining time in the broken lease in seconds.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono breakLease() {
return breakLeaseWithResponse((Integer) null, null).flatMap(FluxUtil::toMono);
}
/**
* Breaks the previously acquired lease, if it exists.
*
* If {@code null} is passed for {@code breakPeriodInSeconds} a fixed duration lease will break after the
* remaining lease period elapses and an infinite lease will break immediately.
*
* Code Samples
*
*
*
* Integer retainLeaseInSeconds = 5;
* RequestConditions modifiedRequestConditions = new RequestConditions()
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* client.breakLeaseWithResponse(retainLeaseInSeconds, modifiedRequestConditions).subscribe(response ->
* System.out.printf("The broken lease has %d seconds remaining on the lease", response.getValue()));
*
*
*
* @param breakPeriodInSeconds An optional duration, between 0 and 60 seconds, that the lease should continue before
* it is broken. If the break period is longer than the time remaining on the lease the remaining time on the lease
* is used. A new lease will not be available before the break period has expired, but the lease may be held for
* longer than the break period.
* @param modifiedRequestConditions Standard HTTP Access conditions related to the modification of data. ETag and
* LastModifiedTime are used to construct conditions related to when the resource was changed relative to the given
* request. The request will fail if the specified condition is not satisfied.
* @return A reactive response containing the remaining time in the broken lease in seconds.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> breakLeaseWithResponse(Integer breakPeriodInSeconds,
RequestConditions modifiedRequestConditions) {
return breakLeaseWithResponse(new BlobBreakLeaseOptions()
.setBreakPeriod(breakPeriodInSeconds == null ? null : Duration.ofSeconds(breakPeriodInSeconds))
.setRequestConditions(ModelHelper.populateBlobLeaseRequestConditions(modifiedRequestConditions)));
}
/**
* Breaks the previously acquired lease, if it exists.
*
* If {@code null} is passed for {@code breakPeriodInSeconds} a fixed duration lease will break after the
* remaining lease period elapses and an infinite lease will break immediately.
*
* Code Samples
*
*
*
* Integer retainLeaseInSeconds = 5;
* BlobLeaseRequestConditions requestConditions = new BlobLeaseRequestConditions()
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* BlobBreakLeaseOptions options = new BlobBreakLeaseOptions()
* .setBreakPeriod(Duration.ofSeconds(retainLeaseInSeconds))
* .setRequestConditions(requestConditions);
*
* client.breakLeaseWithResponse(options).subscribe(response ->
* System.out.printf("The broken lease has %d seconds remaining on the lease", response.getValue()));
*
*
*
* @param options {@link BlobBreakLeaseOptions}
* @return A reactive response containing the remaining time in the broken lease in seconds.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> breakLeaseWithResponse(BlobBreakLeaseOptions options) {
try {
return withContext(context -> breakLeaseWithResponse(options, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> breakLeaseWithResponse(BlobBreakLeaseOptions options, Context context) {
options = (options == null) ? new BlobBreakLeaseOptions() : options;
BlobLeaseRequestConditions requestConditions = (options.getRequestConditions() == null)
? new BlobLeaseRequestConditions() : options.getRequestConditions();
context = context == null ? Context.NONE : context;
Integer breakPeriod = options.getBreakPeriod() == null ? null
: Math.toIntExact(options.getBreakPeriod().getSeconds());
if (this.isBlob) {
return this.client.getBlobs().breakLeaseWithResponseAsync(containerName, blobName, null,
breakPeriod, requestConditions.getIfModifiedSince(),
requestConditions.getIfUnmodifiedSince(), requestConditions.getIfMatch(),
requestConditions.getIfNoneMatch(), requestConditions.getTagsConditions(), null,
context)
.map(rb -> new SimpleResponse<>(rb, rb.getDeserializedHeaders().getXMsLeaseTime()));
} else {
return this.client.getContainers().breakLeaseWithResponseAsync(containerName, null,
breakPeriod, requestConditions.getIfModifiedSince(),
requestConditions.getIfUnmodifiedSince(), null, context)
.map(rb -> new SimpleResponse<>(rb, rb.getDeserializedHeaders().getXMsLeaseTime()));
}
}
/**
* Changes the lease ID.
*
* Code Samples
*
*
*
* client.changeLease("proposedId").subscribe(response -> System.out.printf("Changed lease ID is %s%n", response));
*
*
*
* @param proposedId A new lease ID in a valid GUID format.
* @return A reactive response containing the new lease ID.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono changeLease(String proposedId) {
return changeLeaseWithResponse(proposedId, null).flatMap(FluxUtil::toMono);
}
/**
* Changes the lease ID.
*
* Code Samples
*
*
*
* RequestConditions modifiedRequestConditions = new RequestConditions()
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* client.changeLeaseWithResponse("proposedId", modifiedRequestConditions).subscribe(response ->
* System.out.printf("Changed lease ID is %s%n", response.getValue()));
*
*
*
* @param proposedId A new lease ID in a valid GUID format.
* @param modifiedRequestConditions Standard HTTP Access conditions related to the modification of data. ETag and
* LastModifiedTime are used to construct conditions related to when the resource was changed relative to the given
* request. The request will fail if the specified condition is not satisfied.
* @return A reactive response containing the new lease ID.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> changeLeaseWithResponse(String proposedId,
RequestConditions modifiedRequestConditions) {
return changeLeaseWithResponse(new BlobChangeLeaseOptions(proposedId)
.setRequestConditions(ModelHelper.populateBlobLeaseRequestConditions(modifiedRequestConditions)));
}
/**
* Changes the lease ID.
*
* Code Samples
*
*
*
* BlobLeaseRequestConditions requestConditions = new BlobLeaseRequestConditions()
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* BlobChangeLeaseOptions options = new BlobChangeLeaseOptions("proposedId")
* .setRequestConditions(requestConditions);
*
* client.changeLeaseWithResponse(options).subscribe(response ->
* System.out.printf("Changed lease ID is %s%n", response.getValue()));
*
*
*
* @param options {@link BlobChangeLeaseOptions}
* @return A reactive response containing the new lease ID.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> changeLeaseWithResponse(BlobChangeLeaseOptions options) {
try {
return withContext(context -> changeLeaseWithResponse(options, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> changeLeaseWithResponse(BlobChangeLeaseOptions options, Context context) {
StorageImplUtils.assertNotNull("options", options);
BlobLeaseRequestConditions requestConditions = (options.getRequestConditions() == null)
? new BlobLeaseRequestConditions() : options.getRequestConditions();
context = context == null ? Context.NONE : context;
Mono> response;
if (this.isBlob) {
response = this.client.getBlobs().changeLeaseWithResponseAsync(containerName, blobName, this.leaseId,
options.getProposedId(), null, requestConditions.getIfModifiedSince(),
requestConditions.getIfUnmodifiedSince(), requestConditions.getIfMatch(),
requestConditions.getIfNoneMatch(), requestConditions.getTagsConditions(), null,
context)
.map(rb -> new SimpleResponse<>(rb, rb.getDeserializedHeaders().getXMsLeaseId()));
} else {
response = this.client.getContainers().changeLeaseWithResponseAsync(containerName, this.leaseId,
options.getProposedId(), null, requestConditions.getIfModifiedSince(),
requestConditions.getIfUnmodifiedSince(), null,
context)
.map(rb -> new SimpleResponse<>(rb, rb.getDeserializedHeaders().getXMsLeaseId()));
}
response = response.doOnSuccess(r -> this.leaseId = r.getValue());
return response;
}
/**
* Get associated account name.
*
* @return account name associated with this storage resource.
*/
public String getAccountName() {
return this.accountName;
}
}