Search in sources :

Example 31 with BlobInfo

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

the class ITStorageTest method testGetBlobFail.

@Test
public void testGetBlobFail() {
    String blobName = "test-get-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    BlobId wrongGenerationBlob = BlobId.of(BUCKET, blobName);
    try {
        storage.get(wrongGenerationBlob, Storage.BlobGetOption.generationMatch(-1));
        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) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 32 with BlobInfo

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

the class ITStorageTest method testListBlobsSelectedFields.

@Test(timeout = 5000)
public void testListBlobsSelectedFields() throws InterruptedException {
    String[] blobNames = { "test-list-blobs-selected-fields-blob1", "test-list-blobs-selected-fields-blob2" };
    ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
    BlobInfo blob1 = BlobInfo.newBuilder(BUCKET, blobNames[0]).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
    BlobInfo blob2 = BlobInfo.newBuilder(BUCKET, blobNames[1]).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
    Blob remoteBlob1 = storage.create(blob1);
    Blob remoteBlob2 = storage.create(blob2);
    assertNotNull(remoteBlob1);
    assertNotNull(remoteBlob2);
    Page<Blob> page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"), Storage.BlobListOption.fields(BlobField.METADATA));
    // test fails if timeout is reached.
    while (Iterators.size(page.iterateAll().iterator()) != 2) {
        Thread.sleep(500);
        page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"), Storage.BlobListOption.fields(BlobField.METADATA));
    }
    Set<String> blobSet = ImmutableSet.of(blobNames[0], blobNames[1]);
    Iterator<Blob> iterator = page.iterateAll().iterator();
    while (iterator.hasNext()) {
        Blob remoteBlob = iterator.next();
        assertEquals(BUCKET, remoteBlob.getBucket());
        assertTrue(blobSet.contains(remoteBlob.getName()));
        assertEquals(metadata, remoteBlob.getMetadata());
        assertNull(remoteBlob.getContentType());
    }
    assertTrue(remoteBlob1.delete());
    assertTrue(remoteBlob2.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 33 with BlobInfo

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

the class ITStorageTest method testBlobAcl.

@Test
public void testBlobAcl() {
    BlobId blobId = BlobId.of(BUCKET, "test-blob-acl");
    BlobInfo blob = BlobInfo.newBuilder(blobId).build();
    storage.create(blob);
    assertNull(storage.getAcl(blobId, User.ofAllAuthenticatedUsers()));
    Acl acl = Acl.of(User.ofAllAuthenticatedUsers(), Role.READER);
    assertNotNull(storage.createAcl(blobId, acl));
    Acl updatedAcl = storage.updateAcl(blobId, acl.toBuilder().setRole(Role.OWNER).build());
    assertEquals(Role.OWNER, updatedAcl.getRole());
    Set<Acl> acls = Sets.newHashSet(storage.listAcls(blobId));
    assertTrue(acls.contains(updatedAcl));
    assertTrue(storage.deleteAcl(blobId, User.ofAllAuthenticatedUsers()));
    assertNull(storage.getAcl(blobId, User.ofAllAuthenticatedUsers()));
    // test non-existing blob
    BlobId otherBlobId = BlobId.of(BUCKET, "test-blob-acl", -1L);
    assertNull(storage.getAcl(otherBlobId, User.ofAllAuthenticatedUsers()));
    assertFalse(storage.deleteAcl(otherBlobId, User.ofAllAuthenticatedUsers()));
    try {
        storage.createAcl(otherBlobId, acl);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storage.updateAcl(otherBlobId, acl);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storage.listAcls(otherBlobId);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
}
Also used : 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 34 with BlobInfo

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

the class ITStorageTest method testListBlobsCurrentDirectory.

@Test(timeout = 5000)
public void testListBlobsCurrentDirectory() throws InterruptedException {
    String directoryName = "test-list-blobs-current-directory/";
    String subdirectoryName = "subdirectory/";
    String[] blobNames = { directoryName + subdirectoryName + "blob1", directoryName + "blob2" };
    BlobInfo blob1 = BlobInfo.newBuilder(BUCKET, blobNames[0]).setContentType(CONTENT_TYPE).build();
    BlobInfo blob2 = BlobInfo.newBuilder(BUCKET, blobNames[1]).setContentType(CONTENT_TYPE).build();
    Blob remoteBlob1 = storage.create(blob1, BLOB_BYTE_CONTENT);
    Blob remoteBlob2 = storage.create(blob2, BLOB_BYTE_CONTENT);
    assertNotNull(remoteBlob1);
    assertNotNull(remoteBlob2);
    Page<Blob> page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-current-directory/"), Storage.BlobListOption.currentDirectory());
    // test fails if timeout is reached.
    while (Iterators.size(page.iterateAll().iterator()) != 2) {
        Thread.sleep(500);
        page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-current-directory/"), Storage.BlobListOption.currentDirectory());
    }
    Iterator<Blob> iterator = page.iterateAll().iterator();
    while (iterator.hasNext()) {
        Blob remoteBlob = iterator.next();
        assertEquals(BUCKET, remoteBlob.getBucket());
        if (remoteBlob.getName().equals(blobNames[1])) {
            assertEquals(CONTENT_TYPE, remoteBlob.getContentType());
            assertEquals(BLOB_BYTE_CONTENT.length, (long) remoteBlob.getSize());
            assertFalse(remoteBlob.isDirectory());
        } else if (remoteBlob.getName().equals(directoryName + subdirectoryName)) {
            assertEquals(0L, (long) remoteBlob.getSize());
            assertTrue(remoteBlob.isDirectory());
        } else {
            fail("Unexpected blob with name " + remoteBlob.getName());
        }
    }
    assertTrue(remoteBlob1.delete());
    assertTrue(remoteBlob2.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 35 with BlobInfo

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

the class ITStorageTest method testUpdateBlobUnsetMetadata.

@Test
public void testUpdateBlobUnsetMetadata() {
    String blobName = "test-update-blob-unset-metadata";
    ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a", "k2", "b");
    Map<String, String> newMetadata = new HashMap<>();
    newMetadata.put("k1", "a");
    newMetadata.put("k2", null);
    ImmutableMap<String, String> expectedMetadata = ImmutableMap.of("k1", "a");
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    Blob updatedBlob = remoteBlob.toBuilder().setMetadata(newMetadata).build().update();
    assertNotNull(updatedBlob);
    assertEquals(blob.getName(), updatedBlob.getName());
    assertEquals(blob.getBucket(), updatedBlob.getBucket());
    assertEquals(expectedMetadata, updatedBlob.getMetadata());
    assertTrue(updatedBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) HashMap(java.util.HashMap) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Aggregations

BlobInfo (com.google.cloud.storage.BlobInfo)94 Test (org.junit.Test)61 Blob (com.google.cloud.storage.Blob)56 BlobId (com.google.cloud.storage.BlobId)31 Storage (com.google.cloud.storage.Storage)21 StorageException (com.google.cloud.storage.StorageException)17 WriteChannel (com.google.cloud.WriteChannel)13 ReadChannel (com.google.cloud.ReadChannel)7 CopyWriter (com.google.cloud.storage.CopyWriter)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteBuffer (java.nio.ByteBuffer)7 InputStream (java.io.InputStream)4 URL (java.net.URL)4 StorageBatch (com.google.cloud.storage.StorageBatch)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 TestRunner (org.apache.nifi.util.TestRunner)3 Acl (com.google.cloud.storage.Acl)2 Bucket (com.google.cloud.storage.Bucket)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2