Search in sources :

Example 36 with BlobInfo

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

the class ITStorageTest method testUpdateBlobs.

@Test
public void testUpdateBlobs() {
    String sourceBlobName1 = "test-update-blobs-1";
    String sourceBlobName2 = "test-update-blobs-2";
    BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
    BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
    Blob remoteBlob1 = storage.create(sourceBlob1);
    Blob remoteBlob2 = storage.create(sourceBlob2);
    assertNotNull(remoteBlob1);
    assertNotNull(remoteBlob2);
    List<Blob> updatedBlobs = storage.update(remoteBlob1.toBuilder().setContentType(CONTENT_TYPE).build(), remoteBlob2.toBuilder().setContentType(CONTENT_TYPE).build());
    assertEquals(sourceBlob1.getBucket(), updatedBlobs.get(0).getBucket());
    assertEquals(sourceBlob1.getName(), updatedBlobs.get(0).getName());
    assertEquals(CONTENT_TYPE, updatedBlobs.get(0).getContentType());
    assertEquals(sourceBlob2.getBucket(), updatedBlobs.get(1).getBucket());
    assertEquals(sourceBlob2.getName(), updatedBlobs.get(1).getName());
    assertEquals(CONTENT_TYPE, updatedBlobs.get(1).getContentType());
    assertTrue(updatedBlobs.get(0).delete());
    assertTrue(updatedBlobs.get(1).delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 37 with BlobInfo

use of com.google.cloud.storage.BlobInfo in project spring-cloud-gcp by spring-cloud.

the class GcsMessageHandlerTests method testNewFiles.

@Test
public void testNewFiles() throws IOException {
    File testFile = this.temporaryFolder.newFile("benfica");
    BlobInfo expectedCreateBlobInfo = BlobInfo.newBuilder(BlobId.of("testGcsBucket", "benfica.writing")).build();
    WriteChannel writeChannel = mock(WriteChannel.class);
    willAnswer(invocationOnMock -> writeChannel).given(GCS).writer(eq(expectedCreateBlobInfo));
    willAnswer(invocationOnMock -> 10).given(writeChannel).write(isA(ByteBuffer.class));
    CopyWriter copyWriter = mock(CopyWriter.class);
    ArgumentCaptor<Storage.CopyRequest> copyRequestCaptor = ArgumentCaptor.forClass(Storage.CopyRequest.class);
    willAnswer(invocationOnMock -> copyWriter).given(GCS).copy(isA(Storage.CopyRequest.class));
    willAnswer(invocationOnMock -> true).given(GCS).delete(eq(BlobId.of("testGcsBucket", "benfica.writing")));
    this.channel.send(new GenericMessage<Object>(testFile));
    verify(GCS, times(1)).writer(eq(expectedCreateBlobInfo));
    verify(GCS, times(1)).copy(copyRequestCaptor.capture());
    verify(GCS, times(1)).delete(eq(BlobId.of("testGcsBucket", "benfica.writing")));
    Storage.CopyRequest expectedCopyRequest = copyRequestCaptor.getValue();
    assertThat(expectedCopyRequest.getSource()).isEqualTo(BlobId.of("testGcsBucket", "benfica.writing"));
    assertThat(expectedCopyRequest.getTarget().getBlobId()).isEqualTo(BlobId.of("testGcsBucket", "benfica"));
}
Also used : Storage(com.google.cloud.storage.Storage) WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) CopyWriter(com.google.cloud.storage.CopyWriter) Test(org.junit.Test)

Example 38 with BlobInfo

use of com.google.cloud.storage.BlobInfo in project flink by apache.

the class GSBlobStorageImpl method createBlob.

@Override
public void createBlob(GSBlobIdentifier blobIdentifier) {
    LOGGER.trace("Creating empty blob {}", blobIdentifier);
    Preconditions.checkNotNull(blobIdentifier);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobIdentifier.getBlobId()).build();
    storage.create(blobInfo);
}
Also used : BlobInfo(com.google.cloud.storage.BlobInfo)

Example 39 with BlobInfo

use of com.google.cloud.storage.BlobInfo in project flink by apache.

the class GSBlobStorageImpl method writeBlob.

@Override
public GSBlobStorage.WriteChannel writeBlob(GSBlobIdentifier blobIdentifier) {
    LOGGER.trace("Creating writeable blob for identifier {}", blobIdentifier);
    Preconditions.checkNotNull(blobIdentifier);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobIdentifier.getBlobId()).build();
    com.google.cloud.WriteChannel writeChannel = storage.writer(blobInfo);
    return new WriteChannel(blobIdentifier, writeChannel);
}
Also used : BlobInfo(com.google.cloud.storage.BlobInfo)

Example 40 with BlobInfo

use of com.google.cloud.storage.BlobInfo in project flink by apache.

the class GSBlobStorageImpl method compose.

@Override
public void compose(List<GSBlobIdentifier> sourceBlobIdentifiers, GSBlobIdentifier targetBlobIdentifier) {
    LOGGER.trace("Composing blobs {} to blob {}", sourceBlobIdentifiers, targetBlobIdentifier);
    Preconditions.checkNotNull(sourceBlobIdentifiers);
    Preconditions.checkArgument(sourceBlobIdentifiers.size() > 0);
    Preconditions.checkArgument(sourceBlobIdentifiers.size() <= BlobUtils.COMPOSE_MAX_BLOBS);
    Preconditions.checkNotNull(targetBlobIdentifier);
    // build a request to compose all the source blobs into the target blob
    Storage.ComposeRequest.Builder builder = Storage.ComposeRequest.newBuilder();
    BlobInfo targetBlobInfo = BlobInfo.newBuilder(targetBlobIdentifier.getBlobId()).build();
    builder.setTarget(targetBlobInfo);
    for (GSBlobIdentifier blobIdentifier : sourceBlobIdentifiers) {
        builder.addSource(blobIdentifier.objectName);
    }
    Storage.ComposeRequest request = builder.build();
    storage.compose(request);
}
Also used : Storage(com.google.cloud.storage.Storage) BlobInfo(com.google.cloud.storage.BlobInfo)

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