Class BlobBatchClient

java.lang.Object
com.azure.storage.blob.batch.BlobBatchClient

public final class BlobBatchClient extends Object
This class provides a client that contains all operations that apply to Azure Storage Blob batching.

This client offers the ability to delete and set access tier on multiple blobs at once and to submit a BlobBatch.

See Also:
  • Method Details

    • getBlobBatch

      public BlobBatch getBlobBatch()
      Gets a BlobBatch used to configure a batching operation to send to Azure Storage blobs.
      Returns:
      a new BlobBatch instance.
    • submitBatch

      public void submitBatch(BlobBatch batch)
      Submits a batch operation.

      If any request in a batch fails this will throw a BlobStorageException.

      Code samples

       BlobBatch batch = batchClient.getBlobBatch();
      
       Response<Void> deleteResponse1 = batch.deleteBlob("container", "blob1");
       Response<Void> deleteResponse2 = batch.deleteBlob("container", "blob2", DeleteSnapshotsOptionType.INCLUDE,
           new BlobRequestConditions().setLeaseId("leaseId"));
      
       try {
           batchClient.submitBatch(batch);
           System.out.println("Batch submission completed successfully.");
           System.out.printf("Delete operation 1 completed with status code: %d%n", deleteResponse1.getStatusCode());
           System.out.printf("Delete operation 2 completed with status code: %d%n", deleteResponse2.getStatusCode());
       } catch (BlobStorageException error) {
           System.err.printf("Batch submission failed. Error message: %s%n", error.getMessage());
       }
       
      Parameters:
      batch - Batch to submit.
      Throws:
      BlobStorageException - If the batch request is malformed.
      BlobBatchStorageException - If any request in the BlobBatch failed.
    • submitBatchWithResponse

      public Response<Void> submitBatchWithResponse(BlobBatch batch, boolean throwOnAnyFailure, Duration timeout, Context context)
      Submits a batch operation.

      If throwOnAnyFailure is true a BlobStorageException will be thrown if any request fails.

      Code samples

       BlobBatch batch = batchClient.getBlobBatch();
      
       Response<Void> deleteResponse1 = batch.deleteBlob("container", "blob1");
       Response<Void> deleteResponse2 = batch.deleteBlob("container", "blob2", DeleteSnapshotsOptionType.INCLUDE,
           new BlobRequestConditions().setLeaseId("leaseId"));
      
       try {
           System.out.printf("Batch submission completed with status code: %d%n",
               batchClient.submitBatchWithResponse(batch, true, timeout, Context.NONE).getStatusCode());
           System.out.printf("Delete operation 1 completed with status code: %d%n", deleteResponse1.getStatusCode());
           System.out.printf("Delete operation 2 completed with status code: %d%n", deleteResponse2.getStatusCode());
       } catch (BlobStorageException error) {
           System.err.printf("Batch submission failed. Error message: %s%n", error.getMessage());
       }
       
      Parameters:
      batch - Batch to submit.
      throwOnAnyFailure - Flag to indicate if an exception should be thrown if any request in the batch fails.
      timeout - An optional timeout value beyond which a RuntimeException will be raised.
      context - Additional context that is passed through the Http pipeline during the service call.
      Returns:
      A response only containing header and status code information, used to indicate that the batch operation has completed.
      Throws:
      RuntimeException - If the timeout duration completes before a response is returned.
      BlobStorageException - If the batch request is malformed.
      BlobBatchStorageException - If throwOnAnyFailure is true and any request in the BlobBatch failed.
    • deleteBlobs

      public PagedIterable<Response<Void>> deleteBlobs(List<String> blobUrls, DeleteSnapshotsOptionType deleteOptions)
      Delete multiple blobs in a single request to the service.

      Code samples

       List<String> blobUrls = new ArrayList<>();
       blobUrls.add(blobClient1.getBlobUrl());
       blobUrls.add(blobClient2.getBlobUrl());
       blobUrls.add(blobClient3.getBlobUrl());
      
       try {
           batchClient.deleteBlobs(blobUrls, DeleteSnapshotsOptionType.INCLUDE).forEach(response ->
               System.out.printf("Deleting blob with URL %s completed with status code %d%n",
                   response.getRequest().getUrl(), response.getStatusCode()));
       } catch (Throwable error) {
           System.err.printf("Deleting blob failed with exception: %s%n", error.getMessage());
       }
       
      Parameters:
      blobUrls - Urls of the blobs to delete. Blob names must be encoded to UTF-8.
      deleteOptions - The deletion option for all blobs.
      Returns:
      The status of each delete operation.
      Throws:
      BlobStorageException - If the batch request is malformed.
      BlobBatchStorageException - If any of the delete operations fail.
    • deleteBlobs

      public PagedIterable<Response<Void>> deleteBlobs(List<String> blobUrls, DeleteSnapshotsOptionType deleteOptions, Duration timeout, Context context)
      Delete multiple blobs in a single request to the service.

      Code samples

       List<String> blobUrls = new ArrayList<>();
       blobUrls.add(blobClient1.getBlobUrl());
       blobUrls.add(blobClient2.getBlobUrl());
       blobUrls.add(blobClient3.getBlobUrl());
      
       try {
           batchClient.deleteBlobs(blobUrls, DeleteSnapshotsOptionType.INCLUDE, timeout, Context.NONE)
               .forEach(response -> System.out.printf("Deleting blob with URL %s completed with status code %d%n",
                   response.getRequest().getUrl(), response.getStatusCode()));
       } catch (Throwable error) {
           System.err.printf("Deleting blob failed with exception: %s%n", error.getMessage());
       }
       
      Parameters:
      blobUrls - Urls of the blobs to delete. Blob names must be encoded to UTF-8.
      deleteOptions - The deletion option for all blobs.
      timeout - An optional timeout value beyond which a RuntimeException will be raised.
      context - Additional context that is passed through the Http pipeline during the service call.
      Returns:
      The status of each delete operation.
      Throws:
      RuntimeException - If the timeout duration completes before a response is returned.
      BlobStorageException - If the batch request is malformed.
      BlobBatchStorageException - If any of the delete operations fail.
    • setBlobsAccessTier

      public PagedIterable<Response<Void>> setBlobsAccessTier(List<String> blobUrls, AccessTier accessTier)
      Set access tier on multiple blobs in a single request to the service.

      Code samples

       List<String> blobUrls = new ArrayList<>();
       blobUrls.add(blobClient1.getBlobUrl());
       blobUrls.add(blobClient2.getBlobUrl());
       blobUrls.add(blobClient3.getBlobUrl());
      
       try {
           batchClient.setBlobsAccessTier(blobUrls, AccessTier.HOT).forEach(response ->
               System.out.printf("Setting blob access tier with URL %s completed with status code %d%n",
                   response.getRequest().getUrl(), response.getStatusCode()));
       } catch (Throwable error) {
           System.err.printf("Setting blob access tier failed with exception: %s%n", error.getMessage());
       }
       
      Parameters:
      blobUrls - Urls of the blobs to set their access tier. Blob names must be encoded to UTF-8.
      accessTier - AccessTier to set on each blob.
      Returns:
      The status of each set tier operation.
      Throws:
      BlobStorageException - If the batch request is malformed.
      BlobBatchStorageException - If any of the set tier operations fail.
    • setBlobsAccessTier

      public PagedIterable<Response<Void>> setBlobsAccessTier(List<String> blobUrls, AccessTier accessTier, Duration timeout, Context context)
      Set access tier on multiple blobs in a single request to the service.

      Code samples

       List<String> blobUrls = new ArrayList<>();
       blobUrls.add(blobClient1.getBlobUrl());
       blobUrls.add(blobClient2.getBlobUrl());
       blobUrls.add(blobClient3.getBlobUrl());
      
       try {
           batchClient.setBlobsAccessTier(blobUrls, AccessTier.HOT, timeout, Context.NONE).forEach(response ->
               System.out.printf("Setting blob access tier with URL %s completed with status code %d%n",
                   response.getRequest().getUrl(), response.getStatusCode()));
       } catch (Throwable error) {
           System.err.printf("Setting blob access tier failed with exception: %s%n", error.getMessage());
       }
       
      Parameters:
      blobUrls - Urls of the blobs to set their access tier. Blob names must be encoded to UTF-8.
      accessTier - AccessTier to set on each blob.
      timeout - An optional timeout value beyond which a RuntimeException will be raised.
      context - Additional context that is passed through the Http pipeline during the service call.
      Returns:
      The status of each set tier operation.
      Throws:
      RuntimeException - If the timeout duration completes before a response is returned.
      BlobStorageException - If the batch request is malformed.
      BlobBatchStorageException - If any of the set tier operations fail.