Search in sources :

Example 21 with StorageException

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

the class CloudStorageReadChannelTest method testReadRetry.

@Test
public void testReadRetry() throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(1);
    when(gcsChannel.read(eq(buffer))).thenThrow(new StorageException(new IOException("outer", new IOException("Connection closed prematurely: bytesRead = 33554432, Content-Length = 41943040")))).thenReturn(1);
    assertThat(chan.position()).isEqualTo(0L);
    assertThat(chan.read(buffer)).isEqualTo(1);
    assertThat(chan.position()).isEqualTo(1L);
    verify(gcsChannel, times(2)).read(any(ByteBuffer.class));
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 22 with StorageException

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

the class ITStorageTest method testCreateBlobFail.

@Test
public void testCreateBlobFail() {
    String blobName = "test-create-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    BlobInfo wrongGenerationBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build();
    try {
        storage.create(wrongGenerationBlob, BLOB_BYTE_CONTENT, Storage.BlobTargetOption.generationMatch());
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(remoteBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 23 with StorageException

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

the class ITStorageTest method testUpdateBlobFail.

@Test
public void testUpdateBlobFail() {
    String blobName = "test-update-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    BlobInfo wrongGenerationBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).setContentType(CONTENT_TYPE).build();
    try {
        storage.update(wrongGenerationBlob, Storage.BlobTargetOption.generationMatch());
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(remoteBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 24 with StorageException

use of com.google.cloud.storage.StorageException 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 25 with StorageException

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

the class ITStorageSnippets method testBlob.

@Test
public void testBlob() throws InterruptedException {
    String blobName = "directory/test-blob";
    Blob blob = storageSnippets.createBlob(BUCKET, blobName);
    assertNotNull(blob);
    blob = storageSnippets.getBlobFromId(BUCKET, blobName);
    assertNotNull(blob);
    blob = storageSnippets.updateBlob(BUCKET, blobName);
    assertNotNull(blob);
    blob = storageSnippets.updateBlobWithMetageneration(BUCKET, blobName);
    assertNotNull(blob);
    Blob copiedBlob = storageSnippets.copyBlob(BUCKET, blobName, "directory/copy-blob");
    assertNotNull(copiedBlob);
    Page<Blob> blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
    while (Iterators.size(blobs.iterateAll().iterator()) < 2) {
        Thread.sleep(500);
        blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
    }
    Set<String> blobNames = new HashSet<>();
    Iterator<Blob> blobIterator = blobs.iterateAll().iterator();
    while (blobIterator.hasNext()) {
        blobNames.add(blobIterator.next().getName());
    }
    assertTrue(blobNames.contains(blobName));
    assertTrue(blobNames.contains("directory/copy-blob"));
    try {
        storageSnippets.getBlobFromStringsWithMetageneration(BUCKET, blobName, -1);
        fail("Expected StorageException to be thrown");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(storageSnippets.deleteBlob(BUCKET, blobName));
    copiedBlob.delete();
}
Also used : Blob(com.google.cloud.storage.Blob) StorageException(com.google.cloud.storage.StorageException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

StorageException (com.google.cloud.storage.StorageException)29 Test (org.junit.Test)20 BlobInfo (com.google.cloud.storage.BlobInfo)16 Blob (com.google.cloud.storage.Blob)15 BlobId (com.google.cloud.storage.BlobId)8 IOException (java.io.IOException)5 Acl (com.google.cloud.storage.Acl)4 ByteBuffer (java.nio.ByteBuffer)4 Storage (com.google.cloud.storage.Storage)3 StorageBatch (com.google.cloud.storage.StorageBatch)3 ReadChannel (com.google.cloud.ReadChannel)2 WriteChannel (com.google.cloud.WriteChannel)2 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpResponse (com.google.api.client.http.HttpResponse)1 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)1 Get (com.google.api.services.storage.Storage.Objects.Get)1 BatchResult (com.google.cloud.BatchResult)1 User (com.google.cloud.storage.Acl.User)1