BlobServiceAsyncClient Class

  • java.lang.Object
    • com.azure.storage.blob.BlobServiceAsyncClient

public final class BlobServiceAsyncClient

Client to a storage account. It may only be instantiated through a BlobServiceClientBuilder. This class does not hold any state about a particular storage account 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 and containers.

This client contains operations on a blob. Operations on a container are available on BlobContainerAsyncClient through getBlobContainerAsyncClient(String containerName), and operations on a blob are available on BlobAsyncClient.

Please see 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 .subscribe() is called on the reactive response. You can simply convert one of these responses to a CompletableFuture object through Mono#toFuture().

Method Summary

Modifier and Type Method and Description
Mono<BlobContainerAsyncClient> createBlobContainer(String containerName)

Creates a new container within a storage account.

Mono<BlobContainerAsyncClient> createBlobContainerIfNotExists(String containerName)

Creates a new container within a storage account if it does not exist.

Mono<Response<BlobContainerAsyncClient>> createBlobContainerIfNotExistsWithResponse(String containerName, BlobContainerCreateOptions options)

Creates a new container within a storage account if it does not exist.

Mono<Response<BlobContainerAsyncClient>> createBlobContainerWithResponse(String containerName, Map<String,String> metadata, PublicAccessType accessType)

Creates a new container within a storage account.

Mono<Void> deleteBlobContainer(String containerName)

Deletes the specified container in the storage account.

Mono<Boolean> deleteBlobContainerIfExists(String containerName)

Deletes the specified container in the storage account if it exists.

Mono<Response<Boolean>> deleteBlobContainerIfExistsWithResponse(String containerName)

Deletes the specified container in the storage account if it exists.

Mono<Response<Void>> deleteBlobContainerWithResponse(String containerName)

Deletes the specified container in the storage account.

PagedFlux<TaggedBlobItem> findBlobsByTags(FindBlobsOptions options)

Returns a reactive Publisher emitting the blobs in this account whose tags match the query expression.

PagedFlux<TaggedBlobItem> findBlobsByTags(String query)

Returns a reactive Publisher emitting the blobs in this account whose tags match the query expression.

String generateAccountSas(AccountSasSignatureValues accountSasSignatureValues)

Generates an account SAS for the Azure Storage account using the specified AccountSasSignatureValues.

String generateAccountSas(AccountSasSignatureValues accountSasSignatureValues, Context context)

Generates an account SAS for the Azure Storage account using the specified AccountSasSignatureValues.

Mono<StorageAccountInfo> getAccountInfo()

Returns the sku name and account kind for the account.

Mono<Response<StorageAccountInfo>> getAccountInfoWithResponse()

Returns the sku name and account kind for the account.

String getAccountName()

Get associated account name.

String getAccountUrl()

Gets the URL of the storage account represented by this client.

BlobContainerAsyncClient getBlobContainerAsyncClient(String containerName)

Initializes a BlobContainerAsyncClient object pointing to the specified container.

HttpPipeline getHttpPipeline()

Gets the HttpPipeline powering this client.

Mono<BlobServiceProperties> getProperties()

Gets the properties of a storage account\u2019s Blob service.

Mono<Response<BlobServiceProperties>> getPropertiesWithResponse()

Gets the properties of a storage account\u2019s Blob service.

BlobServiceVersion getServiceVersion()

Gets the service version the client is using.

Mono<BlobServiceStatistics> getStatistics()

Retrieves statistics related to replication for the Blob service.

Mono<Response<BlobServiceStatistics>> getStatisticsWithResponse()

Retrieves statistics related to replication for the Blob service.

Mono<UserDelegationKey> getUserDelegationKey(OffsetDateTime start, OffsetDateTime expiry)

Gets a user delegation key for use with this account's blob storage.

Mono<Response<UserDelegationKey>> getUserDelegationKeyWithResponse(OffsetDateTime start, OffsetDateTime expiry)

Gets a user delegation key for use with this account's blob storage.

PagedFlux<BlobContainerItem> listBlobContainers()

Returns a reactive Publisher emitting all the containers in this account lazily as needed.

PagedFlux<BlobContainerItem> listBlobContainers(ListBlobContainersOptions options)

Returns a reactive Publisher emitting all the containers in this account lazily as needed.

Mono<Void> setProperties(BlobServiceProperties properties)

Sets properties for a storage account's Blob service endpoint.

Mono<Response<Void>> setPropertiesWithResponse(BlobServiceProperties properties)

Sets properties for a storage account's Blob service endpoint.

