Search in sources :

Example 11 with Blob

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

the class StorageSnippets method copyBlobInChunks.

/**
   * Example of copying a blob in chunks.
   */
// [TARGET copy(CopyRequest)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "copy_blob_name"]
public Blob copyBlobInChunks(String bucketName, String blobName, String copyBlobName) {
    // [START copyBlobInChunks]
    CopyRequest request = CopyRequest.newBuilder().setSource(BlobId.of(bucketName, blobName)).setTarget(BlobId.of(bucketName, copyBlobName)).build();
    CopyWriter copyWriter = storage.copy(request);
    while (!copyWriter.isDone()) {
        copyWriter.copyChunk();
    }
    Blob blob = copyWriter.getResult();
    // [END copyBlobInChunks]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) CopyRequest(com.google.cloud.storage.Storage.CopyRequest) CopyWriter(com.google.cloud.storage.CopyWriter)

Example 12 with Blob

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

the class ITBlobSnippets method testMoveBlob.

@Test
public void testMoveBlob() throws IOException {
    BlobSnippets blobSnippets = new BlobSnippets(blob);
    Blob movedBlob = blobSnippets.moveTo(BUCKET, "moveBlob");
    assertNotNull(movedBlob);
    // Assert that the destination blob exists
    Iterator<Blob> blobs = storage.list(BUCKET).iterateAll().iterator();
    Blob moveBlob = blobs.next();
    assertEquals(BUCKET, moveBlob.getBucket());
    assertEquals("moveBlob", moveBlob.getName());
    // Assert that the old blob doesn't exist
    assertFalse(blobs.hasNext());
}
Also used : Blob(com.google.cloud.storage.Blob) Test(org.junit.Test)

Example 13 with Blob

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

the class ITBlobSnippets method testBlob.

@Test
public void testBlob() throws IOException {
    BlobSnippets blobSnippets = new BlobSnippets(blob);
    assertTrue(blobSnippets.exists());
    assertArrayEquals(EMPTY_CONTENT, blobSnippets.getContent());
    try {
        assertNotNull(blobSnippets.reload());
        fail("Expected StorageException to be thrown");
    } catch (StorageException ex) {
    // expected
    }
    Blob updatedBlob = blobSnippets.update();
    assertEquals(ImmutableMap.of("key", "value"), updatedBlob.getMetadata());
    Blob copiedBlob = blobSnippets.copyToStrings(BUCKET, "copyBlob");
    assertNotNull(copiedBlob);
    copiedBlob.delete();
    copiedBlob = blobSnippets.copyToId(BUCKET, "copyBlob");
    assertNotNull(copiedBlob);
    copiedBlob.delete();
    copiedBlob = blobSnippets.copyToBucket(BUCKET);
    assertNotNull(copiedBlob);
    copiedBlob.delete();
    blobSnippets.reload();
    blobSnippets.writer();
    URL signedUrl = blobSnippets.signUrl();
    URLConnection connection = signedUrl.openConnection();
    byte[] readBytes = new byte[CONTENT.length];
    try (InputStream responseStream = connection.getInputStream()) {
        assertEquals(CONTENT.length, responseStream.read(readBytes));
        assertArrayEquals(CONTENT, readBytes);
    }
    signedUrl = blobSnippets.signUrlWithSigner(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
    connection = signedUrl.openConnection();
    try (InputStream responseStream = connection.getInputStream()) {
        assertEquals(CONTENT.length, responseStream.read(readBytes));
        assertArrayEquals(CONTENT, readBytes);
    }
    assertFalse(blobSnippets.delete());
    blobSnippets = new BlobSnippets(storage.get(blob.getBucket(), blob.getName()));
    byte[] subcontent = blobSnippets.readContentRange(1, 8);
    assertArrayEquals("ello, W".getBytes(UTF_8), subcontent);
    assertNull(blobSnippets.getAcl());
    assertNotNull(blobSnippets.createAcl());
    Acl updatedAcl = blobSnippets.updateAcl();
    assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
    Set<Acl> acls = Sets.newHashSet(blobSnippets.listAcls());
    assertTrue(acls.contains(updatedAcl));
    assertTrue(blobSnippets.deleteAcl());
    assertNull(blobSnippets.getAcl());
    storage.delete(BlobId.of(BUCKET, BLOB));
}
Also used : Blob(com.google.cloud.storage.Blob) InputStream(java.io.InputStream) Acl(com.google.cloud.storage.Acl) StorageException(com.google.cloud.storage.StorageException) URL(java.net.URL) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 14 with Blob

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

the class ITStorageSnippets method testBlobAcl.

@Test
public void testBlobAcl() {
    String blobName = "test-blob-acl";
    BlobId blobId = BlobId.of(BUCKET, "test-blob-acl");
    BlobInfo blob = BlobInfo.newBuilder(blobId).build();
    Blob createdBlob = storage.create(blob);
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertNotNull(storageSnippets.createBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    Acl updatedAcl = storageSnippets.updateBlobAcl(BUCKET, blobName, createdBlob.getGeneration());
    assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
    Set<Acl> acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(acls.contains(updatedAcl));
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL));
    storage.createAcl(BlobId.of(BUCKET, blobName), Acl.of(new User(USER_EMAIL), Role.READER));
    Acl userAcl = storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL);
    assertNotNull(userAcl);
    assertEquals(USER_EMAIL, ((User) userAcl.getEntity()).getEmail());
    updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration());
    assertEquals(Acl.Role.READER, updatedAcl.getRole());
    assertEquals(User.ofAllUsers(), updatedAcl.getEntity());
    acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(acls.contains(updatedAcl));
    assertNotNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    // test non-existing blob
    String nonExistingBlob = "test-blob-acl";
    assertNull(storageSnippets.getBlobAcl(BUCKET, nonExistingBlob, -1L));
    assertFalse(storageSnippets.deleteBlobAcl(BUCKET, nonExistingBlob, -1L));
    try {
        storageSnippets.createBlobAcl(BUCKET, nonExistingBlob, -1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storageSnippets.updateBlobAcl(BUCKET, nonExistingBlob, -1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storageSnippets.listBlobAcls(BUCKET, nonExistingBlob, -1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
}
Also used : Blob(com.google.cloud.storage.Blob) User(com.google.cloud.storage.Acl.User) BlobInfo(com.google.cloud.storage.BlobInfo) Acl(com.google.cloud.storage.Acl) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 15 with Blob

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

the class ITStorageSnippets method testCreateUpdateEncryptedBlob.

@Test
public void testCreateUpdateEncryptedBlob() throws InterruptedException {
    // Note: DO NOT put your encryption key in your code, like it is here. Store it somewhere safe,
    // and read it in when you need it. This key is just here to make the code easier to read.
    String encryptionKey1 = "0mMWhFvQOdS4AmxRpo8SJxXn5MjFhbz7DkKBUdUIef8=";
    String blobName = "encrypted-blob";
    Blob blob = storageSnippets.createEncryptedBlob(BUCKET, blobName, encryptionKey1);
    assertNotNull(blob);
    assertEquals("text/plain", blob.getContentType());
    byte[] encryptedContent = storageSnippets.readEncryptedBlob(BUCKET, blobName, encryptionKey1);
    assertEquals("Hello, World!", new String(encryptedContent));
    blob = storageSnippets.getBlobFromId(BUCKET, blobName);
    assertEquals("text/plain", blob.getContentType());
    String encryptionKey2 = "wnxMO0w+dmxribu7rICJ+Q2ES9TLpFRIDy3/L7HN5ZA=";
    blob = storageSnippets.rotateBlobEncryptionKey(BUCKET, blobName, encryptionKey1, encryptionKey2);
    assertNotNull(blob);
    encryptedContent = storageSnippets.readEncryptedBlob(BUCKET, blobName, encryptionKey2);
    assertEquals("Hello, World!", new String(encryptedContent));
    blob = storageSnippets.getBlobFromId(BUCKET, blobName);
    assertEquals("text/plain", blob.getContentType());
}
Also used : Blob(com.google.cloud.storage.Blob) Test(org.junit.Test)

Aggregations

Blob (com.google.cloud.storage.Blob)77 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)15 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