Search in sources :

Example 16 with Blob

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

the class ITStorageSnippets method testCreateCopyAndGetBlob.

@Test
public void testCreateCopyAndGetBlob() {
    String blobName = "test-create-copy-get-blob";
    Blob blob = storageSnippets.createBlobFromByteArray(BUCKET, blobName);
    assertNotNull(blob);
    Blob copiedBlob = storageSnippets.copyBlobInChunks(BUCKET, blobName, "copy-blob");
    assertNotNull(copiedBlob);
    try {
        storageSnippets.getBlobFromIdWithMetageneration(BUCKET, blobName, -1);
        fail("Expected StorageException to be thrown");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(storageSnippets.deleteBlobFromIdWithGeneration(BUCKET, blobName, blob.getGeneration()));
    copiedBlob.delete();
}
Also used : Blob(com.google.cloud.storage.Blob) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 17 with Blob

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

the class ITStorageTest method testGetSignedUrl.

@Test
public void testGetSignedUrl() throws IOException {
    String blobName = "test-get-signed-url-blob/with/slashes/and?special=!#$&'()*+,:;=?@[]";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT);
    assertNotNull(remoteBlob);
    URL url = storage.signUrl(blob, 1, TimeUnit.HOURS);
    URLConnection connection = url.openConnection();
    byte[] readBytes = new byte[BLOB_BYTE_CONTENT.length];
    try (InputStream responseStream = connection.getInputStream()) {
        assertEquals(BLOB_BYTE_CONTENT.length, responseStream.read(readBytes));
        assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
        assertTrue(remoteBlob.delete());
    }
}
Also used : Blob(com.google.cloud.storage.Blob) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BlobInfo(com.google.cloud.storage.BlobInfo) URL(java.net.URL) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 18 with Blob

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

the class ITStorageTest method testCreateBlobStream.

@Test
public void testCreateBlobStream() {
    String blobName = "test-create-blob-stream";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build();
    ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8));
    Blob remoteBlob = storage.create(blob, stream);
    assertNotNull(remoteBlob);
    assertEquals(blob.getBucket(), remoteBlob.getBucket());
    assertEquals(blob.getName(), remoteBlob.getName());
    assertEquals(blob.getContentType(), remoteBlob.getContentType());
    byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
    assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8));
    assertTrue(remoteBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 19 with Blob

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

the class ITStorageTest method testCopyBlobFail.

@Test
public void testCopyBlobFail() {
    String sourceBlobName = "test-copy-blob-source-fail";
    BlobId source = BlobId.of(BUCKET, sourceBlobName, -1L);
    Blob remoteSourceBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT);
    assertNotNull(remoteSourceBlob);
    String targetBlobName = "test-copy-blob-target-fail";
    BlobInfo target = BlobInfo.newBuilder(BUCKET, targetBlobName).setContentType(CONTENT_TYPE).build();
    Storage.CopyRequest req = Storage.CopyRequest.newBuilder().setSource(BUCKET, sourceBlobName).setSourceOptions(Storage.BlobSourceOption.generationMatch(-1L)).setTarget(target).build();
    try {
        storage.copy(req);
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    Storage.CopyRequest req2 = Storage.CopyRequest.newBuilder().setSource(source).setSourceOptions(Storage.BlobSourceOption.generationMatch()).setTarget(target).build();
    try {
        storage.copy(req2);
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(remoteSourceBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 20 with Blob

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

the class ITStorageTest method testCopyBlobNoContentType.

@Test
public void testCopyBlobNoContentType() {
    String sourceBlobName = "test-copy-blob-no-content-type-source";
    BlobId source = BlobId.of(BUCKET, sourceBlobName);
    Blob remoteSourceBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT);
    assertNotNull(remoteSourceBlob);
    String targetBlobName = "test-copy-blob-no-content-type-target";
    ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
    BlobInfo target = BlobInfo.newBuilder(BUCKET, targetBlobName).setMetadata(metadata).build();
    Storage.CopyRequest req = Storage.CopyRequest.of(source, target);
    CopyWriter copyWriter = storage.copy(req);
    assertEquals(BUCKET, copyWriter.getResult().getBucket());
    assertEquals(targetBlobName, copyWriter.getResult().getName());
    assertNull(copyWriter.getResult().getContentType());
    assertEquals(metadata, copyWriter.getResult().getMetadata());
    assertTrue(copyWriter.isDone());
    assertTrue(remoteSourceBlob.delete());
    assertTrue(storage.delete(BUCKET, targetBlobName));
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId) CopyWriter(com.google.cloud.storage.CopyWriter) 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