Search in sources :

Example 76 with BlobInfo

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

the class CreateBlob method main.

public static void main(String... args) {
    Storage storage = StorageOptions.getDefaultInstance().getService();
    BlobId blobId = BlobId.of("bucket", "blob_name");
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
}
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)

Example 77 with BlobInfo

use of com.google.cloud.storage.BlobInfo in project workbench by all-of-us.

the class CloudStorageServiceImpl method writeFile.

@Override
public void writeFile(String bucketName, String fileName, byte[] bytes) {
    Storage storage = StorageOptions.getDefaultInstance().getService();
    BlobId blobId = BlobId.of(bucketName, fileName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    storage.create(blobInfo, bytes);
}
Also used : Storage(com.google.cloud.storage.Storage) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Example 78 with BlobInfo

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

the class GoogleStorageTests method testWritableOutputStreamWithAutoCreateOnNullBlob.

@Test
public void testWritableOutputStreamWithAutoCreateOnNullBlob() throws Exception {
    String location = "gs://test-spring/test";
    Storage storage = mock(Storage.class);
    BlobId blobId = BlobId.of("test-spring", "test");
    when(storage.get(blobId)).thenReturn(null);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
    WriteChannel writeChannel = mock(WriteChannel.class);
    Blob blob = mock(Blob.class);
    when(blob.writer()).thenReturn(writeChannel);
    when(storage.create(blobInfo)).thenReturn(blob);
    GoogleStorageResource resource = new GoogleStorageResource(storage, location);
    GoogleStorageResource spyResource = spy(resource);
    OutputStream os = spyResource.getOutputStream();
    Assert.assertNotNull(os);
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) WriteChannel(com.google.cloud.WriteChannel) OutputStream(java.io.OutputStream) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 79 with BlobInfo

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

the class GoogleStorageTests method testWritableOutputStreamWithAutoCreateOnNonExistantBlob.

@Test
public void testWritableOutputStreamWithAutoCreateOnNonExistantBlob() throws Exception {
    String location = "gs://test-spring/test";
    Storage storage = mock(Storage.class);
    BlobId blobId = BlobId.of("test-spring", "test");
    Blob nonExistantBlob = mock(Blob.class);
    when(nonExistantBlob.exists()).thenReturn(false);
    when(storage.get(blobId)).thenReturn(nonExistantBlob);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
    WriteChannel writeChannel = mock(WriteChannel.class);
    Blob blob = mock(Blob.class);
    when(blob.writer()).thenReturn(writeChannel);
    when(storage.create(blobInfo)).thenReturn(blob);
    GoogleStorageResource resource = new GoogleStorageResource(storage, location);
    GoogleStorageResource spyResource = spy(resource);
    OutputStream os = spyResource.getOutputStream();
    Assert.assertNotNull(os);
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) WriteChannel(com.google.cloud.WriteChannel) OutputStream(java.io.OutputStream) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 80 with BlobInfo

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

the class GcsSession method write.

@Override
public void write(InputStream inputStream, String destination) throws IOException {
    String[] tokens = getBucketAndObjectFromPath(destination);
    Assert.state(tokens.length == 2, "Can only write to files, not buckets.");
    BlobInfo gcsBlobInfo = BlobInfo.newBuilder(BlobId.of(tokens[0], tokens[1])).build();
    try (InputStream is = inputStream) {
        try (WriteChannel channel = this.gcs.writer(gcsBlobInfo)) {
            channel.write(ByteBuffer.wrap(StreamUtils.copyToByteArray(is)));
        }
    }
}
Also used : InputStream(java.io.InputStream) WriteChannel(com.google.cloud.WriteChannel) 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