use of com.google.cloud.storage.Storage in project java-docs-samples by GoogleCloudPlatform.
the class AuthExample method authExplicit.
// [END auth_cloud_implicit]
// [START auth_cloud_explicit]
static void authExplicit(String jsonPath) throws IOException {
// You can specify a credential file by providing a path to GoogleCredentials.
// Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
System.out.println("Buckets:");
Page<Bucket> buckets = storage.list();
for (Bucket bucket : buckets.iterateAll()) {
System.out.println(bucket.toString());
}
}
use of com.google.cloud.storage.Storage 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.Storage in project spring-cloud-gcp by spring-cloud.
the class GoogleStorageTests method testWritableOutputStreamNoAutoCreateOnNullBlob.
@Test(expected = FileNotFoundException.class)
public void testWritableOutputStreamNoAutoCreateOnNullBlob() throws Exception {
String location = "gs://test-spring/test";
Storage storage = mock(Storage.class);
when(storage.get(BlobId.of("test-spring", "test"))).thenReturn(null);
GoogleStorageResource resource = new GoogleStorageResource(storage, location, false);
resource.getOutputStream();
}
use of com.google.cloud.storage.Storage 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.Storage in project spring-cloud-gcp by spring-cloud.
the class GoogleStorageTests method testGetFilenameOnNonExistingBlob.
@Test
public void testGetFilenameOnNonExistingBlob() throws Exception {
String location = "gs://test-spring/test";
Storage storage = mock(Storage.class);
when(storage.get(BlobId.of("test-spring", "test"))).thenReturn(null);
GoogleStorageResource resource = new GoogleStorageResource(storage, location, false);
Assert.assertEquals("test", resource.getFilename());
}
Aggregations