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 ab2ec4df8edf..b82184df6721 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 @@ -56,6 +56,7 @@ import com.google.cloud.storage.StorageClass; import com.google.cloud.storage.StorageException; import com.google.cloud.storage.StorageOptions; + import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.IOException; @@ -70,7 +71,9 @@ 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; @@ -79,7 +82,9 @@ 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) { @@ -89,7 +94,9 @@ 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) { @@ -106,7 +113,9 @@ public Bucket createBucketWithStorageClassAndLocation(String bucketName) { 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"] @@ -119,7 +128,9 @@ 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"] @@ -132,7 +143,9 @@ 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"] @@ -146,7 +159,9 @@ 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"] @@ -162,7 +177,9 @@ public Blob createEncryptedBlob(String bucketName, String blobName, String encry 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); @@ -214,7 +231,9 @@ public Blob 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"] @@ -243,7 +262,9 @@ public Blob getBlobFromIdWithMetageneration( 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) { @@ -259,7 +280,9 @@ 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/"] @@ -275,7 +298,9 @@ 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) { @@ -286,7 +311,9 @@ 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"] @@ -381,7 +408,9 @@ public boolean deleteBlobFromIdWithGeneration( return deleted; } - /** Example of deleting a blob. */ + /** + * Example of deleting a blob. + */ // [TARGET delete(BlobId)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -398,7 +427,9 @@ 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"] @@ -420,7 +451,9 @@ public Blob composeBlobs( return blob; } - /** Example of copying a blob. */ + /** + * Example of copying a blob. + */ // [TARGET copy(CopyRequest)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] @@ -437,7 +470,9 @@ public Blob copyBlob(String bucketName, String blobName, String copyBlobName) { 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"] @@ -458,7 +493,9 @@ 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"] @@ -513,7 +550,9 @@ 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"] @@ -526,7 +565,9 @@ public byte[] readEncryptedBlob(String bucketName, String blobName, String decry 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"] @@ -556,7 +597,9 @@ 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"] @@ -573,7 +616,9 @@ 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"] @@ -591,7 +636,9 @@ 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"] @@ -647,7 +694,9 @@ public URL signUrlWithSigner(String bucketName, String blobName, String keyPath) 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"] @@ -661,7 +710,9 @@ 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"] @@ -676,7 +727,9 @@ 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"] @@ -693,7 +746,9 @@ public List batchUpdate(String bucketName, String blobName1, String blobNa 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"] @@ -710,7 +765,9 @@ 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"] @@ -724,7 +781,9 @@ 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"] @@ -739,7 +798,9 @@ 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) { @@ -749,7 +810,9 @@ 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"] @@ -760,7 +823,9 @@ 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) { @@ -775,7 +840,9 @@ 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) { @@ -785,7 +852,9 @@ 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) { @@ -795,7 +864,9 @@ 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) { @@ -808,7 +879,9 @@ 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) { @@ -818,7 +891,9 @@ 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) { @@ -833,7 +908,9 @@ 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) { @@ -844,7 +921,9 @@ 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) { @@ -855,7 +934,9 @@ 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) { @@ -868,7 +949,9 @@ 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"] @@ -881,7 +964,9 @@ 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"] @@ -894,7 +979,9 @@ 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"] @@ -912,7 +999,9 @@ 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"] @@ -925,7 +1014,9 @@ 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"] @@ -938,7 +1029,9 @@ 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"] @@ -951,7 +1044,9 @@ 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"] @@ -967,7 +1062,9 @@ 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 @@ -983,7 +1080,9 @@ public Page authListBuckets() { return buckets; } - /** Example of enabling Requester pays on a bucket. */ + /** + * 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 @@ -1001,7 +1100,9 @@ public Bucket enableRequesterPays(String bucketName) throws StorageException { 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 @@ -1019,7 +1120,9 @@ public Bucket disableRequesterPays(String bucketName) { 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 @@ -1035,7 +1138,9 @@ public Bucket getRequesterPaysStatus(String bucketName) throws StorageException return bucket; } - /** Example of downloading a file. */ + /** + * Example of downloading a file. + */ public void downloadFile(String bucketName, String srcFilename, Path destFilePath) throws IOException { // [START storage_download_file] @@ -1059,7 +1164,9 @@ public void downloadFile(String bucketName, String srcFilename, Path destFilePat // [END storage_download_file] } - /** Example of downloading a file using Requester pay. */ + /** + * Example of downloading a file using Requester pay. + */ public void downloadFileUsingRequesterPays( String projectId, String bucketName, String srcFilename, Path destFilePath) throws IOException { @@ -1087,7 +1194,9 @@ public void downloadFileUsingRequesterPays( // [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 @@ -1111,7 +1220,9 @@ public Bucket setDefaultKmsKey(String bucketName, String kmsKeyName) throws Stor return bucket; } - /** Example of displaying Blob metadata */ + /** + * Example of displaying Blob metadata + */ public void getBlobMetadata(String bucketName, String blobName) throws StorageException { // [START storage_get_metadata] // Instantiate a Google Cloud Storage client @@ -1165,7 +1276,9 @@ public void getBlobMetadata(String bucketName, String blobName) throws StorageEx // [END storage_get_metadata] } - /** Example of setting a retention policy on a bucket */ + /** + * Example of setting a retention policy on a bucket + */ public Bucket setRetentionPolicy(String bucketName, Long retentionPeriod) throws StorageException { // [START storage_set_retention_policy] @@ -1191,7 +1304,9 @@ public Bucket setRetentionPolicy(String bucketName, Long retentionPeriod) return bucketWithRetentionPolicy; } - /** Example of removing a retention policy on a bucket */ + /** + * Example of removing a retention policy on a bucket + */ public Bucket removeRetentionPolicy(String bucketName) throws StorageException, IllegalArgumentException { // [START storage_remove_retention_policy] @@ -1215,7 +1330,9 @@ public Bucket removeRetentionPolicy(String bucketName) return bucketWithoutRetentionPolicy; } - /** Example of how to get a bucket's retention policy */ + /** + * Example of how to get a bucket's retention policy + */ public Bucket getRetentionPolicy(String bucketName) throws StorageException { // [START storage_get_retention_policy] // Instantiate a Google Cloud Storage client @@ -1238,7 +1355,9 @@ public Bucket getRetentionPolicy(String bucketName) throws StorageException { return bucket; } - /** Example of how to lock a bucket retention policy */ + /** + * Example of how to lock a bucket retention policy + */ public Bucket lockRetentionPolicy(String bucketName) throws StorageException { // [START storage_lock_retention_policy] // Instantiate a Google Cloud Storage client @@ -1259,7 +1378,9 @@ public Bucket lockRetentionPolicy(String bucketName) throws StorageException { return lockedBucket; } - /** Example of how to enable default event-based hold for a bucket */ + /** + * Example of how to enable default event-based hold for a bucket + */ public Bucket enableDefaultEventBasedHold(String bucketName) throws StorageException { // [START storage_enable_default_event_based_hold] // Instantiate a Google Cloud Storage client @@ -1276,7 +1397,9 @@ public Bucket enableDefaultEventBasedHold(String bucketName) throws StorageExcep return bucket; } - /** Example of how to disable default event-based hold for a bucket */ + /** + * Example of how to disable default event-based hold for a bucket + */ public Bucket disableDefaultEventBasedHold(String bucketName) throws StorageException { // [START storage_disable_default_event_based_hold] // Instantiate a Google Cloud Storage client @@ -1293,7 +1416,9 @@ public Bucket disableDefaultEventBasedHold(String bucketName) throws StorageExce return bucket; } - /** Example of how to get default event-based hold for a bucket */ + /** + * Example of how to get default event-based hold for a bucket + */ public Bucket getDefaultEventBasedHold(String bucketName) throws StorageException { // [START storage_get_default_event_based_hold] // Instantiate a Google Cloud Storage client @@ -1314,7 +1439,9 @@ public Bucket getDefaultEventBasedHold(String bucketName) throws StorageExceptio return bucket; } - /** Example of how to set event-based hold for a blob */ + /** + * 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 @@ -1334,7 +1461,9 @@ public Blob setEventBasedHold(String bucketName, String blobName) throws Storage return blob; } - /** Example of how to release event-based hold for a blob */ + /** + * Example of how to release event-based hold for a blob + */ public Blob releaseEventBasedHold(String bucketName, String blobName) throws StorageException { // [START storage_release_event_based_hold] // Instantiate a Google Cloud Storage client @@ -1354,7 +1483,9 @@ public Blob releaseEventBasedHold(String bucketName, String blobName) throws Sto return blob; } - /** Example of how to set a temporary hold for a blob */ + /** + * Example of how to set a temporary hold for a blob + */ public Blob setTemporaryHold(String bucketName, String blobName) throws StorageException { // [START storage_set_temporary_hold] // Instantiate a Google Cloud Storage client @@ -1374,7 +1505,9 @@ public Blob setTemporaryHold(String bucketName, String blobName) throws StorageE return blob; } - /** Example of how to release a temporary hold for a blob */ + /** + * Example of how to release a temporary hold for a blob + */ public Blob releaseTemporaryHold(String bucketName, String blobName) throws StorageException { // [START storage_release_temporary_hold] // Instantiate a Google Cloud Storage client @@ -1393,4 +1526,74 @@ public Blob releaseTemporaryHold(String bucketName, String blobName) throws Stor // [END storage_release_temporary_hold] return blob; } + + /** + * Example of how to enable Bucket Policy Only for a bucket + */ + public Bucket enableBucketPolicyOnly(String bucketName) throws StorageException { + // [START storage_enable_bucket_policy_only] + // Instantiate a Google Cloud Storage client + Storage storage = StorageOptions.getDefaultInstance().getService(); + + // The name of a bucket, e.g. "my-bucket" + // String bucketName = "my-bucket"; + + BucketInfo.IamConfiguration iamConfiguration = BucketInfo.IamConfiguration.newBuilder().setIsBucketPolicyOnlyEnabled( + true).build(); + Bucket bucket = + storage.update(BucketInfo.newBuilder(bucketName).setIamConfiguration(iamConfiguration).build()); + + System.out.println("Bucket Policy Only was enabled for " + bucketName); + // [END storage_enable_bucket_policy_only] + return bucket; + } + + /** + * Example of how to disable Bucket Policy Only for a bucket + */ + public Bucket disableBucketPolicyOnly(String bucketName) throws StorageException { + // [START storage_disable_bucket_policy_only] + // Instantiate a Google Cloud Storage client + Storage storage = StorageOptions.getDefaultInstance().getService(); + + // The name of a bucket, e.g. "my-bucket" + // String bucketName = "my-bucket"; + + BucketInfo.IamConfiguration iamConfiguration = BucketInfo.IamConfiguration.newBuilder().setIsBucketPolicyOnlyEnabled( + false).build(); + Bucket bucket = + storage.update(BucketInfo.newBuilder(bucketName).setIamConfiguration(iamConfiguration).build()); + + System.out.println("Bucket Policy Only was disabled for " + bucketName); + // [END storage_disable_bucket_policy_only] + return bucket; + } + + /** + * Example of how to get Bucket Policy Only metadata for a bucket + */ + public Bucket getBucketPolicyOnly(String bucketName) throws StorageException { + // [START storage_get_bucket_policy_only] + // Instantiate a Google Cloud Storage client + Storage storage = StorageOptions.getDefaultInstance().getService(); + + // The name of a bucket, e.g. "my-bucket" + // String bucketName = "my-bucket"; + + Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.IAMCONFIGURATION)); + BucketInfo.IamConfiguration iamConfiguration = bucket.getIamConfiguration(); + + Boolean enabled = iamConfiguration.isBucketPolicyOnlyEnabled(); + Date lockedTime = new Date(iamConfiguration.getBucketPolicyOnlyLockedTime()); + + if (enabled != null && enabled) { + System.out.println("Bucket Policy Only is enabled for " + bucketName); + System.out.println("Bucket will be locked on " + lockedTime); + } else { + System.out.println("Bucket Policy Only is disabled for " + bucketName); + } + // [END storage_get_bucket_policy_only] + return bucket; + } + } 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 19db91dcf44b..fe618755d5cd 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 @@ -546,4 +546,19 @@ public void testLockRetentionPolicy() { bucket = storageSnippets.lockRetentionPolicy(tempBucket); assertTrue(bucket.retentionPolicyIsLocked()); } + + @Test + public void testBucketPolicyOnly() { + String tempBucket = RemoteStorageHelper.generateBucketName(); + Bucket bucket = storageSnippets.createBucket(tempBucket); + assertNotNull(bucket); + bucket = storageSnippets.enableBucketPolicyOnly(tempBucket); + assertTrue(bucket.getIamConfiguration().isBucketPolicyOnlyEnabled()); + assertNotNull(bucket.getIamConfiguration().getBucketPolicyOnlyLockedTime()); + bucket = storageSnippets.getBucketPolicyOnly(tempBucket); + assertTrue(bucket.getIamConfiguration().isBucketPolicyOnlyEnabled()); + assertNotNull(bucket.getIamConfiguration().getBucketPolicyOnlyLockedTime()); + bucket = storageSnippets.disableBucketPolicyOnly(tempBucket); + assertFalse(bucket.getIamConfiguration().isBucketPolicyOnlyEnabled()); + } }