Mono<BlobContainerAsyncClient> undeleteBlobContainer(String deletedContainerName, String deletedContainerVersion)

Restores a previously deleted container.

Mono<Response<BlobContainerAsyncClient>> undeleteBlobContainerWithResponse(UndeleteBlobContainerOptions options)

Restores a previously deleted container.

Methods inherited from java.lang.Object

Method Details

createBlobContainer

public Mono createBlobContainer(String containerName)

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

BlobContainerAsyncClient blobContainerAsyncClient =
     client.createBlobContainer("containerName").block();

Parameters:

containerName - Name of the container to create

Returns:

A Mono containing a BlobContainerAsyncClient used to interact with the container created.

createBlobContainerIfNotExists

public Mono createBlobContainerIfNotExists(String containerName)

Creates a new container within a storage account if it does not exist. For more information, see the Azure Docs.

Code Samples

BlobContainerAsyncClient blobContainerAsyncClient =
     client.createBlobContainerIfNotExists("containerName").block();

Parameters:

containerName - Name of the container to create

Returns:

A Mono containing a BlobContainerAsyncClient used to interact with the container created.

createBlobContainerIfNotExistsWithResponse

public Mono<>> createBlobContainerIfNotExistsWithResponse(String containerName, BlobContainerCreateOptions options)

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.createBlobContainerIfNotExistsWithResponse("containerName", options).subscribe(response -> {
     if (response.getStatusCode() == 409) {
         System.out.println("Already exists.");
     } else {
         System.out.println("successfully created.");
     }
 });

Parameters:

containerName - Name of the container to create

Returns:

A Mono containing a Response<T> whose value contains a BlobContainerAsyncClient used to interact with the container created. If Response<T>'s status code is 201, a new container was successfully created. If status code is 409, a container with the same name already existed at this location.

createBlobContainerWithResponse

public Mono<>> createBlobContainerWithResponse(String containerName, Map metadata, PublicAccessType accessType)

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");

 BlobContainerAsyncClient containerClient = client
     .createBlobContainerWithResponse("containerName", metadata, PublicAccessType.CONTAINER).block().getValue();

Parameters:

containerName - Name of the container to create
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.
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.

Returns:

A Mono containing a Response<T> whose value contains a BlobContainerAsyncClient used to interact with the container created.

deleteBlobContainer

public Mono deleteBlobContainer(String containerName)

Deletes the specified container in the storage account. If the container doesn't exist the operation fails. For more information see the Azure Docs.

Code Samples

client.deleteBlobContainer("containerName").subscribe(
     response -> System.out.printf("Delete container completed%n"),
     error -> System.out.printf("Delete container failed: %s%n", error));

Parameters:

containerName - Name of the container to delete

Returns:

A Mono containing status code and HTTP headers

deleteBlobContainerIfExists

public Mono deleteBlobContainerIfExists(String containerName)

Deletes the specified container in the storage account if it exists. For more information see the Azure Docs.

Code Samples

client.deleteBlobContainerIfExists("containerName").subscribe(deleted -> {
     if (deleted) {
         System.out.println("Successfully deleted.");
     } else {
         System.out.println("Does not exist.");
     }
 });

Parameters:

containerName - Name of the container to delete

Returns:

A reactive Mono signaling completion. true indicates that the container was deleted. false indicates the container does not exist at this location.

deleteBlobContainerIfExistsWithResponse

public Mono<>> deleteBlobContainerIfExistsWithResponse(String containerName)

Deletes the specified container in the storage account if it exists. For more information see the Azure Docs.

Code Samples

Context context = new Context("Key", "Value");
 client.deleteBlobContainerIfExistsWithResponse("containerName").subscribe(response -> {
     if (response.getStatusCode() == 404) {
         System.out.println("Does not exist.");
     } else {
         System.out.println("successfully deleted.");
     }
 });

Parameters:

containerName - Name of the container to delete

Returns:

A reactive response signaling completion. If Response<T>'s status code is 202, the blob container was successfully deleted. If status code is 404, the blob container does not exist.

deleteBlobContainerWithResponse

public Mono<>> deleteBlobContainerWithResponse(String containerName)

Deletes the specified container in the storage account. If the container doesn't exist the operation fails. For more information see the Azure Docs.

Code Samples

Context context = new Context("Key", "Value");
 client.deleteBlobContainerWithResponse("containerName").subscribe(response ->
     System.out.printf("Delete container completed with status %d%n", response.getStatusCode()));

Parameters:

