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