|
| 1 | +package com.microsoft.azure.storage.blob.gettingstarted; |
| 2 | + |
| 3 | +import com.microsoft.azure.storage.BatchException; |
| 4 | +import com.microsoft.azure.storage.CloudStorageAccount; |
| 5 | +import com.microsoft.azure.storage.OperationContext; |
| 6 | +import com.microsoft.azure.storage.StorageException; |
| 7 | +import com.microsoft.azure.storage.blob.*; |
| 8 | +import com.microsoft.azure.storage.util.Utility; |
| 9 | + |
| 10 | +import java.net.InetSocketAddress; |
| 11 | +import java.net.Proxy; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.security.InvalidKeyException; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.UUID; |
| 18 | + |
| 19 | +public class BlobBatch { |
| 20 | + private static final String SAMPLE_NAME = "BlobBatch"; |
| 21 | + |
| 22 | + public static void main(String[] args) throws URISyntaxException, InvalidKeyException, StorageException { |
| 23 | + Utility.printSampleStartInfo(SAMPLE_NAME); |
| 24 | + |
| 25 | + // Setup the cloud storage account. |
| 26 | + CloudStorageAccount account = CloudStorageAccount.parse(Utility.storageConnectionString); |
| 27 | + |
| 28 | + // Create a blob service client |
| 29 | + CloudBlobClient blobClient = account.createCloudBlobClient(); |
| 30 | + |
| 31 | + // Get a reference to a container |
| 32 | + // The container name must be lower case |
| 33 | + // Append a random UUID to the end of the container name so that |
| 34 | + // this sample can be run more than once in quick succession. |
| 35 | + CloudBlobContainer container = blobClient.getContainerReference("blobbasicscontainer" |
| 36 | + + UUID.randomUUID().toString().replace("-", "")); |
| 37 | + |
| 38 | + try { |
| 39 | + // create the container |
| 40 | + container.create(); |
| 41 | + |
| 42 | + // add some blobs |
| 43 | + final int NUM_BLOBS = 3; |
| 44 | + List<CloudBlob> blobs = new ArrayList<>(); |
| 45 | + for (int i = 0; i < NUM_BLOBS; i++) { |
| 46 | + // blobs have more relaxed naming constraints than containers |
| 47 | + CloudBlockBlob blob = container.getBlockBlobReference("blobtobatchdelete" + UUID.randomUUID()); |
| 48 | + blob.uploadText("Sample data."); |
| 49 | + blobs.add(blob); |
| 50 | + |
| 51 | + System.out.println(String.format("Created blob %s", blob.getName())); |
| 52 | + } |
| 53 | + |
| 54 | + // create a blob object on client, but don't upload it to the service, to cause a bad request in the batch |
| 55 | + // comment this line out to get a success state |
| 56 | + blobs.add(container.getBlockBlobReference("blobtobatchdelete" + UUID.randomUUID())); |
| 57 | + |
| 58 | + // assemble batch request, in this case: delete |
| 59 | + BlobDeleteBatchOperation batch = new BlobDeleteBatchOperation(); |
| 60 | + for (CloudBlob blob : blobs) { |
| 61 | + batch.addSubOperation(blob); |
| 62 | + |
| 63 | + System.out.println(String.format("Added delete request for blob %s", blob.getName())); |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + // send the batch request |
| 68 | + Map<CloudBlob, Void> batchResponse = container.getServiceClient().executeBatch(batch); |
| 69 | + |
| 70 | + // for each blob, view the result |
| 71 | + // Delete returns void, so its batch response maps to Void, but other requests may return data to process |
| 72 | + for (CloudBlob blob : blobs) { |
| 73 | + Void result = batchResponse.get(blob); |
| 74 | + |
| 75 | + System.out.println(String.format("Result from deleting blob %s: %s", blob.getName(), result)); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // when one or more requests in the batch fail |
| 80 | + catch (BatchException e) { |
| 81 | + /* |
| 82 | + * Subclasses of java.lang.Throwable cannot be generic, so the collections of successful and |
| 83 | + * unsuccessful responses cannot be typed correctly on the class. However, the types will directly |
| 84 | + * relate to the batched operation. Meaning: for a BlobBatchOperation<P, R>, |
| 85 | + * e.getSuccessfulResponses() can be safely cast to Map<P, R>, and e.getExceptions() can be |
| 86 | + * safely cast to Map<P, StorageException>. BlobDeleteBatchOperation extends |
| 87 | + * BlobBatchOperation<CloudBlob, Void> so we cast as follows. |
| 88 | + */ |
| 89 | + Map<CloudBlob, Void> successes = (Map<CloudBlob, Void>) e.getSuccessfulResponses(); |
| 90 | + Map<CloudBlob, StorageException> failures = (Map<CloudBlob, StorageException>) e.getExceptions(); |
| 91 | + |
| 92 | + // handle successes |
| 93 | + for (Map.Entry<CloudBlob, Void> entry : successes.entrySet()) { |
| 94 | + System.out.println(String.format("Result from deleting blob %s: %s", |
| 95 | + entry.getKey().getName(), entry.getValue())); |
| 96 | + } |
| 97 | + |
| 98 | + // handle failures |
| 99 | + for (Map.Entry<CloudBlob, StorageException> entry : failures.entrySet()) { |
| 100 | + System.out.println(String.format("Failed to delete blob %s. Exception: %s", |
| 101 | + entry.getKey().getName(), entry.getValue().getErrorCode())); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + catch (Throwable t) { |
| 106 | + Utility.printException(t); |
| 107 | + } |
| 108 | + finally { |
| 109 | + // clean up sample |
| 110 | + container.deleteIfExists(); |
| 111 | + } |
| 112 | + |
| 113 | + Utility.printSampleCompleteInfo(SAMPLE_NAME); |
| 114 | + } |
| 115 | +} |
0 commit comments