containerName - Name of the container to delete

Returns:

A Mono containing status code and HTTP headers

findBlobsByTags

public PagedFlux findBlobsByTags(FindBlobsOptions options)

Returns a reactive Publisher emitting the blobs in this account 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()));

Parameters:

options - FindBlobsOptions

Returns:

A reactive response emitting the list of blobs.

findBlobsByTags

public PagedFlux findBlobsByTags(String query)

Returns a reactive Publisher emitting the blobs in this account 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()));

Parameters:

query - Filters the results to return only blobs whose tags match the specified expression.

Returns:

A reactive response emitting the list of blobs.

generateAccountSas

public String generateAccountSas(AccountSasSignatureValues accountSasSignatureValues)

Generates an account SAS for the Azure Storage account using the specified AccountSasSignatureValues.

Note : The client must be authenticated via StorageSharedKeyCredential

See AccountSasSignatureValues for more information on how to construct an account SAS.

The snippet below generates a SAS that lasts for two days and gives the user read and list access to blob containers and file shares.

AccountSasPermission permissions = new AccountSasPermission()
     .setListPermission(true)
     .setReadPermission(true);
 AccountSasResourceType resourceTypes = new AccountSasResourceType().setContainer(true);
 AccountSasService services = new AccountSasService().setBlobAccess(true).setFileAccess(true);
 OffsetDateTime expiryTime = OffsetDateTime.now().plus(Duration.ofDays(2));

 AccountSasSignatureValues sasValues =
     new AccountSasSignatureValues(expiryTime, permissions, services, resourceTypes);

 // Client must be authenticated via StorageSharedKeyCredential
 String sas = client.generateAccountSas(sasValues);

Parameters:

accountSasSignatureValues - AccountSasSignatureValues

Returns:

A String representing the SAS query parameters.

generateAccountSas

public String generateAccountSas(AccountSasSignatureValues accountSasSignatureValues, Context context)

Generates an account SAS for the Azure Storage account using the specified AccountSasSignatureValues.

Note : The client must be authenticated via StorageSharedKeyCredential

See AccountSasSignatureValues for more information on how to construct an account SAS.

The snippet below generates a SAS that lasts for two days and gives the user read and list access to blob containers and file shares.

AccountSasPermission permissions = new AccountSasPermission()
     .setListPermission(true)
     .setReadPermission(true);
 AccountSasResourceType resourceTypes = new AccountSasResourceType().setContainer(true);
 AccountSasService services = new AccountSasService().setBlobAccess(true).setFileAccess(true);
 OffsetDateTime expiryTime = OffsetDateTime.now().plus(Duration.ofDays(2));

 AccountSasSignatureValues sasValues =
     new AccountSasSignatureValues(expiryTime, permissions, services, resourceTypes);

 // Client must be authenticated via StorageSharedKeyCredential
 String sas = client.generateAccountSas(sasValues, new Context("key", "value"));

Parameters:

accountSasSignatureValues - AccountSasSignatureValues
context - Additional context that is passed through the code when generating a SAS.

Returns:

A String representing the SAS query parameters.

getAccountInfo

public Mono getAccountInfo()

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()));

Returns:

A Mono containing the storage account info.

getAccountInfoWithResponse

public Mono<>> getAccountInfoWithResponse()

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()));

Returns:

A Mono containing a Response<T> whose value the storage account info.

getAccountName

public String getAccountName()

Get associated account name.

Returns:

account name associated with this storage resource.

getAccountUrl

public String getAccountUrl()

Gets the URL of the storage account represented by this client.

Returns:

the URL.

getBlobContainerAsyncClient

public BlobContainerAsyncClient getBlobContainerAsyncClient(String containerName)

Initializes a BlobContainerAsyncClient object pointing to the specified container. This method does not create a container. It simply constructs the URL to the container and offers access to methods relevant to containers.

Code Samples

BlobContainerAsyncClient blobContainerAsyncClient = client.getBlobContainerAsyncClient("containerName");

Parameters:

containerName - The name of the container to point to. A value of null or empty string will be interpreted as pointing to the root container and will be replaced by "$root".

Returns:

A BlobContainerAsyncClient object pointing to the specified container

getHttpPipeline

public HttpPipeline getHttpPipeline()

Gets the HttpPipeline powering this client.

Returns:

The pipeline.

getProperties

public Mono getProperties()

Gets the properties of a storage account\u2019s Blob service. For more information, see the Azure Docs.

Code Samples

