Search in sources :

Example 21 with BlobId

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

the class ITStorageTest method testCopyBlob.

@Test
public void testCopyBlob() {
    String sourceBlobName = "test-copy-blob-source";
    BlobId source = BlobId.of(BUCKET, sourceBlobName);
    ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
    BlobInfo blob = BlobInfo.newBuilder(source).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
    Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT);
    assertNotNull(remoteBlob);
    String targetBlobName = "test-copy-blob-target";
    Storage.CopyRequest req = Storage.CopyRequest.of(source, BlobId.of(BUCKET, targetBlobName));
    CopyWriter copyWriter = storage.copy(req);
    assertEquals(BUCKET, copyWriter.getResult().getBucket());
    assertEquals(targetBlobName, copyWriter.getResult().getName());
    assertEquals(CONTENT_TYPE, copyWriter.getResult().getContentType());
    assertEquals(metadata, copyWriter.getResult().getMetadata());
    assertTrue(copyWriter.isDone());
    assertTrue(remoteBlob.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)

Example 22 with BlobId

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

the class ITStorageTest method testReadChannelFail.

@Test
public void testReadChannelFail() throws IOException {
    String blobName = "test-read-channel-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.metagenerationMatch(-1L))) {
        reader.read(ByteBuffer.allocate(42));
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.generationMatch(-1L))) {
        reader.read(ByteBuffer.allocate(42));
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    BlobId blobIdWrongGeneration = BlobId.of(BUCKET, blobName, -1L);
    try (ReadChannel reader = storage.reader(blobIdWrongGeneration, Storage.BlobSourceOption.generationMatch())) {
        reader.read(ByteBuffer.allocate(42));
        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) BlobId(com.google.cloud.storage.BlobId) ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 23 with BlobId

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

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

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

the class ITStorageTest method testBatchRequestManyOperations.

@Test
public void testBatchRequestManyOperations() {
    List<StorageBatchResult<Boolean>> deleteResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE);
    List<StorageBatchResult<Blob>> getResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
    List<StorageBatchResult<Blob>> updateResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
    StorageBatch batch = storage.batch();
    for (int i = 0; i < MAX_BATCH_SIZE; i++) {
        BlobId blobId = BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i);
        deleteResults.add(batch.delete(blobId));
    }
    for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
        BlobId blobId = BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i);
        getResults.add(batch.get(blobId));
    }
    for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
        BlobInfo blob = BlobInfo.newBuilder(BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i)).build();
        updateResults.add(batch.update(blob));
    }
    String sourceBlobName1 = "test-batch-request-many-operations-source-blob-1";
    String sourceBlobName2 = "test-batch-request-many-operations-source-blob-2";
    BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
    BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
    assertNotNull(storage.create(sourceBlob1));
    assertNotNull(storage.create(sourceBlob2));
    BlobInfo updatedBlob2 = sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build();
    StorageBatchResult<Blob> getResult = batch.get(BUCKET, sourceBlobName1);
    StorageBatchResult<Blob> updateResult = batch.update(updatedBlob2);
    batch.submit();
    // Check deletes
    for (StorageBatchResult<Boolean> failedDeleteResult : deleteResults) {
        assertFalse(failedDeleteResult.get());
    }
    // Check gets
    for (StorageBatchResult<Blob> failedGetResult : getResults) {
        assertNull(failedGetResult.get());
    }
    Blob remoteBlob1 = getResult.get();
    assertEquals(sourceBlob1.getBucket(), remoteBlob1.getBucket());
    assertEquals(sourceBlob1.getName(), remoteBlob1.getName());
    // Check updates
    for (StorageBatchResult<Blob> failedUpdateResult : updateResults) {
        try {
            failedUpdateResult.get();
            fail("Expected StorageException");
        } catch (StorageException ex) {
        // expected
        }
    }
    Blob remoteUpdatedBlob2 = updateResult.get();
    assertEquals(sourceBlob2.getBucket(), remoteUpdatedBlob2.getBucket());
    assertEquals(sourceBlob2.getName(), remoteUpdatedBlob2.getName());
    assertEquals(updatedBlob2.getContentType(), remoteUpdatedBlob2.getContentType());
    assertTrue(remoteBlob1.delete());
    assertTrue(remoteUpdatedBlob2.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) StorageBatchResult(com.google.cloud.storage.StorageBatchResult) StorageBatch(com.google.cloud.storage.StorageBatch) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Aggregations

BlobId (com.google.cloud.storage.BlobId)39 Blob (com.google.cloud.storage.Blob)23 BlobInfo (com.google.cloud.storage.BlobInfo)20 Test (org.junit.Test)12 Acl (com.google.cloud.storage.Acl)9 Storage (com.google.cloud.storage.Storage)9 StorageException (com.google.cloud.storage.StorageException)9 CopyWriter (com.google.cloud.storage.CopyWriter)5 ReadChannel (com.google.cloud.ReadChannel)2 User (com.google.cloud.storage.Acl.User)2 StorageBatch (com.google.cloud.storage.StorageBatch)2 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 BatchResult (com.google.cloud.BatchResult)1 WriteChannel (com.google.cloud.WriteChannel)1 Builder (com.google.cloud.storage.BlobInfo.Builder)1 ComposeRequest (com.google.cloud.storage.Storage.ComposeRequest)1