Search in sources :

Example 86 with Storage

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());
    }
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) FileInputStream(java.io.FileInputStream)

Example 87 with Storage

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);
}
Also used : Storage(com.google.cloud.storage.Storage) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Example 88 with Storage

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();
}
Also used : Storage(com.google.cloud.storage.Storage) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 89 with Storage

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);
}
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 90 with Storage

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());
}
Also used : Storage(com.google.cloud.storage.Storage) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Storage (com.google.cloud.storage.Storage)140 Bucket (com.google.cloud.storage.Bucket)45 Blob (com.google.cloud.storage.Blob)44 Test (org.junit.Test)30 BlobId (com.google.cloud.storage.BlobId)24 BlobInfo (com.google.cloud.storage.BlobInfo)14 TestRunner (org.apache.nifi.util.TestRunner)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 Policy (com.google.cloud.Policy)9 ArrayList (java.util.ArrayList)8 Acl (com.google.cloud.storage.Acl)7 Date (java.util.Date)7 HashMap (java.util.HashMap)7 BucketInfo (com.google.cloud.storage.BucketInfo)6 HmacKeyMetadata (com.google.cloud.storage.HmacKey.HmacKeyMetadata)6 MockFlowFile (org.apache.nifi.util.MockFlowFile)6 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)5 Binding (com.google.cloud.Binding)5 WriteChannel (com.google.cloud.WriteChannel)4 IOException (java.io.IOException)4