com.azure.storage.blob.batch.BlobBatchClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-storage-blob-batch Show documentation
Show all versions of azure-storage-blob-batch Show documentation
This module contains client library for Microsoft Azure Blob Storage batching.
The newest version!
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.storage.blob.batch;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.util.Context;
import com.azure.storage.blob.models.AccessTier;
import com.azure.storage.blob.models.BlobStorageException;
import com.azure.storage.blob.models.DeleteSnapshotsOptionType;
import com.azure.storage.common.implementation.StorageImplUtils;
import java.time.Duration;
import java.util.List;
/**
* 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 {@link
* BlobBatch}.
*
* @see BlobBatch
* @see BlobBatchClientBuilder
*/
@ServiceClient(builder = BlobBatchClientBuilder.class)
public final class BlobBatchClient {
private final BlobBatchAsyncClient client;
BlobBatchClient(BlobBatchAsyncClient client) {
this.client = client;
}
BlobBatchAsyncClient getClient() {
return client;
}
/**
* Gets a {@link BlobBatch} used to configure a batching operation to send to Azure Storage blobs.
*
* @return a new {@link BlobBatch} instance.
*/
public BlobBatch getBlobBatch() {
return client.getBlobBatch();
}
/**
* Submits a batch operation.
*
* If any request in a batch fails this will throw a {@link 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());
* }
*
*
*
* @param batch Batch to submit.
* @throws BlobStorageException If the batch request is malformed.
* @throws BlobBatchStorageException If any request in the {@link BlobBatch} failed.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public void submitBatch(BlobBatch batch) {
submitBatchWithResponse(batch, true, null, Context.NONE);
}
/**
* Submits a batch operation.
*
* If {@code throwOnAnyFailure} is {@code true} a {@link 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());
* }
*
*
*
* @param batch Batch to submit.
* @param throwOnAnyFailure Flag to indicate if an exception should be thrown if any request in the batch fails.
* @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return A response only containing header and status code information, used to indicate that the batch operation
* has completed.
* @throws RuntimeException If the {@code timeout} duration completes before a response is returned.
* @throws BlobStorageException If the batch request is malformed.
* @throws BlobBatchStorageException If {@code throwOnAnyFailure} is {@code true} and any request in the
* {@link BlobBatch} failed.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response submitBatchWithResponse(BlobBatch batch, boolean throwOnAnyFailure, Duration timeout,
Context context) {
return StorageImplUtils.blockWithOptionalTimeout(client.submitBatchWithResponse(batch,
throwOnAnyFailure, context), timeout);
}
/**
* 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());
* }
*
*
*
* @param blobUrls Urls of the blobs to delete. Blob names must be encoded to UTF-8.
* @param deleteOptions The deletion option for all blobs.
* @return The status of each delete operation.
* @throws BlobStorageException If the batch request is malformed.
* @throws BlobBatchStorageException If any of the delete operations fail.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable> deleteBlobs(List blobUrls, DeleteSnapshotsOptionType deleteOptions) {
return new PagedIterable<>(client.deleteBlobs(blobUrls, 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, 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());
* }
*
*
*
* @param blobUrls Urls of the blobs to delete. Blob names must be encoded to UTF-8.
* @param deleteOptions The deletion option for all blobs.
* @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return The status of each delete operation.
* @throws RuntimeException If the {@code timeout} duration completes before a response is returned.
* @throws BlobStorageException If the batch request is malformed.
* @throws BlobBatchStorageException If any of the delete operations fail.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable> deleteBlobs(List blobUrls, DeleteSnapshotsOptionType deleteOptions,
Duration timeout, Context context) {
return new PagedIterable<>(client.deleteBlobsWithTimeout(blobUrls, deleteOptions, timeout, 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).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());
* }
*
*
*
* @param blobUrls Urls of the blobs to set their access tier. Blob names must be encoded to UTF-8.
* @param accessTier {@link AccessTier} to set on each blob.
* @return The status of each set tier operation.
* @throws BlobStorageException If the batch request is malformed.
* @throws BlobBatchStorageException If any of the set tier operations fail.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable> setBlobsAccessTier(List blobUrls, AccessTier accessTier) {
return new PagedIterable<>(client.setBlobsAccessTier(blobUrls, 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, 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());
* }
*
*
*
* @param blobUrls Urls of the blobs to set their access tier. Blob names must be encoded to UTF-8.
* @param accessTier {@link AccessTier} to set on each blob.
* @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return The status of each set tier operation.
* @throws RuntimeException If the {@code timeout} duration completes before a response is returned.
* @throws BlobStorageException If the batch request is malformed.
* @throws BlobBatchStorageException If any of the set tier operations fail.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable> setBlobsAccessTier(List blobUrls, AccessTier accessTier,
Duration timeout, Context context) {
return new PagedIterable<>(client.setBlobsAccessTierWithTimeout(blobUrls, accessTier, timeout, context));
}
}