Search in sources :

Example 31 with Blob

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

the class ITStorageTest method testGetBlobEmptySelectedFields.

@Test
public void testGetBlobEmptySelectedFields() {
    String blobName = "test-get-empty-selected-fields-blob";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build();
    assertNotNull(storage.create(blob));
    Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields());
    assertEquals(blob.getBlobId(), remoteBlob.getBlobId());
    assertNull(remoteBlob.getContentType());
    assertTrue(remoteBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 32 with Blob

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

the class ITStorageTest method testDeleteBlobFail.

@Test
public void testDeleteBlobFail() {
    String blobName = "test-delete-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    try {
        storage.delete(BUCKET, blob.getName(), Storage.BlobSourceOption.generationMatch(-1L));
        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 33 with Blob

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

the class ITStorageTest method testUpdateBlob.

@Test
public void testUpdateBlob() {
    String blobName = "test-update-blob";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    Blob updatedBlob = remoteBlob.toBuilder().setContentType(CONTENT_TYPE).build().update();
    assertNotNull(updatedBlob);
    assertEquals(blob.getName(), updatedBlob.getName());
    assertEquals(blob.getBucket(), updatedBlob.getBucket());
    assertEquals(CONTENT_TYPE, updatedBlob.getContentType());
    assertTrue(updatedBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 34 with Blob

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

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

the class ITStorageTest method testGetBlobSelectedFields.

@Test
public void testGetBlobSelectedFields() {
    String blobName = "test-get-selected-fields-blob";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).setMetadata(ImmutableMap.of("k", "v")).build();
    assertNotNull(storage.create(blob));
    Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields(BlobField.METADATA));
    assertEquals(blob.getBlobId(), remoteBlob.getBlobId());
    assertEquals(ImmutableMap.of("k", "v"), remoteBlob.getMetadata());
    assertNull(remoteBlob.getContentType());
    assertTrue(remoteBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Aggregations

Blob (com.google.cloud.storage.Blob)80 BlobInfo (com.google.cloud.storage.BlobInfo)50 Test (org.junit.Test)50 BlobId (com.google.cloud.storage.BlobId)23 StorageException (com.google.cloud.storage.StorageException)16 Storage (com.google.cloud.storage.Storage)12 CopyWriter (com.google.cloud.storage.CopyWriter)10 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 StorageBatch (com.google.cloud.storage.StorageBatch)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 ReadChannel (com.google.cloud.ReadChannel)3 Acl (com.google.cloud.storage.Acl)3 Bucket (com.google.cloud.storage.Bucket)3 CopyRequest (com.google.cloud.storage.Storage.CopyRequest)3 HashMap (java.util.HashMap)3 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 FileInputStream (java.io.FileInputStream)2 ByteBuffer (java.nio.ByteBuffer)2