com.azure.storage.blob.BlobContainerAsyncClient 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;
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.HttpResponse;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.PagedResponseBase;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.ResponseBase;
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.implementation.AzureBlobStorageImpl;
import com.azure.storage.blob.implementation.AzureBlobStorageImplBuilder;
import com.azure.storage.blob.implementation.accesshelpers.BlobItemConstructorProxy;
import com.azure.storage.blob.implementation.models.BlobHierarchyListSegment;
import com.azure.storage.blob.implementation.models.ContainersGetAccountInfoHeaders;
import com.azure.storage.blob.implementation.models.ContainersGetPropertiesHeaders;
import com.azure.storage.blob.implementation.models.ContainersListBlobFlatSegmentHeaders;
import com.azure.storage.blob.implementation.models.ContainersListBlobHierarchySegmentHeaders;
import com.azure.storage.blob.implementation.models.EncryptionScope;
import com.azure.storage.blob.implementation.models.ListBlobsFlatSegmentResponse;
import com.azure.storage.blob.implementation.models.ListBlobsHierarchySegmentResponse;
import com.azure.storage.blob.implementation.util.BlobSasImplUtil;
import com.azure.storage.blob.implementation.util.ModelHelper;
import com.azure.storage.blob.models.BlobContainerAccessPolicies;
import com.azure.storage.blob.models.BlobContainerEncryptionScope;
import com.azure.storage.blob.models.BlobContainerProperties;
import com.azure.storage.blob.models.BlobItem;
import com.azure.storage.blob.models.BlobRequestConditions;
import com.azure.storage.blob.models.BlobSignedIdentifier;
import com.azure.storage.blob.models.BlobStorageException;
import com.azure.storage.blob.models.CpkInfo;
import com.azure.storage.blob.models.CustomerProvidedKey;
import com.azure.storage.blob.models.ListBlobsIncludeItem;
import com.azure.storage.blob.models.ListBlobsOptions;
import com.azure.storage.blob.models.PublicAccessType;
import com.azure.storage.blob.models.StorageAccountInfo;
import com.azure.storage.blob.models.TaggedBlobItem;
import com.azure.storage.blob.models.UserDelegationKey;
import com.azure.storage.blob.options.BlobContainerCreateOptions;
import com.azure.storage.blob.options.FindBlobsOptions;
import com.azure.storage.blob.sas.BlobServiceSasSignatureValues;
import com.azure.storage.common.StorageSharedKeyCredential;
import com.azure.storage.common.implementation.SasImplUtils;
import com.azure.storage.common.implementation.StorageImplUtils;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import static com.azure.core.util.FluxUtil.monoError;
import static com.azure.core.util.FluxUtil.pagedFluxError;
import static com.azure.core.util.FluxUtil.withContext;
/**
* Client to a container. It may only be instantiated through a {@link BlobContainerClientBuilder} or via the method
* {@link BlobServiceAsyncClient#getBlobContainerAsyncClient(String)}. This class does not hold any state about a
* particular blob but is instead a convenient way of sending off appropriate requests to the resource on the service.
* It may also be used to construct URLs to blobs.
*
*
* This client contains operations on a container. Operations on a blob are available on {@link BlobAsyncClient} through
* {@link #getBlobAsyncClient(String)}, and operations on the service are available on {@link BlobServiceAsyncClient}.
*
*
* Please refer to the Azure
* Docs for more information on containers.
*
*
* Note this client is an async client that returns reactive responses from Spring Reactor Core project
* (https://projectreactor.io/). Calling the methods in this client will NOT start the actual network
* operation, until {@code .subscribe()} is called on the reactive response. You can simply convert one of these
* responses to a {@link java.util.concurrent.CompletableFuture} object through {@link Mono#toFuture()}.
*/
@ServiceClient(builder = BlobContainerClientBuilder.class, isAsync = true)
public final class BlobContainerAsyncClient {
/**
* Special container name for the root container in the Storage account.
*/
public static final String ROOT_CONTAINER_NAME = "$root";
/**
* Special container name for the static website container in the Storage account.
*/
public static final String STATIC_WEBSITE_CONTAINER_NAME = "$web";
/**
* Special container name for the logs container in the Storage account.
*/
public static final String LOG_CONTAINER_NAME = "$logs";
private static final ClientLogger LOGGER = new ClientLogger(BlobContainerAsyncClient.class);
private final AzureBlobStorageImpl azureBlobStorage;
private final String accountName;
private final String containerName;
private final BlobServiceVersion serviceVersion;
private final CpkInfo customerProvidedKey; // only used to pass down to blob clients
private final EncryptionScope encryptionScope; // only used to pass down to blob clients
private final BlobContainerEncryptionScope blobContainerEncryptionScope;
/**
* Package-private constructor for use by {@link BlobContainerClientBuilder}.
*
* @param pipeline The pipeline used to send and receive service requests.
* @param url The endpoint where to send service requests.
* @param serviceVersion The version of the service to receive requests.
* @param accountName The storage account name.
* @param containerName The container name.
* @param customerProvidedKey Customer provided key used during encryption of the blob's data on the server, pass
* {@code null} to allow the service to use its own encryption.
* @param encryptionScope Encryption scope used during encryption of the blob's data on the server, pass
* {@code null} to allow the service to use its own encryption.
*/
BlobContainerAsyncClient(HttpPipeline pipeline, String url, BlobServiceVersion serviceVersion,
String accountName, String containerName, CpkInfo customerProvidedKey, EncryptionScope encryptionScope,
BlobContainerEncryptionScope blobContainerEncryptionScope) {
this.azureBlobStorage = new AzureBlobStorageImplBuilder()
.pipeline(pipeline)
.url(url)
.version(serviceVersion.getVersion())
.buildClient();
this.serviceVersion = serviceVersion;
this.accountName = accountName;
this.containerName = containerName;
this.customerProvidedKey = customerProvidedKey;
this.encryptionScope = encryptionScope;
this.blobContainerEncryptionScope = blobContainerEncryptionScope;
/* Check to make sure the uri is valid. We don't want the error to occur later in the generated layer
when the sas token has already been applied. */
try {
URI.create(getBlobContainerUrl());
} catch (IllegalArgumentException ex) {
throw LOGGER.logExceptionAsError(ex);
}
}
/**
* Creates a new BlobAsyncClient object by concatenating blobName to the end of ContainerAsyncClient's URL. The new
* BlobAsyncClient uses the same request policy pipeline as the ContainerAsyncClient.
*
*
Code Samples
*
*
*
* BlobAsyncClient blobAsyncClient = client.getBlobAsyncClient(blobName);
*
*
*
* @param blobName A {@code String} representing the name of the blob. If the blob name contains special characters,
* pass in the url encoded version of the blob name.
* @return A new {@link BlobAsyncClient} object which references the blob with the specified name in this container.
*/
public BlobAsyncClient getBlobAsyncClient(String blobName) {
return getBlobAsyncClient(blobName, null);
}
/**
* Creates a new BlobAsyncClient object by concatenating blobName to the end of ContainerAsyncClient's URL. The new
* BlobAsyncClient uses the same request policy pipeline as the ContainerAsyncClient.
*
* Code Samples
*
*
*
* BlobAsyncClient blobAsyncClient = client.getBlobAsyncClient(blobName, snapshot);
*
*
*
* @param blobName A {@code String} representing the name of the blob. If the blob name contains special characters,
* pass in the url encoded version of the blob name.
* @param snapshot the snapshot identifier for the blob.
* @return A new {@link BlobAsyncClient} object which references the blob with the specified name in this container.
*/
public BlobAsyncClient getBlobAsyncClient(String blobName, String snapshot) {
return new BlobAsyncClient(getHttpPipeline(), getAccountUrl(), getServiceVersion(), getAccountName(),
getBlobContainerName(), blobName, snapshot, getCustomerProvidedKey(), encryptionScope);
}
/**
* Creates a new BlobAsyncClient object by concatenating blobName to the end of ContainerAsyncClient's URL. The new
* BlobAsyncClient uses the same request policy pipeline as the ContainerAsyncClient.
*
* @param blobName A {@code String} representing the name of the blob. If the blob name contains special characters,
* pass in the url encoded version of the blob name.
* @param versionId the version identifier for the blob, pass {@code null} to interact with the latest blob version.
* @return A new {@link BlobAsyncClient} object which references the blob with the specified name in this container.
*/
public BlobAsyncClient getBlobVersionAsyncClient(String blobName, String versionId) {
return new BlobAsyncClient(getHttpPipeline(), getAccountUrl(), getServiceVersion(), getAccountName(),
getBlobContainerName(), blobName, null, getCustomerProvidedKey(), encryptionScope, versionId);
}
/**
* Get the url of the storage account.
*
* @return the URL of the storage account
*/
public String getAccountUrl() {
return azureBlobStorage.getUrl();
}
/**
* Gets the URL of the container represented by this client.
*
* @return the URL.
*/
public String getBlobContainerUrl() {
return azureBlobStorage.getUrl() + "/" + containerName;
}
/**
* Get the container name.
*
* Code Samples
*
*
*
* String containerName = client.getBlobContainerName();
* System.out.println("The name of the blob is " + containerName);
*
*
*
* @return The name of container.
*/
public String getBlobContainerName() {
return containerName;
}
/**
* Get associated account name.
*
* @return account name associated with this storage resource.
*/
public String getAccountName() {
return this.accountName;
}
/**
* Get an async client pointing to the account.
*
* @return {@link BlobServiceAsyncClient}
*/
public BlobServiceAsyncClient getServiceAsyncClient() {
return getServiceClientBuilder().buildAsyncClient();
}
BlobServiceClientBuilder getServiceClientBuilder() {
CustomerProvidedKey encryptionKey = this.customerProvidedKey == null ? null
: new CustomerProvidedKey(this.customerProvidedKey.getEncryptionKey());
return new BlobServiceClientBuilder()
.endpoint(this.getBlobContainerUrl())
.pipeline(this.getHttpPipeline())
.serviceVersion(this.serviceVersion)
.blobContainerEncryptionScope(this.blobContainerEncryptionScope)
.encryptionScope(this.getEncryptionScope())
.customerProvidedKey(encryptionKey);
}
/**
* Gets the service version the client is using.
*
* @return the service version the client is using.
*/
public BlobServiceVersion getServiceVersion() {
return serviceVersion;
}
/**
* Gets the {@link HttpPipeline} powering this client.
*
* @return The pipeline.
*/
public HttpPipeline getHttpPipeline() {
return azureBlobStorage.getHttpPipeline();
}
/**
* Gets the {@link CpkInfo} associated with this client that will be passed to {@link BlobAsyncClient
* BlobAsyncClients} when {@link #getBlobAsyncClient(String) getBlobAsyncClient} is called.
*
* @return the customer provided key used for encryption.
*/
public CpkInfo getCustomerProvidedKey() {
return customerProvidedKey;
}
/**
* Gets the {@link EncryptionScope} used to encrypt this blob's content on the server.
*
* @return the encryption scope used for encryption.
*/
public String getEncryptionScope() {
if (encryptionScope == null) {
return null;
}
return encryptionScope.getEncryptionScope();
}
/**
* Gets if the container this client represents exists in the cloud.
*
* Code Samples
*
*
*
* client.exists().subscribe(response -> System.out.printf("Exists? %b%n", response));
*
*
*
* @return true if the container exists, false if it doesn't
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono exists() {
return existsWithResponse().flatMap(FluxUtil::toMono);
}
/**
* Gets if the container this client represents exists in the cloud.
*
* Code Samples
*
*
*
* client.existsWithResponse().subscribe(response -> System.out.printf("Exists? %b%n", response.getValue()));
*
*
*
* @return true if the container exists, false if it doesn't
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> existsWithResponse() {
try {
return withContext(this::existsWithResponse);
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
/**
* Gets if the container this client represents exists in the cloud.
*
* @return true if the container exists, false if it doesn't
*/
Mono> existsWithResponse(Context context) {
return this.getPropertiesWithResponse(null, context)
.map(cp -> (Response) new SimpleResponse<>(cp, true))
.onErrorResume(t -> t instanceof BlobStorageException && ((BlobStorageException) t).getStatusCode() == 404,
t -> {
HttpResponse response = ((BlobStorageException) t).getResponse();
return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), false));
});
}
/**
* Creates a new container within a storage account. If a container with the same name already exists, the operation
* fails. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.create().subscribe(
* response -> System.out.printf("Create completed%n"),
* error -> System.out.printf("Error while creating container %s%n", error));
*
*
*
* @return A reactive response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono create() {
return createWithResponse(null, null).flatMap(FluxUtil::toMono);
}
/**
* Creates a new container within a storage account. If a container with the same name already exists, the operation
* fails. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* Map<String, String> metadata = Collections.singletonMap("metadata", "value");
* client.createWithResponse(metadata, PublicAccessType.CONTAINER).subscribe(response ->
* System.out.printf("Create completed with status %d%n", response.getStatusCode()));
*
*
*
* @param metadata Metadata to associate with the container. If there is leading or trailing whitespace in any
* metadata key or value, it must be removed or encoded.
* @param accessType Specifies how the data in this container is available to the public. See the
* x-ms-blob-public-access header in the Azure Docs for more information. Pass null for no public access.
* @return A reactive response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> createWithResponse(Map metadata, PublicAccessType accessType) {
try {
return withContext(context -> createWithResponse(metadata, accessType, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> createWithResponse(Map metadata, PublicAccessType accessType,
Context context) {
context = context == null ? Context.NONE : context;
return this.azureBlobStorage.getContainers().createWithResponseAsync(
containerName, null, metadata, accessType, null, blobContainerEncryptionScope, context)
.map(response -> new SimpleResponse<>(response, null));
}
/**
* Creates a new container within a storage account if it does not exist. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.createIfNotExists().subscribe(created -> {
* if (created) {
* System.out.println("successfully created.");
* } else {
* System.out.println("Already exists.");
* }
* });
*
*
*
* @return A reactive response signaling completion. {@code true} indicates a new container was created,
* {@code true} indicates a container already existed at this location.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono createIfNotExists() {
return createIfNotExistsWithResponse(null).flatMap(FluxUtil::toMono);
}
/**
* Creates a new container within a storage account if it does not exist. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* Map<String, String> metadata = Collections.singletonMap("metadata", "value");
* BlobContainerCreateOptions options = new BlobContainerCreateOptions().setMetadata(metadata)
* .setPublicAccessType(PublicAccessType.CONTAINER);
*
* client.createIfNotExistsWithResponse(options).subscribe(response -> {
* if (response.getStatusCode() == 409) {
* System.out.println("Already exists.");
* } else {
* System.out.println("successfully created.");
* }
* });
*
*
*
* @param options {@link BlobContainerCreateOptions}
* @return A reactive response signaling completion. If {@link Response}'s status code is 201, a new container was
* successfully created. If status code is 409, a container already existed at this location.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> createIfNotExistsWithResponse(BlobContainerCreateOptions options) {
try {
return createIfNotExistsWithResponse(options, null);
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> createIfNotExistsWithResponse(BlobContainerCreateOptions options, Context context) {
try {
options = options == null ? new BlobContainerCreateOptions() : options;
return createWithResponse(options.getMetadata(), options.getPublicAccessType(), context)
.map(response -> (Response) new SimpleResponse<>(response, true))
.onErrorResume(t -> t instanceof BlobStorageException && ((BlobStorageException) t)
.getStatusCode() == 409,
t -> {
HttpResponse response = ((BlobStorageException) t).getResponse();
return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), false));
});
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
/**
* Marks the specified container for deletion. The container and any blobs contained within it are later deleted
* during garbage collection. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.delete().subscribe(
* response -> System.out.printf("Delete completed%n"),
* error -> System.out.printf("Delete failed: %s%n", error));
*
*
*
* @return A reactive response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono delete() {
return deleteWithResponse(null).flatMap(FluxUtil::toMono);
}
/**
* Marks the specified container for deletion. The container and any blobs contained within it are later deleted
* during garbage collection. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* BlobRequestConditions requestConditions = new BlobRequestConditions()
* .setLeaseId(leaseId)
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* client.deleteWithResponse(requestConditions).subscribe(response ->
* System.out.printf("Delete completed with status %d%n", response.getStatusCode()));
*
*
*
* @param requestConditions {@link BlobRequestConditions}
* @return A reactive response signalling completion.
* @throws UnsupportedOperationException If either {@link BlobRequestConditions#getIfMatch()} or
* {@link BlobRequestConditions#getIfNoneMatch()} is set.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> deleteWithResponse(BlobRequestConditions requestConditions) {
try {
return withContext(context -> deleteWithResponse(requestConditions, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> deleteWithResponse(BlobRequestConditions requestConditions, Context context) {
requestConditions = requestConditions == null ? new BlobRequestConditions() : requestConditions;
if (!validateNoETag(requestConditions)) {
// Throwing is preferred to Mono.error because this will error out immediately instead of waiting until
// subscription.
throw LOGGER.logExceptionAsError(
new UnsupportedOperationException("ETag access conditions are not supported for this API."));
}
context = context == null ? Context.NONE : context;
return this.azureBlobStorage.getContainers().deleteWithResponseAsync(containerName, null,
requestConditions.getLeaseId(), requestConditions.getIfModifiedSince(),
requestConditions.getIfUnmodifiedSince(), null, context)
.map(response -> new SimpleResponse<>(response, null));
}
/**
* Marks the specified container for deletion if it exists. The container and any blobs contained within it are later deleted
* during garbage collection. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.deleteIfExists().subscribe(deleted -> {
* if (deleted) {
* System.out.println("Successfully deleted.");
* } else {
* System.out.println("Does not exist.");
* }
* });
*
*
*
* @return A reactive response signaling completion. {@code true} indicates the container was deleted,
* {@code false} indicates the container does not exist.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono deleteIfExists() {
return deleteIfExistsWithResponse(null).flatMap(FluxUtil::toMono);
}
/**
* Marks the specified container for deletion if it exists. The container and any blobs contained within it are
* later deleted during garbage collection. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* BlobRequestConditions requestConditions = new BlobRequestConditions()
* .setLeaseId(leaseId)
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* client.deleteIfExistsWithResponse(requestConditions).subscribe(response -> {
* if (response.getStatusCode() == 404) {
* System.out.println("Does not exist.");
* } else {
* System.out.println("successfully deleted.");
* }
* });
*
*
*
* @param requestConditions {@link BlobRequestConditions}
* @return A reactive response signaling completion. If {@link Response}'s status code is 202, the container was
* successfully deleted. If status code is 404, the container does not exist.
* @throws UnsupportedOperationException If either {@link BlobRequestConditions#getIfMatch()} or
* {@link BlobRequestConditions#getIfNoneMatch()} is set.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> deleteIfExistsWithResponse(BlobRequestConditions requestConditions) {
try {
return deleteIfExistsWithResponse(requestConditions, null);
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> deleteIfExistsWithResponse(BlobRequestConditions requestConditions, Context context) {
requestConditions = requestConditions == null ? new BlobRequestConditions() : requestConditions;
try {
return deleteWithResponse(requestConditions, context)
.map(response -> (Response) new SimpleResponse<>(response, true))
.onErrorResume(t -> t instanceof BlobStorageException && ((BlobStorageException) t).getStatusCode() == 404,
t -> {
HttpResponse response = ((BlobStorageException) t).getResponse();
return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), false));
});
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
/**
* Returns the container's metadata and system properties. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.getProperties().subscribe(response ->
* System.out.printf("Public Access Type: %s, Legal Hold? %b, Immutable? %b%n",
* response.getBlobPublicAccess(),
* response.hasLegalHold(),
* response.hasImmutabilityPolicy()));
*
*
*
* @return A {@link Mono} containing a {@link Response} whose {@link Response#getValue() value} containing the
* container properties.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono getProperties() {
return getPropertiesWithResponse(null).flatMap(FluxUtil::toMono);
}
/**
* Returns the container's metadata and system properties. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.getPropertiesWithResponse(leaseId).subscribe(response ->
* System.out.printf("Public Access Type: %s, Legal Hold? %b, Immutable? %b%n",
* response.getValue().getBlobPublicAccess(),
* response.getValue().hasLegalHold(),
* response.getValue().hasImmutabilityPolicy()));
*
*
*
* @param leaseId The lease ID the active lease on the container must match.
* @return A reactive response containing the container properties.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> getPropertiesWithResponse(String leaseId) {
try {
return withContext(context -> getPropertiesWithResponse(leaseId, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> getPropertiesWithResponse(String leaseId, Context context) {
context = context == null ? Context.NONE : context;
return this.azureBlobStorage.getContainers()
.getPropertiesWithResponseAsync(containerName, null, leaseId, null, context)
.map(rb -> {
ContainersGetPropertiesHeaders hd = rb.getDeserializedHeaders();
BlobContainerProperties properties = new BlobContainerProperties(hd.getXMsMeta(), hd.getETag(),
hd.getLastModified(), hd.getXMsLeaseDuration(), hd.getXMsLeaseState(), hd.getXMsLeaseStatus(),
hd.getXMsBlobPublicAccess(), Boolean.TRUE.equals(hd.isXMsHasImmutabilityPolicy()),
Boolean.TRUE.equals(hd.isXMsHasLegalHold()), hd.getXMsDefaultEncryptionScope(),
hd.isXMsDenyEncryptionScopeOverride(), hd.isXMsImmutableStorageWithVersioningEnabled());
return new SimpleResponse<>(rb, properties);
});
}
/**
* Sets the container's metadata. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* Map<String, String> metadata = Collections.singletonMap("metadata", "value");
* client.setMetadata(metadata).subscribe(
* response -> System.out.printf("Set metadata completed%n"),
* error -> System.out.printf("Set metadata failed: %s%n", error));
*
*
*
* @param metadata Metadata to associate with the container. If there is leading or trailing whitespace in any
* metadata key or value, it must be removed or encoded.
* @return A {@link Mono} containing a {@link Response} whose {@link Response#getValue() value} contains signalling
* completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono setMetadata(Map metadata) {
return setMetadataWithResponse(metadata, null).flatMap(FluxUtil::toMono);
}
/**
* Sets the container's metadata. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* Map<String, String> metadata = Collections.singletonMap("metadata", "value");
* BlobRequestConditions requestConditions = new BlobRequestConditions()
* .setLeaseId(leaseId)
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* client.setMetadataWithResponse(metadata, requestConditions).subscribe(response ->
* System.out.printf("Set metadata completed with status %d%n", response.getStatusCode()));
*
*
*
* @param metadata Metadata to associate with the container. If there is leading or trailing whitespace in any
* metadata key or value, it must be removed or encoded.
* @param requestConditions {@link BlobRequestConditions}
* @return A reactive response signalling completion.
* @throws UnsupportedOperationException If one of {@link BlobRequestConditions#getIfMatch()},
* {@link BlobRequestConditions#getIfNoneMatch()}, or {@link BlobRequestConditions#getIfUnmodifiedSince()} is set.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> setMetadataWithResponse(Map metadata,
BlobRequestConditions requestConditions) {
try {
return withContext(context -> setMetadataWithResponse(metadata, requestConditions, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> setMetadataWithResponse(Map metadata,
BlobRequestConditions requestConditions, Context context) {
context = context == null ? Context.NONE : context;
requestConditions = requestConditions == null ? new BlobRequestConditions() : requestConditions;
if (!validateNoETag(requestConditions) || requestConditions.getIfUnmodifiedSince() != null) {
// Throwing is preferred to Mono.error because this will error out immediately instead of waiting until
// subscription.
throw LOGGER.logExceptionAsError(new UnsupportedOperationException(
"If-Modified-Since is the only HTTP access condition supported for this API"));
}
return this.azureBlobStorage.getContainers().setMetadataWithResponseAsync(containerName, null,
requestConditions.getLeaseId(), metadata, requestConditions.getIfModifiedSince(), null, context)
.map(response -> new SimpleResponse<>(response, null));
}
/**
* Returns the container's permissions. The permissions indicate whether container's blobs may be accessed publicly.
* For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.getAccessPolicy().subscribe(response -> {
* System.out.printf("Blob Access Type: %s%n", response.getBlobAccessType());
*
* for (BlobSignedIdentifier identifier : response.getIdentifiers()) {
* System.out.printf("Identifier Name: %s, Permissions %s%n",
* identifier.getId(),
* identifier.getAccessPolicy().getPermissions());
* }
* });
*
*
*
* @return A reactive response containing the container access policy.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono getAccessPolicy() {
return getAccessPolicyWithResponse(null).flatMap(FluxUtil::toMono);
}
/**
* Returns the container's permissions. The permissions indicate whether container's blobs may be accessed publicly.
* For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.getAccessPolicyWithResponse(leaseId).subscribe(response -> {
* System.out.printf("Blob Access Type: %s%n", response.getValue().getBlobAccessType());
*
* for (BlobSignedIdentifier identifier : response.getValue().getIdentifiers()) {
* System.out.printf("Identifier Name: %s, Permissions %s%n",
* identifier.getId(),
* identifier.getAccessPolicy().getPermissions());
* }
* });
*
*
*
* @param leaseId The lease ID the active lease on the container must match.
* @return A reactive response containing the container access policy.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> getAccessPolicyWithResponse(String leaseId) {
try {
return withContext(context -> getAccessPolicyWithResponse(leaseId, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> getAccessPolicyWithResponse(String leaseId, Context context) {
context = context == null ? Context.NONE : context;
return this.azureBlobStorage.getContainers().getAccessPolicyWithResponseAsync(
containerName, null, leaseId, null, context)
.map(response -> new SimpleResponse<>(response,
new BlobContainerAccessPolicies(response.getDeserializedHeaders().getXMsBlobPublicAccess(),
response.getValue())));
}
/**
* Sets the container's permissions. The permissions indicate whether blobs in a container may be accessed publicly.
* Note that, for each signed identifier, we will truncate the start and expiry times to the nearest second to
* ensure the time formatting is compatible with the service. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* BlobSignedIdentifier identifier = new BlobSignedIdentifier()
* .setId("name")
* .setAccessPolicy(new BlobAccessPolicy()
* .setStartsOn(OffsetDateTime.now())
* .setExpiresOn(OffsetDateTime.now().plusDays(7))
* .setPermissions("permissionString"));
*
* client.setAccessPolicy(PublicAccessType.CONTAINER, Collections.singletonList(identifier)).subscribe(
* response -> System.out.printf("Set access policy completed%n"),
* error -> System.out.printf("Set access policy failed: %s%n", error));
*
*
*
* @param accessType Specifies how the data in this container is available to the public. See the
* x-ms-blob-public-access header in the Azure Docs for more information. Pass null for no public access.
* @param identifiers A list of {@link BlobSignedIdentifier} objects that specify the permissions for the container.
* Please see
* here
* for more information. Passing null will clear all access policies.
* @return A reactive response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono setAccessPolicy(PublicAccessType accessType, List identifiers) {
return setAccessPolicyWithResponse(accessType, identifiers, null).flatMap(FluxUtil::toMono);
}
/**
* Sets the container's permissions. The permissions indicate whether blobs in a container may be accessed publicly.
* Note that, for each signed identifier, we will truncate the start and expiry times to the nearest second to
* ensure the time formatting is compatible with the service. For more information, see the
* Azure Docs.
*
* Code Samples
*
*
*
* BlobSignedIdentifier identifier = new BlobSignedIdentifier()
* .setId("name")
* .setAccessPolicy(new BlobAccessPolicy()
* .setStartsOn(OffsetDateTime.now())
* .setExpiresOn(OffsetDateTime.now().plusDays(7))
* .setPermissions("permissionString"));
*
* BlobRequestConditions requestConditions = new BlobRequestConditions()
* .setLeaseId(leaseId)
* .setIfUnmodifiedSince(OffsetDateTime.now().minusDays(3));
*
* client.setAccessPolicyWithResponse(PublicAccessType.CONTAINER, Collections.singletonList(identifier), requestConditions)
* .subscribe(response ->
* System.out.printf("Set access policy completed with status %d%n", response.getStatusCode()));
*
*
*
* @param accessType Specifies how the data in this container is available to the public. See the
* x-ms-blob-public-access header in the Azure Docs for more information. Pass null for no public access.
* @param identifiers A list of {@link BlobSignedIdentifier} objects that specify the permissions for the container.
* Please see
* here
* for more information. Passing null will clear all access policies.
* @param requestConditions {@link BlobRequestConditions}
* @return A reactive response signalling completion.
* @throws UnsupportedOperationException If either {@link BlobRequestConditions#getIfMatch()} or
* {@link BlobRequestConditions#getIfNoneMatch()} is set.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> setAccessPolicyWithResponse(PublicAccessType accessType,
List identifiers, BlobRequestConditions requestConditions) {
try {
return withContext(context ->
setAccessPolicyWithResponse(accessType, identifiers, requestConditions, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> setAccessPolicyWithResponse(PublicAccessType accessType,
List identifiers, BlobRequestConditions requestConditions, Context context) {
requestConditions = requestConditions == null ? new BlobRequestConditions() : requestConditions;
if (!validateNoETag(requestConditions)) {
// Throwing is preferred to Mono.error because this will error out immediately instead of waiting until
// subscription.
throw LOGGER.logExceptionAsError(
new UnsupportedOperationException("ETag access conditions are not supported for this API."));
}
/*
We truncate to seconds because the service only supports nanoseconds or seconds, but doing an
OffsetDateTime.now will only give back milliseconds (more precise fields are zeroed and not serialized). This
allows for proper serialization with no real detriment to users as sub-second precision on active time for
signed identifiers is not really necessary.
*/
if (identifiers != null) {
for (BlobSignedIdentifier identifier : identifiers) {
if (identifier.getAccessPolicy() != null && identifier.getAccessPolicy().getStartsOn() != null) {
identifier.getAccessPolicy().setStartsOn(
identifier.getAccessPolicy().getStartsOn().truncatedTo(ChronoUnit.SECONDS));
}
if (identifier.getAccessPolicy() != null && identifier.getAccessPolicy().getExpiresOn() != null) {
identifier.getAccessPolicy().setExpiresOn(
identifier.getAccessPolicy().getExpiresOn().truncatedTo(ChronoUnit.SECONDS));
}
}
}
context = context == null ? Context.NONE : context;
return this.azureBlobStorage.getContainers().setAccessPolicyWithResponseAsync(
containerName, null, requestConditions.getLeaseId(), accessType, requestConditions.getIfModifiedSince(),
requestConditions.getIfUnmodifiedSince(), null, identifiers, context)
.map(response -> new SimpleResponse<>(response, null));
}
/**
* Returns a reactive Publisher emitting all the blobs in this container lazily as needed. The directories are
* flattened and only actual blobs and no directories are returned.
*
*
* Blob names are returned in lexicographic order. For more information, see the
* Azure Docs.
*
*
* E.g. listing a container containing a 'foo' folder, which contains blobs 'foo1' and 'foo2', and a blob on the
* root level 'bar', will return
*
*
* - foo/foo1
*
- foo/foo2
*
- bar
*
*
* Code Samples
*
*
*
* client.listBlobs().subscribe(blob ->
* System.out.printf("Name: %s, Directory? %b%n", blob.getName(), blob.isPrefix()));
*
*
*
* @return A reactive response emitting the flattened blobs.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux listBlobs() {
return this.listBlobs(new ListBlobsOptions());
}
/**
* Returns a reactive Publisher emitting all the blobs in this container lazily as needed. The directories are
* flattened and only actual blobs and no directories are returned.
*
*
* Blob names are returned in lexicographic order. For more information, see the
* Azure Docs.
*
*
* E.g. listing a container containing a 'foo' folder, which contains blobs 'foo1' and 'foo2', and a blob on the
* root level 'bar', will return
*
*
* - foo/foo1
*
- foo/foo2
*
- bar
*
*
*
*
*
*
* ListBlobsOptions options = new ListBlobsOptions()
* .setPrefix("prefixToMatch")
* .setDetails(new BlobListDetails()
* .setRetrieveDeletedBlobs(true)
* .setRetrieveSnapshots(true));
*
* client.listBlobs(options).subscribe(blob ->
* System.out.printf("Name: %s, Directory? %b, Deleted? %b, Snapshot ID: %s%n",
* blob.getName(),
* blob.isPrefix(),
* blob.isDeleted(),
* blob.getSnapshot()));
*
*
*
* @param options {@link ListBlobsOptions}
* @return A reactive response emitting the listed blobs, flattened.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux listBlobs(ListBlobsOptions options) {
return listBlobs(options, null);
}
/**
* Returns a reactive Publisher emitting all the blobs in this container lazily as needed. The directories are
* flattened and only actual blobs and no directories are returned.
*
*
* Blob names are returned in lexicographic order. For more information, see the
* Azure Docs.
*
*
* E.g. listing a container containing a 'foo' folder, which contains blobs 'foo1' and 'foo2', and a blob on the
* root level 'bar', will return
*
*
* - foo/foo1
*
- foo/foo2
*
- bar
*
*
*
*
*
*
* ListBlobsOptions options = new ListBlobsOptions()
* .setPrefix("prefixToMatch")
* .setDetails(new BlobListDetails()
* .setRetrieveDeletedBlobs(true)
* .setRetrieveSnapshots(true));
*
* String continuationToken = "continuationToken";
*
* client.listBlobs(options, continuationToken).subscribe(blob ->
* System.out.printf("Name: %s, Directory? %b, Deleted? %b, Snapshot ID: %s%n",
* blob.getName(),
* blob.isPrefix(),
* blob.isDeleted(),
* blob.getSnapshot()));
*
*
*
* @param options {@link ListBlobsOptions}
* @param continuationToken Identifies the portion of the list to be returned with the next list operation.
* @return A reactive response emitting the listed blobs, flattened.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux listBlobs(ListBlobsOptions options, String continuationToken) {
try {
return listBlobsFlatWithOptionalTimeout(options, continuationToken, null);
} catch (RuntimeException ex) {
return pagedFluxError(LOGGER, ex);
}
}
/*
* Implementation for this paged listing operation, supporting an optional timeout provided by the synchronous
* ContainerClient. Applies the given timeout to each
* Mono> backing the PagedFlux.
*
* @param options {@link ListBlobsOptions}.
* @param timeout An optional timeout to be applied to the network asynchronous operations.
* @return A reactive response emitting the listed blobs, flattened.
*/
PagedFlux listBlobsFlatWithOptionalTimeout(ListBlobsOptions options, String continuationToken,
Duration timeout) {
BiFunction>> func =
(marker, pageSize) -> {
ListBlobsOptions finalOptions;
if (pageSize != null) {
if (options == null) {
finalOptions = new ListBlobsOptions().setMaxResultsPerPage(pageSize);
} else {
finalOptions = new ListBlobsOptions()
.setMaxResultsPerPage(pageSize)
.setPrefix(options.getPrefix())
.setDetails(options.getDetails());
}
} else {
finalOptions = options;
}
return listBlobsFlatSegment(marker, finalOptions, timeout)
.map(response -> {
List value = response.getValue().getSegment() == null
? Collections.emptyList()
: response.getValue().getSegment().getBlobItems().stream()
.map(ModelHelper::populateBlobItem)
.collect(Collectors.toList());
return new PagedResponseBase<>(
response.getRequest(),
response.getStatusCode(),
response.getHeaders(),
value,
response.getValue().getNextMarker(),
response.getDeserializedHeaders());
});
};
return new PagedFlux<>(pageSize -> func.apply(continuationToken, pageSize), func);
}
/*
* Returns a single segment of blobs starting from the specified Marker. Use an empty
* marker to start enumeration from the beginning. Blob names are returned in lexicographic order.
* After getting a segment, process it, and then call ListBlobs again (passing the previously-returned
* Marker) to get the next segment. For more information, see the
* Azure Docs.
*
* @param marker
* Identifies the portion of the list to be returned with the next list operation.
* This value is returned by the response of a previous list operation as the
* ListBlobsFlatSegmentResponse.body().getNextMarker(). Set to null to list the first segment.
* @param options
* {@link ListBlobsOptions}
*
* @return Emits the successful response.
*/
private Mono>
listBlobsFlatSegment(String marker, ListBlobsOptions options, Duration timeout) {
options = options == null ? new ListBlobsOptions() : options;
ArrayList include =
options.getDetails().toList().isEmpty() ? null : options.getDetails().toList();
return StorageImplUtils.applyOptionalTimeout(
this.azureBlobStorage.getContainers().listBlobFlatSegmentWithResponseAsync(containerName,
options.getPrefix(), marker, options.getMaxResultsPerPage(), include, null, null, Context.NONE),
timeout);
}
/**
* Returns a reactive Publisher emitting all the blobs and directories (prefixes) under the given directory
* (prefix). Directories will have {@link BlobItem#isPrefix()} set to true.
*
*
* Blob names are returned in lexicographic order. For more information, see the
* Azure Docs.
*
*
* E.g. listing a container containing a 'foo' folder, which contains blobs 'foo1' and 'foo2', and a blob on the
* root level 'bar', will return the following results when prefix=null:
*
*
* - foo/ (isPrefix = true)
*
- bar (isPrefix = false)
*
*
* will return the following results when prefix="foo/":
*
*
* - foo/foo1 (isPrefix = false)
*
- foo/foo2 (isPrefix = false)
*
*
* Code Samples
*
*
*
* client.listBlobsByHierarchy("directoryName").subscribe(blob ->
* System.out.printf("Name: %s, Directory? %b%n", blob.getName(), blob.isDeleted()));
*
*
*
* @param directory The directory to list blobs underneath
* @return A reactive response emitting the prefixes and blobs.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux listBlobsByHierarchy(String directory) {
return this.listBlobsByHierarchy("/", new ListBlobsOptions().setPrefix(directory));
}
/**
* Returns a reactive Publisher emitting all the blobs and prefixes (directories) under the given prefix
* (directory). Directories will have {@link BlobItem#isPrefix()} set to true.
*
*
* Blob names are returned in lexicographic order. For more information, see the
* Azure Docs.
*
*
* E.g. listing a container containing a 'foo' folder, which contains blobs 'foo1' and 'foo2', and a blob on the
* root level 'bar', will return the following results when prefix=null:
*
*
* - foo/ (isPrefix = true)
*
- bar (isPrefix = false)
*
*
* will return the following results when prefix="foo/":
*
*
* - foo/foo1 (isPrefix = false)
*
- foo/foo2 (isPrefix = false)
*
*
* Code Samples
*
*
*
* ListBlobsOptions options = new ListBlobsOptions()
* .setPrefix("directoryName")
* .setDetails(new BlobListDetails()
* .setRetrieveDeletedBlobs(true)
* .setRetrieveSnapshots(true));
*
* client.listBlobsByHierarchy("/", options).subscribe(blob ->
* System.out.printf("Name: %s, Directory? %b, Deleted? %b, Snapshot ID: %s%n",
* blob.getName(),
* blob.isPrefix(),
* blob.isDeleted(),
* blob.getSnapshot()));
*
*
*
* @param delimiter The delimiter for blob hierarchy, "/" for hierarchy based on directories
* @param options {@link ListBlobsOptions}
* @return A reactive response emitting the prefixes and blobs.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux listBlobsByHierarchy(String delimiter, ListBlobsOptions options) {
try {
return listBlobsHierarchyWithOptionalTimeout(delimiter, options, null);
} catch (RuntimeException ex) {
return pagedFluxError(LOGGER, ex);
}
}
/*
* Implementation for this paged listing operation, supporting an optional timeout provided by the synchronous
* ContainerClient. Applies the given timeout to each
* Mono> backing the
* PagedFlux.
*
* @param delimiter The delimiter for blob hierarchy, "/" for hierarchy based on directories
* @param options {@link ListBlobsOptions}
* @param timeout An optional timeout to be applied to the network asynchronous operations.
* @return A reactive response emitting the listed blobs, flattened.
*/
PagedFlux listBlobsHierarchyWithOptionalTimeout(String delimiter, ListBlobsOptions options,
Duration timeout) {
BiFunction>> func =
(marker, pageSize) -> {
ListBlobsOptions finalOptions;
/*
If pageSize was not set in a .byPage(int) method, the page size from options will be preserved.
Otherwise, prefer the new value.
*/
if (pageSize != null) {
if (options == null) {
finalOptions = new ListBlobsOptions().setMaxResultsPerPage(pageSize);
} else {
// Note that this prefers the value passed to .byPage(int) over the value on the options
finalOptions = new ListBlobsOptions()
.setMaxResultsPerPage(pageSize)
.setPrefix(options.getPrefix())
.setDetails(options.getDetails());
}
} else {
finalOptions = options;
}
return listBlobsHierarchySegment(marker, delimiter, finalOptions, timeout)
.map(response -> {
BlobHierarchyListSegment segment = response.getValue().getSegment();
List value;
if (segment == null) {
value = Collections.emptyList();
} else {
value = new ArrayList<>(segment.getBlobItems().size() + segment.getBlobPrefixes().size());
segment.getBlobItems().forEach(item -> value.add(BlobItemConstructorProxy.create(item)));
segment.getBlobPrefixes().forEach(prefix -> value.add(new BlobItem()
.setName(ModelHelper.toBlobNameString(prefix.getName()))
.setIsPrefix(true)));
}
return new PagedResponseBase<>(
response.getRequest(),
response.getStatusCode(),
response.getHeaders(),
value,
response.getValue().getNextMarker(),
response.getDeserializedHeaders());
});
};
return new PagedFlux<>(pageSize -> func.apply(null, pageSize), func);
}
private Mono>
listBlobsHierarchySegment(String marker, String delimiter, ListBlobsOptions options, Duration timeout) {
options = options == null ? new ListBlobsOptions() : options;
if (options.getDetails().getRetrieveSnapshots()) {
throw LOGGER.logExceptionAsError(
new UnsupportedOperationException("Including snapshots in a hierarchical listing is not supported."));
}
ArrayList include =
options.getDetails().toList().isEmpty() ? null : options.getDetails().toList();
return StorageImplUtils.applyOptionalTimeout(
this.azureBlobStorage.getContainers().listBlobHierarchySegmentWithResponseAsync(containerName, delimiter,
options.getPrefix(), marker, options.getMaxResultsPerPage(), include, null, null,
Context.NONE),
timeout);
}
/**
* Returns a reactive Publisher emitting the blobs in this container whose tags match the query expression. For more
* information, including information on the query syntax, see the Azure Docs.
*
* Code Samples
*
*
*
* client.findBlobsByTags("where=tag=value").subscribe(blob -> System.out.printf("Name: %s%n", blob.getName()));
*
*
*
* @param query Filters the results to return only blobs whose tags match the specified expression.
* @return A reactive response emitting the list of blobs.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux findBlobsByTags(String query) {
try {
return this.findBlobsByTags(new FindBlobsOptions(query));
} catch (RuntimeException ex) {
return pagedFluxError(LOGGER, ex);
}
}
/**
* Returns a reactive Publisher emitting the blobs in this container whose tags match the query expression. For more
* information, including information on the query syntax, see the Azure Docs.
*
* Code Samples
*
*
*
* client.findBlobsByTags(new FindBlobsOptions("where=tag=value").setMaxResultsPerPage(10))
* .subscribe(blob -> System.out.printf("Name: %s%n", blob.getName()));
*
*
*
* @param options {@link FindBlobsOptions}
* @return A reactive response emitting the list of blobs.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux findBlobsByTags(FindBlobsOptions options) {
try {
return findBlobsByTags(options, null);
} catch (RuntimeException ex) {
return pagedFluxError(LOGGER, ex);
}
}
PagedFlux findBlobsByTags(FindBlobsOptions options, Duration timeout) {
StorageImplUtils.assertNotNull("options", options);
BiFunction>> func =
(marker, pageSize) -> withContext(context -> this.findBlobsByTags(
new FindBlobsOptions(options.getQuery()).setMaxResultsPerPage(pageSize), marker, timeout, context));
return new PagedFlux<>(pageSize -> func.apply(null, pageSize), func);
}
PagedFlux findBlobsByTags(FindBlobsOptions options, Duration timeout, Context context) {
StorageImplUtils.assertNotNull("options", options);
BiFunction>> func =
(marker, pageSize) -> {
FindBlobsOptions finalOptions;
if (pageSize != null) {
finalOptions = new FindBlobsOptions(options.getQuery())
.setMaxResultsPerPage(pageSize);
} else {
finalOptions = options;
}
return this.findBlobsByTags(finalOptions, marker, timeout, context);
};
return new PagedFlux<>(pageSize -> func.apply(null, pageSize), func);
}
private Mono> findBlobsByTags(
FindBlobsOptions options, String marker,
Duration timeout, Context context) {
StorageImplUtils.assertNotNull("options", options);
return StorageImplUtils.applyOptionalTimeout(
this.azureBlobStorage.getContainers().filterBlobsWithResponseAsync(containerName, null, null,
options.getQuery(), marker, options.getMaxResultsPerPage(), null, context), timeout)
.map(response -> {
List value = response.getValue().getBlobs() == null
? Collections.emptyList()
: response.getValue().getBlobs().stream()
.map(ModelHelper::populateTaggedBlobItem)
.collect(Collectors.toList());
return new PagedResponseBase<>(
response.getRequest(),
response.getStatusCode(),
response.getHeaders(),
value,
response.getValue().getNextMarker(),
response.getDeserializedHeaders());
});
}
/**
* Returns the sku name and account kind for the account. For more information, please see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.getAccountInfo().subscribe(response ->
* System.out.printf("Account Kind: %s, SKU: %s%n",
* response.getAccountKind(),
* response.getSkuName()));
*
*
*
* @return A reactive response containing the account info.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono getAccountInfo() {
return getAccountInfoWithResponse().flatMap(FluxUtil::toMono);
}
/**
* Returns the sku name and account kind for the account. For more information, please see the
* Azure Docs.
*
* Code Samples
*
*
*
* client.getAccountInfoWithResponse().subscribe(response ->
* System.out.printf("Account Kind: %s, SKU: %s%n",
* response.getValue().getAccountKind(),
* response.getValue().getSkuName()));
*
*
*
* @return A reactive response containing the account info.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> getAccountInfoWithResponse() {
try {
return withContext(this::getAccountInfoWithResponse);
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}
Mono> getAccountInfoWithResponse(Context context) {
context = context == null ? Context.NONE : context;
return this.azureBlobStorage.getContainers().getAccountInfoWithResponseAsync(containerName, context)
.map(rb -> {
ContainersGetAccountInfoHeaders hd = rb.getDeserializedHeaders();
return new SimpleResponse<>(rb, new StorageAccountInfo(hd.getXMsSkuName(), hd.getXMsAccountKind()));
});
}
// TODO: Reintroduce this API once service starts supporting it.
// Mono rename(String destinationContainerName) {
// return renameWithResponse(new BlobContainerRenameOptions(destinationContainerName)).flatMap(FluxUtil::toMono);
// }
// TODO: Reintroduce this API once service starts supporting it.
// Mono> renameWithResponse(BlobContainerRenameOptions options) {
// try {
// return withContext(context -> this.renameWithResponse(options, context));
// } catch (RuntimeException ex) {
// return monoError(LOGGER, ex);
// }
// }
// Mono> renameWithResponse(BlobContainerRenameOptions options, Context context) {
// BlobContainerAsyncClient destinationContainerClient = getServiceAsyncClient()
// .getBlobContainerAsyncClient(options.getDestinationContainerName());
// return destinationContainerClient.renameWithResponseHelper(this.getBlobContainerName(), options, context);
// }
// Mono> renameWithResponseHelper(String sourceContainerName,
// BlobContainerRenameOptions options, Context context) {
// StorageImplUtils.assertNotNull("options", options);
// BlobRequestConditions requestConditions = options.getRequestConditions() == null ? new BlobRequestConditions()
// : options.getRequestConditions();
// context = context == null ? Context.NONE : context;
//
// if (!validateNoETag(requestConditions) || !validateNoTime(requestConditions)
// || requestConditions.getTagsConditions() != null) {
// throw LOGGER.logExceptionAsError(new UnsupportedOperationException(
// "Lease-Id is the only HTTP access condition supported for this API"));
// }
//
// return this.azureBlobStorage.getContainers().renameWithResponseAsync(containerName,
// sourceContainerName, null, null, requestConditions.getLeaseId(),
// context)
// .map(response -> new SimpleResponse<>(response, this));
// }
/**
* Generates a user delegation SAS for the container using the specified {@link BlobServiceSasSignatureValues}.
* See {@link BlobServiceSasSignatureValues} for more information on how to construct a user delegation SAS.
*
* Code Samples
*
*
*
* OffsetDateTime myExpiryTime = OffsetDateTime.now().plusDays(1);
* BlobContainerSasPermission myPermission = new BlobContainerSasPermission().setReadPermission(true);
*
* BlobServiceSasSignatureValues myValues = new BlobServiceSasSignatureValues(expiryTime, permission)
* .setStartTime(OffsetDateTime.now());
*
* client.generateUserDelegationSas(values, userDelegationKey);
*
*
*
* @param blobServiceSasSignatureValues {@link BlobServiceSasSignatureValues}
* @param userDelegationKey A {@link UserDelegationKey} object used to sign the SAS values.
* See {@link BlobServiceAsyncClient#getUserDelegationKey(OffsetDateTime, OffsetDateTime)} for more information on
* how to get a user delegation key.
*
* @return A {@code String} representing the SAS query parameters.
*/
public String generateUserDelegationSas(BlobServiceSasSignatureValues blobServiceSasSignatureValues,
UserDelegationKey userDelegationKey) {
return generateUserDelegationSas(blobServiceSasSignatureValues, userDelegationKey, getAccountName(),
Context.NONE);
}
/**
* Generates a user delegation SAS for the container using the specified {@link BlobServiceSasSignatureValues}.
* See {@link BlobServiceSasSignatureValues} for more information on how to construct a user delegation SAS.
*
* Code Samples
*
*
*
* OffsetDateTime myExpiryTime = OffsetDateTime.now().plusDays(1);
* BlobContainerSasPermission myPermission = new BlobContainerSasPermission().setReadPermission(true);
*
* BlobServiceSasSignatureValues myValues = new BlobServiceSasSignatureValues(expiryTime, permission)
* .setStartTime(OffsetDateTime.now());
*
* client.generateUserDelegationSas(values, userDelegationKey, accountName, new Context("key", "value"));
*
*
*
* @param blobServiceSasSignatureValues {@link BlobServiceSasSignatureValues}
* @param userDelegationKey A {@link UserDelegationKey} object used to sign the SAS values.
* See {@link BlobServiceAsyncClient#getUserDelegationKey(OffsetDateTime, OffsetDateTime)} for more information on
* how to get a user delegation key.
* @param accountName The account name.
* @param context Additional context that is passed through the code when generating a SAS.
*
* @return A {@code String} representing the SAS query parameters.
*/
public String generateUserDelegationSas(BlobServiceSasSignatureValues blobServiceSasSignatureValues,
UserDelegationKey userDelegationKey, String accountName, Context context) {
return new BlobSasImplUtil(blobServiceSasSignatureValues, getBlobContainerName())
.generateUserDelegationSas(userDelegationKey, accountName, context);
}
/**
* Generates a service SAS for the container using the specified {@link BlobServiceSasSignatureValues}
* Note : The client must be authenticated via {@link StorageSharedKeyCredential}
*
See {@link BlobServiceSasSignatureValues} for more information on how to construct a service SAS.
*
* Code Samples
*
*
*
* OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(1);
* BlobContainerSasPermission permission = new BlobContainerSasPermission().setReadPermission(true);
*
* BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, permission)
* .setStartTime(OffsetDateTime.now());
*
* client.generateSas(values); // Client must be authenticated via StorageSharedKeyCredential
*
*
*
* @param blobServiceSasSignatureValues {@link BlobServiceSasSignatureValues}
*
* @return A {@code String} representing the SAS query parameters.
*/
public String generateSas(BlobServiceSasSignatureValues blobServiceSasSignatureValues) {
return generateSas(blobServiceSasSignatureValues, Context.NONE);
}
/**
* Generates a service SAS for the container using the specified {@link BlobServiceSasSignatureValues}
* Note : The client must be authenticated via {@link StorageSharedKeyCredential}
*
See {@link BlobServiceSasSignatureValues} for more information on how to construct a service SAS.
*
* Code Samples
*
*
*
* OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(1);
* BlobContainerSasPermission permission = new BlobContainerSasPermission().setReadPermission(true);
*
* BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, permission)
* .setStartTime(OffsetDateTime.now());
*
* // Client must be authenticated via StorageSharedKeyCredential
* client.generateSas(values, new Context("key", "value"));
*
*
*
* @param blobServiceSasSignatureValues {@link BlobServiceSasSignatureValues}
* @param context Additional context that is passed through the code when generating a SAS.
*
* @return A {@code String} representing the SAS query parameters.
*/
public String generateSas(BlobServiceSasSignatureValues blobServiceSasSignatureValues, Context context) {
return new BlobSasImplUtil(blobServiceSasSignatureValues, getBlobContainerName())
.generateSas(SasImplUtils.extractSharedKeyCredential(getHttpPipeline()), context);
}
private static boolean validateNoETag(BlobRequestConditions modifiedRequestConditions) {
if (modifiedRequestConditions == null) {
return true;
}
return modifiedRequestConditions.getIfMatch() == null && modifiedRequestConditions.getIfNoneMatch() == null;
}
// private boolean validateNoTime(BlobRequestConditions modifiedRequestConditions) {
// if (modifiedRequestConditions == null) {
// return true;
// }
// return modifiedRequestConditions.getIfModifiedSince() == null
// && modifiedRequestConditions.getIfUnmodifiedSince() == null;
// }
}