BlobContainerClient Class

Definition

Client to a container. It may only be instantiated through a BlobContainerClientBuilder or via the method getBlobContainerClient(String containerName). This class does not hold any state about a particular container 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 BlobClient through getBlobClient(String blobName), and operations on the service are available on BlobServiceClient.

Please refer to the Azure Docs for more information on containers.

public final class BlobContainerClient
Inheritance
java.lang.Object
BlobContainerClient

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)

Fields

LOG_CONTAINER_NAME
ROOT_CONTAINER_NAME
STATIC_WEBSITE_CONTAINER_NAME

Methods

create()

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

try {
     client.create();
     System.out.printf("Create completed%n");
 } catch (BlobStorageException error) {
     if (error.getErrorCode().equals(BlobErrorCode.CONTAINER_ALREADY_EXISTS)) {
         System.out.printf("Can't create container. It already exists %n");
     }
 }
createWithResponse(Map<String,String> metadata, PublicAccessType accessType, Duration timeout, 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");
 
 System.out.printf("Create completed with status %d%n",
     client.createWithResponse(metadata, PublicAccessType.CONTAINER, timeout, context).getStatusCode());
delete()

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

try {
     client.delete();
     System.out.printf("Delete completed%n");
 } catch (BlobStorageException error) {
     if (error.getErrorCode().equals(BlobErrorCode.CONTAINER_NOT_FOUND)) {
         System.out.printf("Delete failed. Container was not found %n");
     }
 }
deleteWithResponse(BlobRequestConditions requestConditions, Duration timeout, Context context)

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));
 Context context = new Context("Key", "Value");
 
 System.out.printf("Delete completed with status %d%n", client.deleteWithResponse(
     requestConditions, timeout, context).getStatusCode());
exists()

Gets if the container this client represents exists in the cloud.

Code Samples

System.out.printf("Exists? %b%n", client.exists());
existsWithResponse(Duration timeout, Context context)

Gets if the container this client represents exists in the cloud.

Code Samples

Context context = new Context("Key", "Value");
 System.out.printf("Exists? %b%n", client.existsWithResponse(timeout, context).getValue());
generateSas(BlobServiceSasSignatureValues blobServiceSasSignatureValues)

Generates a service SAS for the container using the specified BlobServiceSasSignatureValues

Note : The client must be authenticated via StorageSharedKeyCredential

See 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
generateSas(BlobServiceSasSignatureValues blobServiceSasSignatureValues, Context context)

Generates a service SAS for the container using the specified BlobServiceSasSignatureValues

Note : The client must be authenticated via StorageSharedKeyCredential

See 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"));
generateUserDelegationSas(BlobServiceSasSignatureValues blobServiceSasSignatureValues, UserDelegationKey userDelegationKey)

Generates a user delegation SAS for the container using the specified BlobServiceSasSignatureValues.

See 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);
generateUserDelegationSas(BlobServiceSasSignatureValues blobServiceSasSignatureValues, UserDelegationKey userDelegationKey, String accountName, Context context)

Generates a user delegation SAS for the container using the specified BlobServiceSasSignatureValues.

See 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"));
getAccessPolicy()

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

BlobContainerAccessPolicies accessPolicies = client.getAccessPolicy();
 System.out.printf("Blob Access Type: %s%n", accessPolicies.getBlobAccessType());
 
 for (BlobSignedIdentifier identifier : accessPolicies.getIdentifiers()) {
     System.out.printf("Identifier Name: %s, Permissions %s%n",
         identifier.getId(),
         identifier.getAccessPolicy().getPermissions());
 }
getAccessPolicyWithResponse(String leaseId, Duration timeout, Context context)

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

Context context = new Context("Key", "Value");
 BlobContainerAccessPolicies accessPolicies = client.getAccessPolicyWithResponse(leaseId, timeout, context)
     .getValue();
 System.out.printf("Blob Access Type: %s%n", accessPolicies.getBlobAccessType());
 
 for (BlobSignedIdentifier identifier : accessPolicies.getIdentifiers()) {
     System.out.printf("Identifier Name: %s, Permissions %s%n",
         identifier.getId(),
         identifier.getAccessPolicy().getPermissions());
 }
getAccountInfo(Duration timeout)

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

getAccountInfoWithResponse(Duration timeout, Context context)

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

Code Samples

Context context = new Context("Key", "Value");
 StorageAccountInfo accountInfo = client.getAccountInfoWithResponse(timeout, context).getValue();
 System.out.printf("Account Kind: %s, SKU: %s%n", accountInfo.getAccountKind(), accountInfo.getSkuName());
getAccountName()

Get associated account name.

getAccountUrl()

Get the url of the storage account.

getBlobClient(String blobName)

Initializes a new BlobClient object by concatenating blobName to the end of ContainerAsyncClient's URL. The new BlobClient uses the same request policy pipeline as the ContainerAsyncClient.

getBlobClient(String blobName, String snapshot)

Initializes a new BlobClient object by concatenating blobName to the end of ContainerAsyncClient's URL. The new BlobClient uses the same request policy pipeline as the ContainerAsyncClient.

Code Samples

BlobClient blobClient = client.getBlobClient(blobName, snapshot);
getBlobContainerName()

Get the container name.

Code Samples

String containerName = client.getBlobContainerName();
 System.out.println("The name of the blob is " + containerName);
getBlobContainerUrl()

Gets the URL of the container represented by this client.

getBlobVersionClient(String blobName, String versionId)

Initializes a new BlobClient object by concatenating blobName to the end of ContainerAsyncClient's URL. The new BlobClient uses the same request policy pipeline as the ContainerAsyncClient.

getCustomerProvidedKey()

Gets the CpkInfo associated with this client that will be passed to BlobClient when getBlobClient(String blobName) is called.

