use of com.google.cloud.storage.StorageBatchResult in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testBatchRequestManyOperations.
@Test
public void testBatchRequestManyOperations() {
List<StorageBatchResult<Boolean>> deleteResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE);
List<StorageBatchResult<Blob>> getResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
List<StorageBatchResult<Blob>> updateResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
StorageBatch batch = storage.batch();
for (int i = 0; i < MAX_BATCH_SIZE; i++) {
BlobId blobId = BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i);
deleteResults.add(batch.delete(blobId));
}
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
BlobId blobId = BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i);
getResults.add(batch.get(blobId));
}
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
BlobInfo blob = BlobInfo.newBuilder(BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i)).build();
updateResults.add(batch.update(blob));
}
String sourceBlobName1 = "test-batch-request-many-operations-source-blob-1";
String sourceBlobName2 = "test-batch-request-many-operations-source-blob-2";
BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
assertNotNull(storage.create(sourceBlob1));
assertNotNull(storage.create(sourceBlob2));
BlobInfo updatedBlob2 = sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build();
StorageBatchResult<Blob> getResult = batch.get(BUCKET, sourceBlobName1);
StorageBatchResult<Blob> updateResult = batch.update(updatedBlob2);
batch.submit();
// Check deletes
for (StorageBatchResult<Boolean> failedDeleteResult : deleteResults) {
assertFalse(failedDeleteResult.get());
}
// Check gets
for (StorageBatchResult<Blob> failedGetResult : getResults) {
assertNull(failedGetResult.get());
}
Blob remoteBlob1 = getResult.get();
assertEquals(sourceBlob1.getBucket(), remoteBlob1.getBucket());
assertEquals(sourceBlob1.getName(), remoteBlob1.getName());
// Check updates
for (StorageBatchResult<Blob> failedUpdateResult : updateResults) {
try {
failedUpdateResult.get();
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
}
Blob remoteUpdatedBlob2 = updateResult.get();
assertEquals(sourceBlob2.getBucket(), remoteUpdatedBlob2.getBucket());
assertEquals(sourceBlob2.getName(), remoteUpdatedBlob2.getName());
assertEquals(updatedBlob2.getContentType(), remoteUpdatedBlob2.getContentType());
assertTrue(remoteBlob1.delete());
assertTrue(remoteUpdatedBlob2.delete());
}
use of com.google.cloud.storage.StorageBatchResult in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method batch.
/**
* Example of using a batch request to delete, update and get a blob.
*/
// [TARGET batch()]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name1"]
// [VARIABLE "my_blob_name2"]
public Blob batch(String bucketName, String blobName1, String blobName2) {
// [START batch]
StorageBatch batch = storage.batch();
BlobId firstBlob = BlobId.of(bucketName, blobName1);
BlobId secondBlob = BlobId.of(bucketName, blobName2);
batch.delete(firstBlob).notify(new BatchResult.Callback<Boolean, StorageException>() {
public void success(Boolean result) {
// deleted successfully
}
public void error(StorageException exception) {
// delete failed
}
});
batch.update(BlobInfo.newBuilder(secondBlob).setContentType("text/plain").build());
StorageBatchResult<Blob> result = batch.get(secondBlob);
batch.submit();
// returns get result or throws StorageException
Blob blob = result.get();
// [END batch]
return blob;
}
Aggregations