Search in sources :

Example 1 with StorageBatch

use of com.google.cloud.storage.StorageBatch in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testBatchRequestFail.

@Test
public void testBatchRequestFail() {
    String blobName = "test-batch-request-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    BlobInfo updatedBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build();
    StorageBatch batch = storage.batch();
    StorageBatchResult<Blob> updateResult = batch.update(updatedBlob, Storage.BlobTargetOption.generationMatch());
    StorageBatchResult<Boolean> deleteResult1 = batch.delete(BUCKET, blobName, Storage.BlobSourceOption.generationMatch(-1L));
    StorageBatchResult<Boolean> deleteResult2 = batch.delete(BlobId.of(BUCKET, blobName, -1L));
    StorageBatchResult<Blob> getResult1 = batch.get(BUCKET, blobName, Storage.BlobGetOption.generationMatch(-1L));
    StorageBatchResult<Blob> getResult2 = batch.get(BlobId.of(BUCKET, blobName, -1L));
    batch.submit();
    try {
        updateResult.get();
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        deleteResult1.get();
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    assertFalse(deleteResult2.get());
    try {
        getResult1.get();
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    assertNull(getResult2.get());
}
Also used : Blob(com.google.cloud.storage.Blob) StorageBatch(com.google.cloud.storage.StorageBatch) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 2 with StorageBatch

use of com.google.cloud.storage.StorageBatch in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testBatchRequest.

@Test
public void testBatchRequest() {
    String sourceBlobName1 = "test-batch-request-blob-1";
    String sourceBlobName2 = "test-batch-request-blob-2";
    BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
    BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
    assertNotNull(storage.create(sourceBlob1));
    assertNotNull(storage.create(sourceBlob2));
    // Batch update request
    BlobInfo updatedBlob1 = sourceBlob1.toBuilder().setContentType(CONTENT_TYPE).build();
    BlobInfo updatedBlob2 = sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build();
    StorageBatch updateBatch = storage.batch();
    StorageBatchResult<Blob> updateResult1 = updateBatch.update(updatedBlob1);
    StorageBatchResult<Blob> updateResult2 = updateBatch.update(updatedBlob2);
    updateBatch.submit();
    Blob remoteUpdatedBlob1 = updateResult1.get();
    Blob remoteUpdatedBlob2 = updateResult2.get();
    assertEquals(sourceBlob1.getBucket(), remoteUpdatedBlob1.getBucket());
    assertEquals(sourceBlob1.getName(), remoteUpdatedBlob1.getName());
    assertEquals(sourceBlob2.getBucket(), remoteUpdatedBlob2.getBucket());
    assertEquals(sourceBlob2.getName(), remoteUpdatedBlob2.getName());
    assertEquals(updatedBlob1.getContentType(), remoteUpdatedBlob1.getContentType());
    assertEquals(updatedBlob2.getContentType(), remoteUpdatedBlob2.getContentType());
    // Batch get request
    StorageBatch getBatch = storage.batch();
    StorageBatchResult<Blob> getResult1 = getBatch.get(BUCKET, sourceBlobName1);
    StorageBatchResult<Blob> getResult2 = getBatch.get(BUCKET, sourceBlobName2);
    getBatch.submit();
    Blob remoteBlob1 = getResult1.get();
    Blob remoteBlob2 = getResult2.get();
    assertEquals(remoteUpdatedBlob1, remoteBlob1);
    assertEquals(remoteUpdatedBlob2, remoteBlob2);
    // Batch delete request
    StorageBatch deleteBatch = storage.batch();
    StorageBatchResult<Boolean> deleteResult1 = deleteBatch.delete(BUCKET, sourceBlobName1);
    StorageBatchResult<Boolean> deleteResult2 = deleteBatch.delete(BUCKET, sourceBlobName2);
    deleteBatch.submit();
    assertTrue(deleteResult1.get());
    assertTrue(deleteResult2.get());
}
Also used : Blob(com.google.cloud.storage.Blob) StorageBatch(com.google.cloud.storage.StorageBatch) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 3 with StorageBatch

use of com.google.cloud.storage.StorageBatch 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());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) StorageBatchResult(com.google.cloud.storage.StorageBatchResult) StorageBatch(com.google.cloud.storage.StorageBatch) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 4 with StorageBatch

use of com.google.cloud.storage.StorageBatch 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;
}
Also used : Blob(com.google.cloud.storage.Blob) StorageBatch(com.google.cloud.storage.StorageBatch) StorageBatchResult(com.google.cloud.storage.StorageBatchResult) BatchResult(com.google.cloud.BatchResult) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException)

Aggregations

Blob (com.google.cloud.storage.Blob)4 StorageBatch (com.google.cloud.storage.StorageBatch)4 BlobInfo (com.google.cloud.storage.BlobInfo)3 StorageException (com.google.cloud.storage.StorageException)3 Test (org.junit.Test)3 BlobId (com.google.cloud.storage.BlobId)2 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 BatchResult (com.google.cloud.BatchResult)1