Search in sources :

Example 91 with Storage

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

the class GoogleStorageTests method testGetInputStreamOnNullBlob.

@Test(expected = FileNotFoundException.class)
public void testGetInputStreamOnNullBlob() 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.getInputStream();
}
Also used : Storage(com.google.cloud.storage.Storage) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 92 with Storage

use of com.google.cloud.storage.Storage 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 93 with Storage

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

the class StorageSnippets method getRequesterPaysStatus.

/**
 * Example of retrieving Requester pays status on a bucket.
 */
public Bucket getRequesterPaysStatus(String bucketName) throws StorageException {
    // [START get_requester_pays_status]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The name of the bucket to retrieve requester-pays status, eg. "my-bucket"
    // String bucketName = "my-bucket"
    // Retrieve the bucket, throws StorageException on failure
    Bucket bucket = storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.BILLING));
    System.out.println("Requester pays status : " + bucket.requesterPays());
    // [END get_requester_pays_status]
    return bucket;
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 94 with Storage

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

the class StorageSnippets method listBuckets.

/**
 * Example of a simple listBuckets()
 */
public void listBuckets() {
    // [START storage_list_buckets]
    Storage storage = StorageOptions.getDefaultInstance().getService();
    Page<Bucket> buckets = storage.list();
    System.out.println("Buckets:");
    for (Bucket bucket : buckets.iterateAll()) {
        System.out.println(bucket.getName());
    }
// [END storage_list_buckets]
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 95 with Storage

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

the class StorageSnippets method setEventBasedHold.

/**
 * Example of how to set event-based hold for a blob
 */
public Blob setEventBasedHold(String bucketName, String blobName) throws StorageException {
    // [START storage_set_event_based_hold]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The name of a bucket, e.g. "my-bucket"
    // String bucketName = "my-bucket";
    // The name of a blob, e.g. "my-blob"
    // String blobName = "my-blob";
    BlobId blobId = BlobId.of(bucketName, blobName);
    Blob blob = storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(true).build());
    System.out.println("Event-based hold was set for " + blobName);
    // [END storage_set_event_based_hold]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) BlobId(com.google.cloud.storage.BlobId)

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