Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 94e0d58

Browse files
jaschrep-msftrickle-msft
authored andcommitted
Added samples
1 parent 169afec commit 94e0d58

File tree

4 files changed

+126
-9
lines changed

4 files changed

+126
-9
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/util/Utility.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public final class Utility {
3333
*
3434
* Stores the storage connection string.
3535
*/
36-
public static final String storageConnectionString = "DefaultEndpointsProtocol=https;"
37-
+ "AccountName=[MY_ACCOUNT_NAME];"
38-
+ "AccountKey=[MY_ACCOUNT_KEY]";
36+
public static final String storageConnectionString = "DefaultEndpointsProtocol=http;"
37+
+ "AccountName=jamesschreppler;"
38+
+ "AccountKey=5POtFgqT5vsDYmIUOSsEpoXnbd3iw+mpBiXB4jiMvdqOcprfPX3gZXGU/sGZviLbIIFDMq+5c00w7O2eG0UT6Q==";
3939

4040
/**
4141
* You only need to modify the following values if you want to run the

microsoft-azure-storage/src/com/microsoft/azure/storage/BatchException.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.microsoft.azure.storage;
22

3+
import com.microsoft.azure.storage.core.Utility;
4+
35
import java.io.BufferedReader;
46
import java.io.IOException;
57
import java.io.InputStreamReader;
@@ -29,7 +31,7 @@ public class BatchException extends StorageException {
2931
private final Map<?, StorageException> exceptions;
3032

3133

32-
BatchException(Map<?, ?> successfulResponses, Map<?, BatchSubResponse> failedResponses) {
34+
BatchException(Map<?, ?> successfulResponses, Map<?, BatchSubResponse> failedResponses, OperationContext opContext) {
3335
super("Batch exception", "One ore more requests in a batch operation failed", null);
3436

3537
Map<Object, StorageException> exceptions = new HashMap<>(failedResponses.size());
@@ -45,10 +47,10 @@ public class BatchException extends StorageException {
4547
throw new RuntimeException(e);
4648
}
4749

48-
exceptions.put(
49-
response.getKey(),
50-
new StorageException(response.getValue().getStatusMessage(), builder.toString(),
51-
response.getValue().getStatusCode(), null, null));
50+
StorageException exception = new StorageException(response.getValue().getStatusMessage(), builder.toString(),
51+
response.getValue().getStatusCode(), null, null);
52+
Utility.logHttpError(exception, opContext);
53+
exceptions.put(response.getKey(), exception);
5254
}
5355

5456
this.successfulResponses = Collections.unmodifiableMap(successfulResponses);

microsoft-azure-storage/src/com/microsoft/azure/storage/BatchOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public Map<P, R> postProcessResponse(HttpURLConnection connection, BatchOperatio
137137
sortResponses(parsedResponses, successfulResponses, failedResponses);
138138

139139
if (!failedResponses.isEmpty()) {
140-
throw new BatchException(successfulResponses, failedResponses);
140+
throw new BatchException(successfulResponses, failedResponses, context);
141141
}
142142

143143
return successfulResponses;

0 commit comments

Comments
 (0)