client.getProperties().subscribe(response ->
     System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b%n",
         response.getHourMetrics().isEnabled(),
         response.getMinuteMetrics().isEnabled()));

Returns:

A reactive response containing the storage account properties.

getPropertiesWithResponse

public Mono<>> getPropertiesWithResponse()

Gets the properties of a storage account\u2019s Blob service. For more information, see the Azure Docs.

Code Samples

client.getPropertiesWithResponse().subscribe(response ->
     System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b%n",
         response.getValue().getHourMetrics().isEnabled(),
         response.getValue().getMinuteMetrics().isEnabled()));

Returns:

A Mono containing a Response<T> whose value contains the storage account properties.

getServiceVersion

public BlobServiceVersion getServiceVersion()

Gets the service version the client is using.

Returns:

the service version the client is using.

getStatistics

public Mono getStatistics()

Retrieves statistics related to replication for the Blob service. It is only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the storage account. For more information, see the Azure Docs.

Code Samples

client.getStatistics().subscribe(response ->
     System.out.printf("Geo-replication status: %s%n", response.getGeoReplication().getStatus()));

Returns:

A Mono containing the storage account statistics.

getStatisticsWithResponse

public Mono<>> getStatisticsWithResponse()

Retrieves statistics related to replication for the Blob service. It is only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the storage account. For more information, see the Azure Docs.

Code Samples

client.getStatisticsWithResponse().subscribe(response ->
     System.out.printf("Geo-replication status: %s%n", response.getValue().getGeoReplication().getStatus()));

Returns:

A Mono containing a Response<T> whose value containing the storage account statistics.

getUserDelegationKey

public Mono getUserDelegationKey(OffsetDateTime start, OffsetDateTime expiry)

Gets a user delegation key for use with this account's blob storage. Note: This method call is only valid when using TokenCredential in this object's HttpPipeline.

Code Samples

client.getUserDelegationKey(delegationKeyStartTime, delegationKeyExpiryTime).subscribe(response ->
     System.out.printf("User delegation key: %s%n", response.getValue()));

Parameters:

start - Start time for the key's validity. Null indicates immediate start.
expiry - Expiration of the key's validity.

Returns:

A Mono containing the user delegation key.

getUserDelegationKeyWithResponse

public Mono<>> getUserDelegationKeyWithResponse(OffsetDateTime start, OffsetDateTime expiry)

Gets a user delegation key for use with this account's blob storage. Note: This method call is only valid when using TokenCredential in this object's HttpPipeline.

Code Samples

client.getUserDelegationKeyWithResponse(delegationKeyStartTime, delegationKeyExpiryTime).subscribe(response ->
     System.out.printf("User delegation key: %s%n", response.getValue().getValue()));

Parameters:

start - Start time for the key's validity. Null indicates immediate start.
expiry - Expiration of the key's validity.

Returns:

A Mono containing a Response<T> whose value containing the user delegation key.

listBlobContainers

public PagedFlux listBlobContainers()

Returns a reactive Publisher emitting all the containers in this account lazily as needed. For more information, see the Azure Docs.

Code Samples

client.listBlobContainers().subscribe(container -> System.out.printf("Name: %s%n", container.getName()));

Returns:

A reactive response emitting the list of containers.

listBlobContainers

public PagedFlux listBlobContainers(ListBlobContainersOptions options)

Returns a reactive Publisher emitting all the containers in this account lazily as needed. For more information, see the Azure Docs.

Code Samples

ListBlobContainersOptions options = new ListBlobContainersOptions()
     .setPrefix("containerNamePrefixToMatch")
     .setDetails(new BlobContainerListDetails().setRetrieveMetadata(true));

 client.listBlobContainers(options).subscribe(container -> System.out.printf("Name: %s%n", container.getName()));

Parameters:

options - A ListBlobContainersOptions which specifies what data should be returned by the service.

Returns:

A reactive response emitting the list of containers.

setProperties

public Mono setProperties(BlobServiceProperties properties)

Sets properties for a storage account's Blob service endpoint. For more information, see the Azure Docs. Note that setting the default service version has no effect when using this client because this client explicitly sets the version header on each request, overriding the default.

This method checks to ensure the properties being sent follow the specifications indicated in the Azure Docs. If CORS policies are set, CORS parameters that are not set default to the empty string.

Code Samples

