Search in sources :

Example 66 with Blob

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

the class ITBucketSnippets method testBucket.

@Test
public void testBucket() throws InterruptedException {
    assertTrue(bucketSnippets.exists());
    Bucket bucket = bucketSnippets.reload();
    assertNotNull(bucket);
    Bucket updatedBucket = bucketSnippets.update();
    assertTrue(updatedBucket.versioningEnabled());
    Blob blob1 = bucketSnippets.createBlobFromByteArray(BLOB1);
    assertNotNull(blob1);
    Blob blob2 = bucketSnippets.createBlobFromByteArrayWithContentType(BLOB2);
    assertNotNull(blob2);
    Blob blob3 = bucketSnippets.createBlobFromInputStream(BLOB3);
    assertNotNull(blob3);
    Blob blob4 = bucketSnippets.createBlobFromInputStreamWithContentType(BLOB4);
    assertNotNull(blob4);
    Set<Blob> blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll());
    while (blobSet.size() < 4) {
        Thread.sleep(500);
        blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll());
    }
    assertTrue(blobSet.contains(blob1));
    assertTrue(blobSet.contains(blob2));
    assertTrue(blobSet.contains(blob3));
    assertTrue(blobSet.contains(blob4));
    blob1 = bucketSnippets.getBlob(BLOB1, blob1.getGeneration());
    assertEquals(BLOB1, blob1.getName());
    List<Blob> blobs = bucketSnippets.getBlobFromStrings(BLOB2, BLOB3);
    assertEquals(BLOB2, blobs.get(0).getName());
    assertEquals(BLOB3, blobs.get(1).getName());
    blobs = bucketSnippets.getBlobFromStringIterable(BLOB3, BLOB4);
    assertEquals(BLOB3, blobs.get(0).getName());
    assertEquals(BLOB4, blobs.get(1).getName());
    // test ACLs
    assertNull(bucketSnippets.getAcl());
    assertNotNull(bucketSnippets.createAcl());
    Acl updatedAcl = bucketSnippets.updateAcl();
    assertEquals(Role.OWNER, updatedAcl.getRole());
    Set<Acl> acls = Sets.newHashSet(bucketSnippets.listAcls());
    assertTrue(acls.contains(updatedAcl));
    assertTrue(bucketSnippets.deleteAcl());
    assertNull(bucketSnippets.getAcl());
    // test default ACLs
    assertNull(bucketSnippets.getDefaultAcl());
    assertNotNull(bucketSnippets.createDefaultAcl());
    updatedAcl = bucketSnippets.updateDefaultAcl();
    assertEquals(Role.OWNER, updatedAcl.getRole());
    acls = Sets.newHashSet(bucketSnippets.listDefaultAcls());
    assertTrue(acls.contains(updatedAcl));
    assertTrue(bucketSnippets.deleteDefaultAcl());
    assertNull(bucketSnippets.getDefaultAcl());
    thrown.expect(StorageException.class);
    assertTrue(bucketSnippets.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) Bucket(com.google.cloud.storage.Bucket) Acl(com.google.cloud.storage.Acl) Test(org.junit.Test)

Example 67 with Blob

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

the class ITStorageSnippets method testReadWriteAndSignUrl.

@Test
public void testReadWriteAndSignUrl() throws IOException {
    String blobName = "text-read-write-sign-url";
    byte[] content = "Hello, World!".getBytes(UTF_8);
    Blob blob = storage.create(BlobInfo.newBuilder(BUCKET, blobName).build(), content);
    assertArrayEquals(content, storageSnippets.readBlobFromId(BUCKET, blobName, blob.getGeneration()));
    assertArrayEquals(content, storageSnippets.readBlobFromStringsWithGeneration(BUCKET, blobName, blob.getGeneration()));
    storageSnippets.readerFromId(BUCKET, blobName);
    storageSnippets.readerFromStrings(BUCKET, blobName);
    storageSnippets.writer(BUCKET, blobName);
    URL signedUrl = storageSnippets.signUrl(BUCKET, blobName);
    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 = storageSnippets.signUrlWithSigner(BUCKET, blobName, System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
    connection = signedUrl.openConnection();
    try (InputStream responseStream = connection.getInputStream()) {
        assertEquals(content.length, responseStream.read(readBytes));
        assertArrayEquals(content, readBytes);
    }
    blob.delete();
}
Also used : Blob(com.google.cloud.storage.Blob) InputStream(java.io.InputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 68 with Blob

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

Example 69 with Blob

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

the class ITStorageSnippets method testCreateBlobFromInputStream.

@Test
public void testCreateBlobFromInputStream() {
    Blob blob = storageSnippets.createBlobFromInputStream(BUCKET, "test-create-blob-from-input-stream");
    assertNotNull(blob);
    assertTrue(storageSnippets.deleteBlobFromIdWithGeneration(BUCKET, "test-create-blob-from-input-stream", blob.getGeneration()));
}
Also used : Blob(com.google.cloud.storage.Blob) Test(org.junit.Test)

Example 70 with Blob

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

the class UpdateBlob method main.

public static void main(String... args) throws IOException {
    Storage storage = StorageOptions.getDefaultInstance().getService();
    BlobId blobId = BlobId.of("bucket", "blob_name");
    Blob blob = storage.get(blobId);
    if (blob != null) {
        byte[] prevContent = blob.getContent();
        System.out.println(new String(prevContent, UTF_8));
        WritableByteChannel channel = blob.writer();
        channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
        channel.close();
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) WritableByteChannel(java.nio.channels.WritableByteChannel) BlobId(com.google.cloud.storage.BlobId)

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