getEncryptionScope()

Gets the encryption scope used to encrypt this blob's content on the server.

getHttpPipeline()

Gets the HttpPipeline powering this client.

getProperties()

Returns the container's metadata and system properties. For more information, see the Azure Docs.

Code Samples

BlobContainerProperties properties = client.getProperties();
 System.out.printf("Public Access Type: %s, Legal Hold? %b, Immutable? %b%n",
     properties.getBlobPublicAccess(),
     properties.hasLegalHold(),
     properties.hasImmutabilityPolicy());
getPropertiesWithResponse(String leaseId, Duration timeout, Context context)

Returns the container's metadata and system properties. For more information, see the Azure Docs.

Code Samples

Context context = new Context("Key", "Value");
 
 BlobContainerProperties properties = client.getPropertiesWithResponse(leaseId, timeout, context)
     .getValue();
 System.out.printf("Public Access Type: %s, Legal Hold? %b, Immutable? %b%n",
     properties.getBlobPublicAccess(),
     properties.hasLegalHold(),
     properties.hasImmutabilityPolicy());
getServiceClient()

Get a client pointing to the account.

getServiceVersion()

Gets the service version the client is using.

listBlobs()

Returns a lazy loaded list of blobs in this container, with folder structures flattened. The returned PagedIterable<T> can be consumed through while new items are automatically retrieved as needed.

Blob names are returned in lexicographic order.

For more information, see the Azure Docs.

Code Samples

client.listBlobs().forEach(blob ->
     System.out.printf("Name: %s, Directory? %b%n", blob.getName(), blob.isPrefix()));
listBlobs(ListBlobsOptions options, String continuationToken, Duration timeout)

Returns a lazy loaded list of blobs in this container, with folder structures flattened. The returned PagedIterable<T> can be consumed through while new items are automatically retrieved as needed.

Blob names are returned in lexicographic order.

For more information, see the Azure Docs.

Code Samples

ListBlobsOptions options = new ListBlobsOptions()
     .setPrefix("prefixToMatch")
     .setDetails(new BlobListDetails()
         .setRetrieveDeletedBlobs(true)
         .setRetrieveSnapshots(true));
 
 String continuationToken = "continuationToken";
 
 client.listBlobs(options, continuationToken, timeout).forEach(blob ->
     System.out.printf("Name: %s, Directory? %b, Deleted? %b, Snapshot ID: %s%n",
         blob.getName(),
         blob.isPrefix(),
         blob.isDeleted(),
         blob.getSnapshot()));
listBlobs(ListBlobsOptions options, Duration timeout)

Returns a lazy loaded list of blobs in this container, with folder structures flattened. The returned PagedIterable<T> can be consumed through while new items are automatically retrieved as needed.

Blob names are returned in lexicographic order.

For more information, see the Azure Docs.

Code Samples

ListBlobsOptions options = new ListBlobsOptions()
     .setPrefix("prefixToMatch")
     .setDetails(new BlobListDetails()
         .setRetrieveDeletedBlobs(true)
         .setRetrieveSnapshots(true));
 
 client.listBlobs(options, timeout).forEach(blob ->
     System.out.printf("Name: %s, Directory? %b, Deleted? %b, Snapshot ID: %s%n",
         blob.getName(),
         blob.isPrefix(),
         blob.isDeleted(),
         blob.getSnapshot()));
listBlobsByHierarchy(String directory)

Returns a reactive Publisher emitting all the blobs and directories (prefixes) under the given directory (prefix). Directories will have 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").forEach(blob ->
     System.out.printf("Name: %s, Directory? %b%n", blob.getName(), blob.isPrefix()));
listBlobsByHierarchy(String delimiter, ListBlobsOptions options, Duration timeout)

Returns a reactive Publisher emitting all the blobs and prefixes (directories) under the given prefix (directory). Directories will have 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, timeout).forEach(blob ->
     System.out.printf("Name: %s, Directory? %b, Deleted? %b, Snapshot ID: %s%n",
         blob.getName(),
         blob.isPrefix(),
         blob.isDeleted(),
         blob.getSnapshot()));
setAccessPolicy(PublicAccessType accessType, List<BlobSignedIdentifier> identifiers)

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"));
 
 try {
     client.setAccessPolicy(PublicAccessType.CONTAINER, Collections.singletonList(identifier));
     System.out.printf("Set Access Policy completed %n");
 } catch (UnsupportedOperationException error) {
     System.out.printf("Set Access Policy completed %s%n", error);
 }
setAccessPolicyWithResponse(PublicAccessType accessType, List<BlobSignedIdentifier> identifiers, BlobRequestConditions requestConditions, Duration timeout, Context context)

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));
 
 Context context = new Context("Key", "Value");
 
 System.out.printf("Set access policy completed with status %d%n",
     client.setAccessPolicyWithResponse(PublicAccessType.CONTAINER,
         Collections.singletonList(identifier),
         requestConditions,
         timeout,
         context).getStatusCode());
setMetadata(Map<String,String> metadata)

Sets the container's metadata. For more information, see the Azure Docs.

Code Samples

Map<String, String> metadata = Collections.singletonMap("metadata", "value");
 try {
     client.setMetadata(metadata);
     System.out.printf("Set metadata completed with status %n");
 } catch (UnsupportedOperationException error) {
     System.out.printf("Fail while setting metadata %n");
 }
setMetadataWithResponse(Map<String,String> metadata, BlobRequestConditions requestConditions, Duration timeout, Context context)

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));
 Context context = new Context("Key", "Value");
 
 System.out.printf("Set metadata completed with status %d%n",
     client.setMetadataWithResponse(metadata, requestConditions, timeout, context).getStatusCode());

Applies to