BlobRetentionPolicy loggingRetentionPolicy = new BlobRetentionPolicy().setEnabled(true).setDays(3);
 BlobRetentionPolicy metricsRetentionPolicy = new BlobRetentionPolicy().setEnabled(true).setDays(1);

 BlobServiceProperties properties = new BlobServiceProperties()
     .setLogging(new BlobAnalyticsLogging()
         .setWrite(true)
         .setDelete(true)
         .setRetentionPolicy(loggingRetentionPolicy))
     .setHourMetrics(new BlobMetrics()
         .setEnabled(true)
         .setRetentionPolicy(metricsRetentionPolicy))
     .setMinuteMetrics(new BlobMetrics()
         .setEnabled(true)
         .setRetentionPolicy(metricsRetentionPolicy));

 client.setProperties(properties).subscribe(
     response -> System.out.printf("Setting properties completed%n"),
     error -> System.out.printf("Setting properties failed: %s%n", error));

Parameters:

properties - Configures the service.

Returns:

A Mono containing the storage account properties.

setPropertiesWithResponse

public Mono<>> setPropertiesWithResponse(BlobServiceProperties properties)

Sets properties for a storage account's Blob service endpoint. For more information, see the Azure Docs. Note that setting the default service version has no effect when using this client because this client explicitly sets the version header on each request, overriding the default.

This method checks to ensure the properties being sent follow the specifications indicated in the Azure Docs. If CORS policies are set, CORS parameters that are not set default to the empty string.

Code Samples

BlobRetentionPolicy loggingRetentionPolicy = new BlobRetentionPolicy().setEnabled(true).setDays(3);
 BlobRetentionPolicy metricsRetentionPolicy = new BlobRetentionPolicy().setEnabled(true).setDays(1);

 BlobServiceProperties properties = new BlobServiceProperties()
     .setLogging(new BlobAnalyticsLogging()
         .setWrite(true)
         .setDelete(true)
         .setRetentionPolicy(loggingRetentionPolicy))
     .setHourMetrics(new BlobMetrics()
         .setEnabled(true)
         .setRetentionPolicy(metricsRetentionPolicy))
     .setMinuteMetrics(new BlobMetrics()
         .setEnabled(true)
         .setRetentionPolicy(metricsRetentionPolicy));

 client.setPropertiesWithResponse(properties).subscribe(response ->
     System.out.printf("Setting properties completed with status %d%n", response.getStatusCode()));

Parameters:

properties - Configures the service.

Returns:

A Mono containing the storage account properties.

undeleteBlobContainer

public Mono undeleteBlobContainer(String deletedContainerName, String deletedContainerVersion)

Restores a previously deleted container. If the container associated with provided deletedContainerName already exists, this call will result in a 409 (conflict). This API is only functional if Container Soft Delete is enabled for the storage account associated with the container.

Code Samples

ListBlobContainersOptions listBlobContainersOptions = new ListBlobContainersOptions();
 listBlobContainersOptions.getDetails().setRetrieveDeleted(true);
 client.listBlobContainers(listBlobContainersOptions).flatMap(
     deletedContainer -> {
         Mono<BlobContainerAsyncClient> blobContainerClient = client.undeleteBlobContainer(
             deletedContainer.getName(), deletedContainer.getVersion());
         return blobContainerClient;
     }
 ).then().block();

Parameters:

deletedContainerName - The name of the previously deleted container.
deletedContainerVersion - The version of the previously deleted container.

Returns:

A Mono containing a BlobContainerAsyncClient used to interact with the restored container.

undeleteBlobContainerWithResponse

public Mono<>> undeleteBlobContainerWithResponse(UndeleteBlobContainerOptions options)

Restores a previously deleted container. The restored container will be renamed to the destinationContainerName if provided in options. Otherwise deletedContainerName is used as destination container name. If the container associated with provided destinationContainerName already exists, this call will result in a 409 (conflict). This API is only functional if Container Soft Delete is enabled for the storage account associated with the container.

Code Samples

ListBlobContainersOptions listBlobContainersOptions = new ListBlobContainersOptions();
 listBlobContainersOptions.getDetails().setRetrieveDeleted(true);
 client.listBlobContainers(listBlobContainersOptions).flatMap(
     deletedContainer -> {
         Mono<BlobContainerAsyncClient> blobContainerClient = client.undeleteBlobContainerWithResponse(
             new UndeleteBlobContainerOptions(deletedContainer.getName(), deletedContainer.getVersion()))
             .map(Response::getValue);
         return blobContainerClient;
     }
 ).then().block();

Parameters:

Returns:

A Mono containing a Response<T> whose value contains a BlobContainerAsyncClient used to interact with the restored container.

Applies to