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));
}
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);
}
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);
}
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);
}
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)));
}
}
}
Aggregations