From 739d519e14649f8b7612631d90eed97a614bf93a Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Fri, 14 Sep 2018 18:02:46 -0700 Subject: [PATCH] Add downloadFile sample and reformat storage snippets (#3689) --- .../storage/snippets/StorageSnippets.java | 467 ++++++++---------- .../storage/snippets/ITStorageSnippets.java | 76 +-- 2 files changed, 240 insertions(+), 303 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java index 729b65b3b950..9d943c87331e 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java @@ -73,9 +73,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -/** - * This class contains a number of snippets for the {@link Storage} interface. - */ +/** This class contains a number of snippets for the {@link Storage} interface. */ public class StorageSnippets { private final Storage storage; @@ -84,9 +82,7 @@ public StorageSnippets(Storage storage) { this.storage = storage; } - /** - * Example of creating a bucket. - */ + /** Example of creating a bucket. */ // [TARGET create(BucketInfo, BucketTargetOption...)] // [VARIABLE "my_unique_bucket"] public Bucket createBucket(String bucketName) { @@ -96,26 +92,24 @@ public Bucket createBucket(String bucketName) { return bucket; } - /** - * Example of creating a bucket with storage class and location. - */ + /** Example of creating a bucket with storage class and location. */ // [TARGET create(BucketInfo, BucketTargetOption...)] // [VARIABLE "my_unique_bucket"] public Bucket createBucketWithStorageClassAndLocation(String bucketName) { // [START createBucketWithStorageClassAndLocation] - Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName) - // See here for possible values: http://g.co/cloud/storage/docs/storage-classes - .setStorageClass(StorageClass.COLDLINE) - // Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr - .setLocation("asia") - .build()); + Bucket bucket = + storage.create( + BucketInfo.newBuilder(bucketName) + // See here for possible values: http://g.co/cloud/storage/docs/storage-classes + .setStorageClass(StorageClass.COLDLINE) + // Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr + .setLocation("asia") + .build()); // [END createBucketWithStorageClassAndLocation] return bucket; } - /** - * Example of creating a blob with no content. - */ + /** Example of creating a blob with no content. */ // [TARGET create(BlobInfo, BlobTargetOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -128,9 +122,7 @@ public Blob createBlob(String bucketName, String blobName) { return blob; } - /** - * Example of creating a blob from a byte array. - */ + /** Example of creating a blob from a byte array. */ // [TARGET create(BlobInfo, byte[], BlobTargetOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -143,9 +135,7 @@ public Blob createBlobFromByteArray(String bucketName, String blobName) { return blob; } - /** - * Example of creating a blob from an input stream. - */ + /** Example of creating a blob from an input stream. */ // [TARGET create(BlobInfo, InputStream, BlobWriteOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -159,9 +149,7 @@ public Blob createBlobFromInputStream(String bucketName, String blobName) { return blob; } - /** - * Example of uploading an encrypted blob. - */ + /** Example of uploading an encrypted blob. */ // [TARGET create(BlobInfo, InputStream, BlobWriteOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -171,17 +159,13 @@ public Blob createEncryptedBlob(String bucketName, String blobName, String encry byte[] data = "Hello, World!".getBytes(UTF_8); BlobId blobId = BlobId.of(bucketName, blobName); - BlobInfo blobInfo = BlobInfo.newBuilder(blobId) - .setContentType("text/plain") - .build(); + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, data, BlobTargetOption.encryptionKey(encryptionKey)); // [END storageUploadEncryptedFile] return blob; } - /** - * Example of uploading a blob encrypted service side with a Cloud KMS key. - */ + /** Example of uploading a blob encrypted service side with a Cloud KMS key. */ public Blob createKmsEncrpytedBlob(String bucketName, String blobName, String kmsKeyName) { // [START storage_upload_with_kms_key] byte[] data = "Hello, World!".getBytes(UTF_8); @@ -195,9 +179,7 @@ public Blob createKmsEncrpytedBlob(String bucketName, String blobName, String km // String kmsKeyName = "" BlobId blobId = BlobId.of(bucketName, blobName); - BlobInfo blobInfo = BlobInfo.newBuilder(blobId) - .setContentType("text/plain") - .build(); + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, data, BlobTargetOption.kmsKeyName(kmsKeyName)); // [END storage_upload_with_kms_key] return blob; @@ -212,32 +194,30 @@ public Blob createKmsEncrpytedBlob(String bucketName, String blobName, String km // [VARIABLE 42] public Bucket getBucketWithMetageneration(String bucketName, long bucketMetageneration) { // [START getBucketWithMetageneration] - Bucket bucket = storage.get(bucketName, - BucketGetOption.metagenerationMatch(bucketMetageneration)); + Bucket bucket = + storage.get(bucketName, BucketGetOption.metagenerationMatch(bucketMetageneration)); // [END getBucketWithMetageneration] return bucket; } /** - * Example of getting information on a blob, only if its metageneration matches a value, - * otherwise a {@link StorageException} is thrown. + * Example of getting information on a blob, only if its metageneration matches a value, otherwise + * a {@link StorageException} is thrown. */ // [TARGET get(String, String, BlobGetOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42] - public Blob getBlobFromStringsWithMetageneration(String bucketName, String blobName, - long blobMetageneration) { + public Blob getBlobFromStringsWithMetageneration( + String bucketName, String blobName, long blobMetageneration) { // [START getBlobFromStringsWithMetageneration] - Blob blob = storage.get(bucketName, blobName, - BlobGetOption.metagenerationMatch(blobMetageneration)); + Blob blob = + storage.get(bucketName, blobName, BlobGetOption.metagenerationMatch(blobMetageneration)); // [END getBlobFromStringsWithMetageneration] return blob; } - /** - * Example of getting information on a blob. - */ + /** Example of getting information on a blob. */ // [TARGET get(BlobId)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -250,15 +230,15 @@ public Blob getBlobFromId(String bucketName, String blobName) { } /** - * Example of getting information on a blob, only if its metageneration matches a value, - * otherwise a {@link StorageException} is thrown. + * Example of getting information on a blob, only if its metageneration matches a value, otherwise + * a {@link StorageException} is thrown. */ // [TARGET get(BlobId, BlobGetOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42] - public Blob getBlobFromIdWithMetageneration(String bucketName, String blobName, - long blobMetageneration) { + public Blob getBlobFromIdWithMetageneration( + String bucketName, String blobName, long blobMetageneration) { // [START getBlobFromIdWithMetageneration] BlobId blobId = BlobId.of(bucketName, blobName); Blob blob = storage.get(blobId, BlobGetOption.metagenerationMatch(blobMetageneration)); @@ -266,17 +246,15 @@ public Blob getBlobFromIdWithMetageneration(String bucketName, String blobName, return blob; } - /** - * Example of listing buckets, specifying the page size and a name prefix. - */ + /** Example of listing buckets, specifying the page size and a name prefix. */ // [TARGET list(BucketListOption...)] // [VARIABLE "bucket_"] public Page listBucketsWithSizeAndPrefix(String prefix) { // [START listBucketsWithSizeAndPrefix] // Include a prefix of bucket-name to reduce search space. // For more information read https://cloud.google.com/storage/docs/json_api/v1/buckets/list - Page buckets = storage.list(BucketListOption.pageSize(100), - BucketListOption.prefix(prefix)); + Page buckets = + storage.list(BucketListOption.pageSize(100), BucketListOption.prefix(prefix)); for (Bucket bucket : buckets.iterateAll()) { // do something with the bucket } @@ -284,16 +262,15 @@ public Page listBucketsWithSizeAndPrefix(String prefix) { return buckets; } - /** - * Example of listing blobs in a provided directory. - */ + /** Example of listing blobs in a provided directory. */ // [TARGET list(String, BlobListOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_directory/"] public Page listBlobsWithDirectoryAndPrefix(String bucketName, String directory) { // [START listBlobsWithDirectoryAndPrefix] - Page blobs = storage.list(bucketName, BlobListOption.currentDirectory(), - BlobListOption.prefix(directory)); + Page blobs = + storage.list( + bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(directory)); for (Blob blob : blobs.iterateAll()) { // do something with the blob } @@ -301,9 +278,7 @@ public Page listBlobsWithDirectoryAndPrefix(String bucketName, String dire return blobs; } - /** - * Example of updating bucket information. - */ + /** Example of updating bucket information. */ // [TARGET update(BucketInfo, BucketTargetOption...)] // [VARIABLE "my_unique_bucket"] public Bucket updateBucket(String bucketName) { @@ -314,9 +289,7 @@ public Bucket updateBucket(String bucketName) { return bucket; } - /** - * Example of replacing blob's metadata. - */ + /** Example of replacing blob's metadata. */ // [TARGET update(BlobInfo)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -325,9 +298,8 @@ public Blob updateBlob(String bucketName, String blobName) { Map newMetadata = new HashMap<>(); newMetadata.put("key", "value"); storage.update(BlobInfo.newBuilder(bucketName, blobName).setMetadata(null).build()); - Blob blob = storage.update(BlobInfo.newBuilder(bucketName, blobName) - .setMetadata(newMetadata) - .build()); + Blob blob = + storage.update(BlobInfo.newBuilder(bucketName, blobName).setMetadata(newMetadata).build()); // [END updateBlob] return blob; } @@ -349,16 +321,16 @@ public Blob updateBlobWithMetageneration(String bucketName, String blobName) { } /** - * Example of deleting a bucket, only if its metageneration matches a value, otherwise a - * {@link StorageException} is thrown. + * Example of deleting a bucket, only if its metageneration matches a value, otherwise a {@link + * StorageException} is thrown. */ // [TARGET delete(String, BucketSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE 42] public boolean deleteBucketWithMetageneration(String bucketName, long bucketMetageneration) { // [START deleteBucketWithMetageneration] - boolean deleted = storage.delete(bucketName, - BucketSourceOption.metagenerationMatch(bucketMetageneration)); + boolean deleted = + storage.delete(bucketName, BucketSourceOption.metagenerationMatch(bucketMetageneration)); if (deleted) { // the bucket was deleted } else { @@ -369,18 +341,18 @@ public boolean deleteBucketWithMetageneration(String bucketName, long bucketMeta } /** - * Example of deleting a blob, only if its generation matches a value, otherwise a - * {@link StorageException} is thrown. + * Example of deleting a blob, only if its generation matches a value, otherwise a {@link + * StorageException} is thrown. */ // [TARGET delete(String, String, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42] - public boolean deleteBlobFromStringsWithGeneration(String bucketName, String blobName, - long blobGeneration) { + public boolean deleteBlobFromStringsWithGeneration( + String bucketName, String blobName, long blobGeneration) { // [START deleteBlobFromStringsWithGeneration] - boolean deleted = storage.delete(bucketName, blobName, - BlobSourceOption.generationMatch(blobGeneration)); + boolean deleted = + storage.delete(bucketName, blobName, BlobSourceOption.generationMatch(blobGeneration)); if (deleted) { // the blob was deleted } else { @@ -391,15 +363,15 @@ public boolean deleteBlobFromStringsWithGeneration(String bucketName, String blo } /** - * Example of deleting a blob, only if its generation matches a value, otherwise a - * {@link StorageException} is thrown. + * Example of deleting a blob, only if its generation matches a value, otherwise a {@link + * StorageException} is thrown. */ // [TARGET delete(BlobId, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42] - public boolean deleteBlobFromIdWithGeneration(String bucketName, String blobName, - long blobGeneration) { + public boolean deleteBlobFromIdWithGeneration( + String bucketName, String blobName, long blobGeneration) { // [START deleteBlobFromIdWithGeneration] BlobId blobId = BlobId.of(bucketName, blobName); boolean deleted = storage.delete(blobId, BlobSourceOption.generationMatch(blobGeneration)); @@ -412,9 +384,7 @@ public boolean deleteBlobFromIdWithGeneration(String bucketName, String blobName return deleted; } - /** - * Example of deleting a blob. - */ + /** Example of deleting a blob. */ // [TARGET delete(BlobId)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -431,60 +401,57 @@ public boolean deleteBlob(String bucketName, String blobName) { return deleted; } - /** - * Example of composing two blobs. - */ + /** Example of composing two blobs. */ // [TARGET compose(ComposeRequest)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "source_blob_1"] // [VARIABLE "source_blob_2"] - public Blob composeBlobs(String bucketName, String blobName, String sourceBlob1, - String sourceBlob2) { + public Blob composeBlobs( + String bucketName, String blobName, String sourceBlob1, String sourceBlob2) { // [START composeBlobs] BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); - ComposeRequest request = ComposeRequest.newBuilder() - .setTarget(blobInfo) - .addSource(sourceBlob1) - .addSource(sourceBlob2) - .build(); + ComposeRequest request = + ComposeRequest.newBuilder() + .setTarget(blobInfo) + .addSource(sourceBlob1) + .addSource(sourceBlob2) + .build(); Blob blob = storage.compose(request); // [END composeBlobs] return blob; } - /** - * Example of copying a blob. - */ + /** Example of copying a blob. */ // [TARGET copy(CopyRequest)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "copy_blob_name"] public Blob copyBlob(String bucketName, String blobName, String copyBlobName) { // [START copyBlob] - CopyRequest request = CopyRequest.newBuilder() - .setSource(BlobId.of(bucketName, blobName)) - .setTarget(BlobId.of(bucketName, copyBlobName)) - .build(); + CopyRequest request = + CopyRequest.newBuilder() + .setSource(BlobId.of(bucketName, blobName)) + .setTarget(BlobId.of(bucketName, copyBlobName)) + .build(); Blob blob = storage.copy(request).getResult(); // [END copyBlob] return blob; } - /** - * Example of copying a blob in chunks. - */ + /** Example of copying a blob in chunks. */ // [TARGET copy(CopyRequest)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "copy_blob_name"] public Blob copyBlobInChunks(String bucketName, String blobName, String copyBlobName) { // [START copyBlobInChunks] - CopyRequest request = CopyRequest.newBuilder() - .setSource(BlobId.of(bucketName, blobName)) - .setTarget(BlobId.of(bucketName, copyBlobName)) - .build(); + CopyRequest request = + CopyRequest.newBuilder() + .setSource(BlobId.of(bucketName, blobName)) + .setTarget(BlobId.of(bucketName, copyBlobName)) + .build(); CopyWriter copyWriter = storage.copy(request); while (!copyWriter.isDone()) { copyWriter.copyChunk(); @@ -494,9 +461,7 @@ public Blob copyBlobInChunks(String bucketName, String blobName, String copyBlob return blob; } - /** - * Example of rotating the encryption key of a blob. - */ + /** Example of rotating the encryption key of a blob. */ // [TARGET copy(CopyRequest)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -506,36 +471,38 @@ public Blob rotateBlobEncryptionKey( String bucketName, String blobName, String oldEncryptionKey, String newEncryptionKey) { // [START storageRotateEncryptionKey] BlobId blobId = BlobId.of(bucketName, blobName); - CopyRequest request = CopyRequest.newBuilder() - .setSource(blobId) - .setSourceOptions(BlobSourceOption.decryptionKey(oldEncryptionKey)) - .setTarget(blobId, BlobTargetOption.encryptionKey(newEncryptionKey)) - .build(); + CopyRequest request = + CopyRequest.newBuilder() + .setSource(blobId) + .setSourceOptions(BlobSourceOption.decryptionKey(oldEncryptionKey)) + .setTarget(blobId, BlobTargetOption.encryptionKey(newEncryptionKey)) + .build(); Blob blob = storage.copy(request).getResult(); // [END storageRotateEncryptionKey] return blob; } /** - * Example of reading all bytes of a blob, if generation matches a value, otherwise a - * {@link StorageException} is thrown. + * Example of reading all bytes of a blob, if generation matches a value, otherwise a {@link + * StorageException} is thrown. */ // [TARGET readAllBytes(String, String, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42"] - public byte[] readBlobFromStringsWithGeneration(String bucketName, String blobName, - long blobGeneration) { + public byte[] readBlobFromStringsWithGeneration( + String bucketName, String blobName, long blobGeneration) { // [START readBlobFromStringsWithGeneration] - byte[] content = storage.readAllBytes(bucketName, blobName, - BlobSourceOption.generationMatch(blobGeneration)); + byte[] content = + storage.readAllBytes( + bucketName, blobName, BlobSourceOption.generationMatch(blobGeneration)); // [END readBlobFromStringsWithGeneration] return content; } /** - * Example of reading all bytes of a blob's specific generation, otherwise a - * {@link StorageException} is thrown. + * Example of reading all bytes of a blob's specific generation, otherwise a {@link + * StorageException} is thrown. */ // [TARGET readAllBytes(BlobId, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] @@ -549,24 +516,20 @@ public byte[] readBlobFromId(String bucketName, String blobName, long blobGenera return content; } - /** - * Example of reading all bytes of an encrypted blob. - */ + /** Example of reading all bytes of an encrypted blob. */ // [TARGET readAllBytes(BlobId, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "my_encryption_key"] public byte[] readEncryptedBlob(String bucketName, String blobName, String decryptionKey) { // [START readEncryptedBlob] - byte[] content = storage.readAllBytes( - bucketName, blobName, BlobSourceOption.decryptionKey(decryptionKey)); + byte[] content = + storage.readAllBytes(bucketName, blobName, BlobSourceOption.decryptionKey(decryptionKey)); // [END readEncryptedBlob] return content; } - /** - * Example of using a batch request to delete, update and get a blob. - */ + /** Example of using a batch request to delete, update and get a blob. */ // [TARGET batch()] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] @@ -576,15 +539,18 @@ public Blob batch(String bucketName, String blobName1, String blobName2) { StorageBatch batch = storage.batch(); BlobId firstBlob = BlobId.of(bucketName, blobName1); BlobId secondBlob = BlobId.of(bucketName, blobName2); - batch.delete(firstBlob).notify(new BatchResult.Callback() { - public void success(Boolean result) { - // deleted successfully - } - - public void error(StorageException exception) { - // delete failed - } - }); + batch + .delete(firstBlob) + .notify( + new BatchResult.Callback() { + public void success(Boolean result) { + // deleted successfully + } + + public void error(StorageException exception) { + // delete failed + } + }); batch.update(BlobInfo.newBuilder(secondBlob).setContentType("text/plain").build()); StorageBatchResult result = batch.get(secondBlob); batch.submit(); @@ -593,9 +559,7 @@ public void error(StorageException exception) { return blob; } - /** - * Example of reading a blob's content through a reader. - */ + /** Example of reading a blob's content through a reader. */ // [TARGET reader(String, String, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -612,9 +576,7 @@ public void readerFromStrings(String bucketName, String blobName) throws IOExcep // [END readerFromStrings] } - /** - * Example of reading a blob's content through a reader. - */ + /** Example of reading a blob's content through a reader. */ // [TARGET reader(BlobId, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -632,9 +594,7 @@ public void readerFromId(String bucketName, String blobName) throws IOException // [END readerFromId] } - /** - * Example of writing a blob's content through a writer. - */ + /** Example of writing a blob's content through a writer. */ // [TARGET writer(BlobInfo, BlobWriteOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -654,24 +614,23 @@ public void writer(String bucketName, String blobName) throws IOException { } /** - * Example of creating a signed URL that is valid for 2 weeks, using the default credentials - * for signing the URL. + * Example of creating a signed URL that is valid for 2 weeks, using the default credentials for + * signing the URL. */ // [TARGET signUrl(BlobInfo, long, TimeUnit, SignUrlOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public URL signUrl(String bucketName, String blobName) { // [START signUrl] - URL signedUrl = storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), 14, - TimeUnit.DAYS); + URL signedUrl = + storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), 14, TimeUnit.DAYS); // [END signUrl] return signedUrl; } /** - * Example of creating a signed URL passing the - * {@link SignUrlOption#signWith(ServiceAccountSigner)} option, that will be used for signing the - * URL. + * Example of creating a signed URL passing the {@link + * SignUrlOption#signWith(ServiceAccountSigner)} option, that will be used for signing the URL. */ // [TARGET signUrl(BlobInfo, long, TimeUnit, SignUrlOption...)] // [VARIABLE "my_unique_bucket"] @@ -680,16 +639,18 @@ public URL signUrl(String bucketName, String blobName) { public URL signUrlWithSigner(String bucketName, String blobName, String keyPath) throws IOException { // [START signUrlWithSigner] - URL signedUrl = storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), - 14, TimeUnit.DAYS, SignUrlOption.signWith( - ServiceAccountCredentials.fromStream(new FileInputStream(keyPath)))); + URL signedUrl = + storage.signUrl( + BlobInfo.newBuilder(bucketName, blobName).build(), + 14, + TimeUnit.DAYS, + SignUrlOption.signWith( + ServiceAccountCredentials.fromStream(new FileInputStream(keyPath)))); // [END signUrlWithSigner] return signedUrl; } - /** - * Example of getting information on several blobs using a single batch request. - */ + /** Example of getting information on several blobs using a single batch request. */ // [TARGET get(BlobId...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] @@ -703,9 +664,7 @@ public List batchGet(String bucketName, String blobName1, String blobName2 return blobs; } - /** - * Example of getting information on several blobs using a single batch request. - */ + /** Example of getting information on several blobs using a single batch request. */ // [TARGET get(Iterable)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] @@ -720,9 +679,7 @@ public List batchGetIterable(String bucketName, String blobName1, String b return blobs; } - /** - * Example of updating information on several blobs using a single batch request. - */ + /** Example of updating information on several blobs using a single batch request. */ // [TARGET update(BlobInfo...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] @@ -731,16 +688,15 @@ public List batchUpdate(String bucketName, String blobName1, String blobNa // [START batchUpdate] Blob firstBlob = storage.get(bucketName, blobName1); Blob secondBlob = storage.get(bucketName, blobName2); - List updatedBlobs = storage.update( - firstBlob.toBuilder().setContentType("text/plain").build(), - secondBlob.toBuilder().setContentType("text/plain").build()); + List updatedBlobs = + storage.update( + firstBlob.toBuilder().setContentType("text/plain").build(), + secondBlob.toBuilder().setContentType("text/plain").build()); // [END batchUpdate] return updatedBlobs; } - /** - * Example of updating information on several blobs using a single batch request. - */ + /** Example of updating information on several blobs using a single batch request. */ // [TARGET update(Iterable)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] @@ -757,9 +713,7 @@ public List batchUpdateIterable(String bucketName, String blobName1, Strin return updatedBlobs; } - /** - * Example of deleting several blobs using a single batch request. - */ + /** Example of deleting several blobs using a single batch request. */ // [TARGET delete(BlobId...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] @@ -773,9 +727,7 @@ public List batchDelete(String bucketName, String blobName1, String blo return deleted; } - /** - * Example of deleting several blobs using a single batch request. - */ + /** Example of deleting several blobs using a single batch request. */ // [TARGET delete(Iterable)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] @@ -790,9 +742,7 @@ public List batchDeleteIterable(String bucketName, String blobName1, St return deleted; } - /** - * Example of getting the ACL entry for an entity on a bucket. - */ + /** Example of getting the ACL entry for an entity on a bucket. */ // [TARGET getAcl(String, Entity)] // [VARIABLE "my_unique_bucket"] public Acl getBucketAcl(String bucketName) { @@ -802,9 +752,7 @@ public Acl getBucketAcl(String bucketName) { return acl; } - /** - * Example of getting the ACL entry for a specific user on a bucket. - */ + /** Example of getting the ACL entry for a specific user on a bucket. */ // [TARGET getAcl(String, Entity)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "google-cloud-java-tests@java-docs-samples-tests.iam.gserviceaccount.com"] @@ -815,9 +763,7 @@ public Acl getBucketAcl(String bucketName, String userEmail) { return acl; } - /** - * Example of deleting the ACL entry for an entity on a bucket. - */ + /** Example of deleting the ACL entry for an entity on a bucket. */ // [TARGET deleteAcl(String, Entity)] // [VARIABLE "my_unique_bucket"] public boolean deleteBucketAcl(String bucketName) { @@ -832,9 +778,7 @@ public boolean deleteBucketAcl(String bucketName) { return deleted; } - /** - * Example of creating a new ACL entry on a bucket. - */ + /** Example of creating a new ACL entry on a bucket. */ // [TARGET createAcl(String, Acl)] // [VARIABLE "my_unique_bucket"] public Acl createBucketAcl(String bucketName) { @@ -844,9 +788,7 @@ public Acl createBucketAcl(String bucketName) { return acl; } - /** - * Example of updating a new ACL entry on a bucket. - */ + /** Example of updating a new ACL entry on a bucket. */ // [TARGET updateAcl(String, Acl)] // [VARIABLE "my_unique_bucket"] public Acl updateBucketAcl(String bucketName) { @@ -856,9 +798,7 @@ public Acl updateBucketAcl(String bucketName) { return acl; } - /** - * Example of listing the ACL entries for a blob. - */ + /** Example of listing the ACL entries for a blob. */ // [TARGET listAcls(String)] // [VARIABLE "my_unique_bucket"] public List listBucketAcls(String bucketName) { @@ -871,9 +811,7 @@ public List listBucketAcls(String bucketName) { return acls; } - /** - * Example of getting the default ACL entry for an entity on a bucket. - */ + /** Example of getting the default ACL entry for an entity on a bucket. */ // [TARGET getDefaultAcl(String, Entity)] // [VARIABLE "my_unique_bucket"] public Acl getDefaultBucketAcl(String bucketName) { @@ -883,9 +821,7 @@ public Acl getDefaultBucketAcl(String bucketName) { return acl; } - /** - * Example of deleting the default ACL entry for an entity on a bucket. - */ + /** Example of deleting the default ACL entry for an entity on a bucket. */ // [TARGET deleteDefaultAcl(String, Entity)] // [VARIABLE "my_unique_bucket"] public boolean deleteDefaultBucketAcl(String bucketName) { @@ -900,9 +836,7 @@ public boolean deleteDefaultBucketAcl(String bucketName) { return deleted; } - /** - * Example of creating a new default ACL entry on a bucket. - */ + /** Example of creating a new default ACL entry on a bucket. */ // [TARGET createDefaultAcl(String, Acl)] // [VARIABLE "my_unique_bucket"] public Acl createDefaultBucketAcl(String bucketName) { @@ -913,9 +847,7 @@ public Acl createDefaultBucketAcl(String bucketName) { return acl; } - /** - * Example of updating a new default ACL entry on a bucket. - */ + /** Example of updating a new default ACL entry on a bucket. */ // [TARGET updateDefaultAcl(String, Acl)] // [VARIABLE "my_unique_bucket"] public Acl updateDefaultBucketAcl(String bucketName) { @@ -926,9 +858,7 @@ public Acl updateDefaultBucketAcl(String bucketName) { return acl; } - /** - * Example of listing the default ACL entries for a blob. - */ + /** Example of listing the default ACL entries for a blob. */ // [TARGET listDefaultAcls(String)] // [VARIABLE "my_unique_bucket"] public List listDefaultBucketAcls(String bucketName) { @@ -941,9 +871,7 @@ public List listDefaultBucketAcls(String bucketName) { return acls; } - /** - * Example of getting the ACL entry for an entity on a blob. - */ + /** Example of getting the ACL entry for an entity on a blob. */ // [TARGET getAcl(BlobId, Entity)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -956,9 +884,7 @@ public Acl getBlobAcl(String bucketName, String blobName, long blobGeneration) { return acl; } - /** - * Example of getting the ACL entry for a specific user on a blob. - */ + /** Example of getting the ACL entry for a specific user on a blob. */ // [TARGET getAcl(BlobId, Entity)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -971,9 +897,7 @@ public Acl getBlobAcl(String bucketName, String blobName, String userEmail) { return acl; } - /** - * Example of deleting the ACL entry for an entity on a blob. - */ + /** Example of deleting the ACL entry for an entity on a blob. */ // [TARGET deleteAcl(BlobId, Entity)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -991,9 +915,7 @@ public boolean deleteBlobAcl(String bucketName, String blobName, long blobGenera return deleted; } - /** - * Example of creating a new ACL entry on a blob. - */ + /** Example of creating a new ACL entry on a blob. */ // [TARGET createAcl(BlobId, Acl)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -1006,9 +928,7 @@ public Acl createBlobAcl(String bucketName, String blobName, long blobGeneration return acl; } - /** - * Example of updating a new ACL entry on a blob. - */ + /** Example of updating a new ACL entry on a blob. */ // [TARGET updateAcl(BlobId, Acl)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -1021,9 +941,7 @@ public Acl updateBlobAcl(String bucketName, String blobName, long blobGeneration return acl; } - /** - * Example of updating a blob to be public-read. - */ + /** Example of updating a blob to be public-read. */ // [TARGET createAcl(BlobId, Acl)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -1036,9 +954,7 @@ public Acl blobToPublicRead(String bucketName, String blobName, long blobGenerat return acl; } - /** - * Example of listing the ACL entries for a blob. - */ + /** Example of listing the ACL entries for a blob. */ // [TARGET listAcls(BlobId)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -1054,9 +970,7 @@ public List listBlobAcls(String bucketName, String blobName, long blobGener return acls; } - /** - * Example of default auth - */ + /** Example of default auth */ public Page authListBuckets() { // [START auth_cloud_implicit] // If you don't specify credentials when constructing the client, the @@ -1072,31 +986,25 @@ public Page authListBuckets() { return buckets; } - /** - * Example of enabling Requester pays on a bucket. - */ - public Bucket enableRequesterPays(String bucketName) throws StorageException { + /** Example of enabling Requester pays on a bucket. */ + public Bucket enableRequesterPays(String bucketName) throws StorageException { // [START enable_requester_pays] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of the existing bucket to enable requester-paying for, e.g. "my-bucket" // String bucketName = "my-bucket" - BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName) - .setRequesterPays(true) - .build(); + BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setRequesterPays(true).build(); // Update the bucket, throws StorageException on failure Bucket bucket = storage.update(bucketInfo); - System.out.println("Requester pay status for " + bucketName +": " + bucket.requesterPays()); + System.out.println("Requester pay status for " + bucketName + ": " + bucket.requesterPays()); // [END enable_requester_pays] return bucket; } - /** - * Example of disabling Requester pays on a bucket. - */ + /** Example of disabling Requester pays on a bucket. */ public Bucket disableRequesterPays(String bucketName) { // [START disable_requester_pays] // Instantiate a Google Cloud Storage client @@ -1104,21 +1012,17 @@ public Bucket disableRequesterPays(String bucketName) { // The name of the bucket to disable requester-paying for, e.g. "my-bucket" // String bucketName = "my-bucket" - BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName) - .setRequesterPays(false) - .build(); + BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setRequesterPays(false).build(); // Update the bucket, throws StorageException on failure Bucket bucket = storage.update(bucketInfo); - System.out.println("Requester pays status for " + bucketName +": " + bucket.requesterPays()); + System.out.println("Requester pays status for " + bucketName + ": " + bucket.requesterPays()); // [END disable_requester_pays] return bucket; } - /** - * Example of retrieving Requester pays status on a bucket. - */ + /** 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 @@ -1127,19 +1031,41 @@ public Bucket getRequesterPaysStatus(String bucketName) throws StorageException // 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)); + 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; } - /** - * Example of downloading a file using Requester pay. - */ - public void downloadFileUsingRequesterPays(String projectId, String bucketName, - String srcFilename, Path destFilePath) throws IOException { + /** Example of downloading a file. */ + public void downloadFile(String bucketName, String srcFilename, Path destFilePath) + throws IOException { + // [START storage_download_file] + // The name of the bucket to access + // String bucketName = "my-bucket"; + + // The name of the remote file to download + // String srcFilename = "file.txt"; + + // The path to which the file should be downloaded + // Path destFilePath = Paths.get("/local/path/to/file.txt"); + + // Instantiate a Google Cloud Storage client + Storage storage = StorageOptions.getDefaultInstance().getService(); + + // Get specific file from specified bucket + Blob blob = storage.get(BlobId.of(bucketName, srcFilename)); + + // Download file to specified path + blob.downloadTo(destFilePath); + // [END storage_download_file] + } + + /** Example of downloading a file using Requester pay. */ + public void downloadFileUsingRequesterPays( + String projectId, String bucketName, String srcFilename, Path destFilePath) + throws IOException { // [START storage_download_file_requester_pays] // The project ID to bill // String projectId = "my-billable-project-id"; @@ -1164,9 +1090,7 @@ public void downloadFileUsingRequesterPays(String projectId, String bucketName, // [END storage_download_file_requester_pays] } - /** - * Example of setting a default KMS key on a bucket. - */ + /** Example of setting a default KMS key on a bucket. */ public Bucket setDefaultKmsKey(String bucketName, String kmsKeyName) throws StorageException { // [START storage_set_bucket_default_kms_key] // Instantiate a Google Cloud Storage client @@ -1180,9 +1104,8 @@ public Bucket setDefaultKmsKey(String bucketName, String kmsKeyName) throws Stor // 'projects//locations//keyRings//cryptoKeys/' // String kmsKeyName = "" - BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName) - .setDefaultKmsKeyName(kmsKeyName) - .build(); + BucketInfo bucketInfo = + BucketInfo.newBuilder(bucketName).setDefaultKmsKeyName(kmsKeyName).build(); Bucket bucket = storage.update(bucketInfo); diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java index 470a761ff645..fffd0c3eb5b5 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java @@ -69,21 +69,19 @@ public class ITStorageSnippets { private static final Logger log = Logger.getLogger(ITStorageSnippets.class.getName()); private static final String BUCKET = RemoteStorageHelper.generateBucketName(); private static final byte[] BLOB_BYTE_CONTENT = {0xD, 0xE, 0xA, 0xD}; - private static final String USER_EMAIL = "google-cloud-java-tests@" - + "java-docs-samples-tests.iam.gserviceaccount.com"; + private static final String USER_EMAIL = + "google-cloud-java-tests@" + "java-docs-samples-tests.iam.gserviceaccount.com"; - private static final String KMS_KEY_NAME = "projects/gcloud-devel/locations/us/" - + "keyRings/gcs_kms_key_ring_us/cryptoKeys/key"; + private static final String KMS_KEY_NAME = + "projects/gcloud-devel/locations/us/" + "keyRings/gcs_kms_key_ring_us/cryptoKeys/key"; private static Storage storage; private static StorageSnippets storageSnippets; private static List bucketsToCleanUp; - @Rule - public ExpectedException thrown = ExpectedException.none(); + @Rule public ExpectedException thrown = ExpectedException.none(); - @Rule - public Timeout globalTimeout = Timeout.seconds(300); + @Rule public Timeout globalTimeout = Timeout.seconds(300); @BeforeClass public static void beforeClass() { @@ -171,8 +169,8 @@ public void testCreateUpdateEncryptedBlob() throws InterruptedException { String encryptionKey2 = "wnxMO0w+dmxribu7rICJ+Q2ES9TLpFRIDy3/L7HN5ZA="; - blob = storageSnippets.rotateBlobEncryptionKey( - BUCKET, blobName, encryptionKey1, encryptionKey2); + blob = + storageSnippets.rotateBlobEncryptionKey(BUCKET, blobName, encryptionKey1, encryptionKey2); assertNotNull(blob); encryptedContent = storageSnippets.readEncryptedBlob(BUCKET, blobName, encryptionKey2); @@ -211,8 +209,9 @@ public void testCreateBlobFromInputStream() { Blob blob = storageSnippets.createBlobFromInputStream(BUCKET, "test-create-blob-from-input-stream"); assertNotNull(blob); - assertTrue(storageSnippets.deleteBlobFromIdWithGeneration( - BUCKET, "test-create-blob-from-input-stream", blob.getGeneration())); + assertTrue( + storageSnippets.deleteBlobFromIdWithGeneration( + BUCKET, "test-create-blob-from-input-stream", blob.getGeneration())); } @Test @@ -261,11 +260,11 @@ public void testComposeBlobs() { public void testReadWriteAndSignUrl() throws IOException { String blobName = "text-read-write-sign-url"; byte[] content = "Hello, World!".getBytes(UTF_8); - Blob blob = storage.create( - BlobInfo.newBuilder(BUCKET, blobName).build(), content); - assertArrayEquals(content, - storageSnippets.readBlobFromId(BUCKET, blobName, blob.getGeneration())); - assertArrayEquals(content, + Blob blob = storage.create(BlobInfo.newBuilder(BUCKET, blobName).build(), content); + assertArrayEquals( + content, storageSnippets.readBlobFromId(BUCKET, blobName, blob.getGeneration())); + assertArrayEquals( + content, storageSnippets.readBlobFromStringsWithGeneration(BUCKET, blobName, blob.getGeneration())); storageSnippets.readerFromId(BUCKET, blobName); storageSnippets.readerFromStrings(BUCKET, blobName); @@ -277,8 +276,9 @@ public void testReadWriteAndSignUrl() throws IOException { assertEquals(content.length, responseStream.read(readBytes)); assertArrayEquals(content, readBytes); } - signedUrl = storageSnippets.signUrlWithSigner(BUCKET, blobName, - System.getenv("GOOGLE_APPLICATION_CREDENTIALS")); + signedUrl = + storageSnippets.signUrlWithSigner( + BUCKET, blobName, System.getenv("GOOGLE_APPLICATION_CREDENTIALS")); connection = signedUrl.openConnection(); try (InputStream responseStream = connection.getInputStream()) { assertEquals(content.length, responseStream.read(readBytes)); @@ -342,7 +342,7 @@ public void testBucketAcl() { storage.createAcl(BUCKET, Acl.of(new User(USER_EMAIL), Role.READER)); Acl userAcl = storageSnippets.getBucketAcl(BUCKET, USER_EMAIL); assertNotNull(userAcl); - assertEquals(USER_EMAIL, ((User)userAcl.getEntity()).getEmail()); + assertEquals(USER_EMAIL, ((User) userAcl.getEntity()).getEmail()); assertTrue(storageSnippets.deleteBucketAcl(BUCKET)); assertNull(storageSnippets.getBucketAcl(BUCKET)); @@ -371,21 +371,23 @@ public void testBlobAcl() { assertNotNull(storageSnippets.createBlobAcl(BUCKET, blobName, createdBlob.getGeneration())); Acl updatedAcl = storageSnippets.updateBlobAcl(BUCKET, blobName, createdBlob.getGeneration()); assertEquals(Acl.Role.OWNER, updatedAcl.getRole()); - Set acls = Sets.newHashSet( - storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration())); + Set acls = + Sets.newHashSet( + storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration())); assertTrue(acls.contains(updatedAcl)); assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL)); storage.createAcl(BlobId.of(BUCKET, blobName), Acl.of(new User(USER_EMAIL), Role.READER)); Acl userAcl = storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL); assertNotNull(userAcl); - assertEquals(USER_EMAIL, ((User)userAcl.getEntity()).getEmail()); + assertEquals(USER_EMAIL, ((User) userAcl.getEntity()).getEmail()); updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration()); assertEquals(Acl.Role.READER, updatedAcl.getRole()); assertEquals(User.ofAllUsers(), updatedAcl.getEntity()); - acls = Sets.newHashSet( - storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration())); + acls = + Sets.newHashSet( + storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration())); assertTrue(acls.contains(updatedAcl)); assertNotNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration())); @@ -421,6 +423,18 @@ public void testAuthListBuckets() { assertNotNull(bucket); } + @Test + public void testBlobDownload() throws Exception { + String blobName = "test-create-empty-blob"; + BlobId blobId = BlobId.of(BUCKET, blobName); + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); + Blob remoteBlob = storage.create(blobInfo, BLOB_BYTE_CONTENT); + assertNotNull(remoteBlob); + storageSnippets.downloadFile(BUCKET, blobName, Paths.get(blobName)); + byte[] readBytes = Files.readAllBytes(Paths.get(blobName)); + assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); + } + @Test public void testRequesterPays() throws Exception { Bucket bucket = storageSnippets.enableRequesterPays(BUCKET); @@ -429,11 +443,11 @@ public void testRequesterPays() throws Exception { assertTrue(bucket.requesterPays()); String projectId = ServiceOptions.getDefaultProjectId(); String blobName = "test-create-empty-blob-requester-pays"; - Blob remoteBlob = bucket.create(blobName, BLOB_BYTE_CONTENT, - BlobTargetOption.userProject(projectId)); + Blob remoteBlob = + bucket.create(blobName, BLOB_BYTE_CONTENT, BlobTargetOption.userProject(projectId)); assertNotNull(remoteBlob); - storageSnippets.downloadFileUsingRequesterPays(projectId, BUCKET, blobName, - Paths.get(blobName)); + storageSnippets.downloadFileUsingRequesterPays( + projectId, BUCKET, blobName, Paths.get(blobName)); byte[] readBytes = Files.readAllBytes(Paths.get(blobName)); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); bucket = storageSnippets.disableRequesterPays(BUCKET); @@ -441,10 +455,10 @@ public void testRequesterPays() throws Exception { } @Test - public void testDefaultKMSKey(){ + public void testDefaultKMSKey() { Bucket bucket = storageSnippets.setDefaultKmsKey(BUCKET, KMS_KEY_NAME); assertEquals(KMS_KEY_NAME, bucket.getDefaultKmsKeyName()); // Remove default key - storageSnippets.setDefaultKmsKey(BUCKET,null); + storageSnippets.setDefaultKmsKey(BUCKET, null); } }