BlobServiceClient Class

Definition

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 BlobContainerClient through getBlobContainerClient(String containerName), and operations on a blob are available on BlobClient.

Please see here for more information on containers.

public final class BlobServiceClient
Inheritance
java.lang.Object
BlobServiceClient

Inherited Members

java.lang.Object.clone() java.lang.Object.equals(java.lang.Object) java.lang.Object.finalize() java.lang.Object.getClass() java.lang.Object.hashCode() java.lang.Object.notify() java.lang.Object.notifyAll() java.lang.Object.toString() java.lang.Object.wait() java.lang.Object.wait(long) java.lang.Object.wait(long,int)

Methods

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

BlobContainerClient blobContainerClient = client.createBlobContainer("containerName");
createBlobContainerWithResponse(String containerName, Map<String,String> metadata, PublicAccessType accessType, Context context)

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");
 Context context = new Context("Key", "Value");
 
 BlobContainerClient blobContainerClient = client.createBlobContainerWithResponse(
     "containerName",
     metadata,
     PublicAccessType.CONTAINER,
     context).getValue();
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

try {
     client.deleteBlobContainer("container Name");
     System.out.printf("Delete container completed with status %n");
 } catch (UnsupportedOperationException error) {
     System.out.printf("Delete container failed: %s%n", error);
 }
deleteBlobContainerWithResponse(String containerName, Context context)

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

findBlobsByTags(FindBlobsOptions options, Duration timeout, Context context)

Returns a lazy loaded list of blobs in this account whose tags match the query expression. The returned PagedIterable<T> can be consumed while new items are automatically retrieved as needed. For more information, including information on the query syntax, see the Azure Docs.

Code Samples

Context context = new Context("Key", "Value");
 client.findBlobsByTags(new FindBlobsOptions("where=tag=value").setMaxResultsPerPage(10), timeout, context)
     .forEach(blob -> System.out.printf("Name: %s%n", blob.getName()));
findBlobsByTags(String query)

Returns a lazy loaded list of blobs in this account whose tags match the query expression. The returned PagedIterable<T> can be consumed while new items are automatically retrieved as needed. For more information, including information on the query syntax, see the Azure Docs.

Code Samples

client.findBlobsByTags("where=tag=value").forEach(blob -> System.out.printf("Name: %s%n", blob.getName()));
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.

Generating an account SAS

The snippet below generates an AccountSasSignatureValues object that lasts for two days and gives the user read and list access to blob 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);
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.

Generating an account SAS

The snippet below generates an AccountSasSignatureValues object that lasts for two days and gives the user read and list access to blob 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"));
getAccountInfo()

Returns the sku name and account kind for the account. For more information, please see the Azure Docs.

Code Samples

StorageAccountInfo accountInfo = client.getAccountInfo();
 
 System.out.printf("Account kind: %s, SKU: %s%n", accountInfo.getAccountKind(), accountInfo.getSkuName());
getAccountInfoWithResponse(Duration timeout, Context context)

Returns the sku name and account kind for the account. For more information, please see the Azure Docs.

getAccountName()

Get associated account name.

getAccountUrl()

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

getBlobContainerClient(String containerName)

Initializes a BlobContainerClient 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

BlobContainerClient blobContainerClient = client.getBlobContainerClient("containerName");
getHttpPipeline()

Gets the HttpPipeline powering this client.

getProperties()

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

Code Samples

BlobServiceProperties properties = client.getProperties();
 
 System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b%n",
     properties.getHourMetrics().isEnabled(),
     properties.getMinuteMetrics().isEnabled());
getPropertiesWithResponse(Duration timeout, Context context)

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

Code Samples

Context context = new Context("Key", "Value");
 BlobServiceProperties properties = client.getPropertiesWithResponse(timeout, context).getValue();
 
 System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b%n",
     properties.getHourMetrics().isEnabled(),
     properties.getMinuteMetrics().isEnabled());
getServiceVersion()

Gets the service version the client is using.

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

System.out.printf("Geo-replication status: %s%n",
     client.getStatistics().getGeoReplication().getStatus());
getStatisticsWithResponse(Duration timeout, Context context)

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

System.out.printf("Geo-replication status: %s%n",
     client.getStatisticsWithResponse(timeout, context).getValue().getGeoReplication().getStatus());
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

System.out.printf("User delegation key: %s%n",
     client.getUserDelegationKey(delegationKeyStartTime, delegationKeyExpiryTime));
getUserDelegationKeyWithResponse(OffsetDateTime start, OffsetDateTime expiry, Duration timeout, Context context)

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

System.out.printf("User delegation key: %s%n",
     client.getUserDelegationKeyWithResponse(delegationKeyStartTime, delegationKeyExpiryTime, timeout, context));
listBlobContainers()

Returns a lazy loaded list of containers in this account. The returned PagedIterable<T> can be consumed while new items are automatically retrieved as needed. For more information, see the Azure Docs.

Code Samples

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

Returns a lazy loaded list of containers in this account. The returned PagedIterable<T> can be consumed while new items are automatically retrieved 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, timeout).forEach(container -> System.out.printf("Name: %s%n", container.getName()));
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));
 
 try {
     client.setProperties(properties);
     System.out.printf("Setting properties completed%n");
 } catch (UnsupportedOperationException error) {
     System.out.printf("Setting properties failed: %s%n", error);
 }
setPropertiesWithResponse(BlobServiceProperties properties, Duration timeout, Context context)

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));
 
 Context context = new Context("Key", "Value");
 
 System.out.printf("Setting properties completed with status %d%n",
     client.setPropertiesWithResponse(properties, timeout, context).getStatusCode());
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, null).forEach(
     deletedContainer -> {
         BlobContainerClient blobContainerClient = client.undeleteBlobContainer(
             deletedContainer.getName(), deletedContainer.getVersion());
     }
 );
undeleteBlobContainerWithResponse(UndeleteBlobContainerOptions options, Duration timeout, Context context)

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, null).forEach(
     deletedContainer -> {
         BlobContainerClient blobContainerClient = client.undeleteBlobContainerWithResponse(
             new UndeleteBlobContainerOptions(deletedContainer.getName(), deletedContainer.getVersion()),
             timeout, context).getValue();
     }
 );

Applies to