From d8e2c5ccb83d928a0e9d3a5a6c8c86440a2eac99 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Thu, 17 Jan 2019 13:00:02 -0800 Subject: [PATCH 1/4] implement BucketPolicyOnly --- .../java/com/google/cloud/storage/Bucket.java | 6 + .../com/google/cloud/storage/BucketInfo.java | 165 ++++++++++++++++++ .../com/google/cloud/storage/Storage.java | 3 +- .../cloud/storage/it/ITStorageTest.java | 41 +++++ google-cloud-clients/pom.xml | 2 +- 5 files changed, 215 insertions(+), 2 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index b10f56194423..30f9ce61f1fc 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -655,6 +655,12 @@ public Builder setRetentionPeriod(Long retentionPeriod) { return this; } + @Override + public Builder setIamConfiguration(IamConfiguration iamConfiguration) { + infoBuilder.setIamConfiguration(iamConfiguration); + return this; + } + @Override public Bucket build() { return new Bucket(storage, infoBuilder); diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java index caceb2f19d2a..ad8b72d0738d 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java @@ -95,6 +95,147 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo) private final Long retentionEffectiveTime; private final Boolean retentionPolicyIsLocked; private final Long retentionPeriod; + private final IamConfiguration iamConfiguration; + + public static class IamConfiguration implements Serializable { + private static final long serialVersionUID = -8671736104909424616L; + + private final BucketPolicyOnly bucketPolicyOnly; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) { + return false; + } + IamConfiguration other = (IamConfiguration) o; + return Objects.equals(bucketPolicyOnly.toPb(), other.getBucketPolicyOnly().toPb()); + } + + @Override + public int hashCode() { + return Objects.hash(bucketPolicyOnly); + } + + private IamConfiguration(Builder builder) { + this.bucketPolicyOnly = builder.bucketPolicyOnly; + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + Builder builder = new Builder(); + builder.bucketPolicyOnly = this.bucketPolicyOnly; + return builder; + } + + public BucketPolicyOnly getBucketPolicyOnly() { + return bucketPolicyOnly; + } + + Bucket.IamConfiguration toPb() { + Bucket.IamConfiguration iamConfiguration = new Bucket.IamConfiguration(); + iamConfiguration.setBucketPolicyOnly(this.bucketPolicyOnly.toPb()); + + return iamConfiguration; + } + + static IamConfiguration fromPb(Bucket.IamConfiguration iamConfiguration) { + return newBuilder() + .setBucketPolicyOnly(BucketPolicyOnly.fromPb(iamConfiguration.getBucketPolicyOnly())) + .build(); + } + + public static class Builder { + private BucketPolicyOnly bucketPolicyOnly; + + public Builder setBucketPolicyOnly(BucketPolicyOnly bucketPolicyOnly) { + this.bucketPolicyOnly = bucketPolicyOnly; + return this; + } + + public IamConfiguration build() { + return new IamConfiguration(this); + } + } + } + + public static class BucketPolicyOnly implements Serializable { + private static final long serialVersionUID = 2260445211318576550L; + private final Boolean enabled; + private final DateTime lockedTime; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) { + return false; + } + BucketPolicyOnly other = (BucketPolicyOnly) o; + return Objects.equals(toPb(), other.toPb()); + } + + @Override + public int hashCode() { + return Objects.hash(enabled, lockedTime); + } + + public Boolean getEnabled() { + return enabled; + } + + public DateTime getLockedTime() { + return lockedTime; + } + + public static Builder newBuilder() { + return new Builder(); + } + + Bucket.IamConfiguration.BucketPolicyOnly toPb() { + Bucket.IamConfiguration.BucketPolicyOnly bucketPolicyOnly = new Bucket.IamConfiguration.BucketPolicyOnly(); + + bucketPolicyOnly.setEnabled(enabled); + bucketPolicyOnly.setLockedTime(lockedTime); + + return bucketPolicyOnly; + } + + static BucketPolicyOnly fromPb(Bucket.IamConfiguration.BucketPolicyOnly bucketPolicyOnly) { + return newBuilder() + .setEnabled(bucketPolicyOnly.getEnabled()) + .setLockedTime(bucketPolicyOnly.getLockedTime()) + .build(); + } + + private BucketPolicyOnly(Builder builder) { + this.enabled = builder.enabled; + this.lockedTime = builder.lockedTime; + } + + public static class Builder { + private Boolean enabled; + private DateTime lockedTime; + + private Builder() {} + + public Builder setEnabled(Boolean enabled) { + this.enabled = enabled; + return this; + } + + Builder setLockedTime(DateTime lockedTime) { + this.lockedTime = lockedTime; + return this; + } + + public BucketPolicyOnly build() { + return new BucketPolicyOnly(this); + } + } + } /** * Lifecycle rule for a bucket. Allows supported Actions, such as deleting and changing storage @@ -786,6 +927,9 @@ public abstract static class Builder { @BetaApi public abstract Builder setRetentionPeriod(Long retentionPeriod); + @BetaApi + public abstract Builder setIamConfiguration(IamConfiguration iamConfiguration); + /** Creates a {@code BucketInfo} object. */ public abstract BucketInfo build(); } @@ -816,6 +960,7 @@ static final class BuilderImpl extends Builder { private Long retentionEffectiveTime; private Boolean retentionPolicyIsLocked; private Long retentionPeriod; + private IamConfiguration iamConfiguration; BuilderImpl(String name) { this.name = name; @@ -846,6 +991,7 @@ static final class BuilderImpl extends Builder { retentionEffectiveTime = bucketInfo.retentionEffectiveTime; retentionPolicyIsLocked = bucketInfo.retentionPolicyIsLocked; retentionPeriod = bucketInfo.retentionPeriod; + iamConfiguration = bucketInfo.iamConfiguration; } @Override @@ -998,6 +1144,12 @@ public Builder setRetentionPeriod(Long retentionPeriod) { return this; } + @Override + public Builder setIamConfiguration(IamConfiguration iamConfiguration) { + this.iamConfiguration = iamConfiguration; + return this; + } + @Override public BucketInfo build() { checkNotNull(name); @@ -1030,6 +1182,7 @@ public BucketInfo build() { retentionEffectiveTime = builder.retentionEffectiveTime; retentionPolicyIsLocked = builder.retentionPolicyIsLocked; retentionPeriod = builder.retentionPeriod; + iamConfiguration = builder.iamConfiguration; } /** Returns the service-generated id for the bucket. */ @@ -1268,6 +1421,11 @@ public Long getRetentionPeriod() { return retentionPeriod; } + @BetaApi + public IamConfiguration getIamConfiguration() { + return iamConfiguration; + } + /** Returns a builder for the current bucket. */ public Builder toBuilder() { return new BuilderImpl(this); @@ -1405,6 +1563,9 @@ public Rule apply(LifecycleRule lifecycleRule) { bucketPb.setRetentionPolicy(retentionPolicy); } } + if (iamConfiguration != null) { + bucketPb.setIamConfiguration(iamConfiguration.toPb()); + } return bucketPb; } @@ -1526,6 +1687,10 @@ public DeleteRule apply(Rule rule) { builder.setRetentionPeriod(retentionPolicy.getRetentionPeriod()); } } + Bucket.IamConfiguration iamConfiguration = bucketPb.getIamConfiguration(); + if (iamConfiguration != null) { + builder.setIamConfiguration(IamConfiguration.fromPb(iamConfiguration)); + } return builder.build(); } } diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index 6ce9ca8c6e2a..6a8696748784 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -96,7 +96,8 @@ enum BucketField implements FieldSelector { ENCRYPTION("encryption"), BILLING("billing"), DEFAULT_EVENT_BASED_HOLD("defaultEventBasedHold"), - RETENTION_POLICY("retentionPolicy"); + RETENTION_POLICY("retentionPolicy"), + IAMCONFIGURATION("iamConfiguration"); static final List REQUIRED_FIELDS = ImmutableList.of(NAME); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 3a97dab2800d..4171069e563c 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -2442,4 +2442,45 @@ public void testGetServiceAccount() { assertNotNull(serviceAccount); assertTrue(serviceAccount.getEmail().endsWith(SERVICE_ACCOUNT_EMAIL_SUFFIX)); } + + @Test + public void testCreateBucketWithBucketPolicyOnly() { + String bpoBucket = RemoteStorageHelper.generateBucketName(); + try { + storage.create( + Bucket.newBuilder(bpoBucket) + .setIamConfiguration( + BucketInfo.IamConfiguration.newBuilder().setBucketPolicyOnly( + BucketInfo.BucketPolicyOnly.newBuilder() + .setEnabled(true) + .build()) + .build()) + .build()); + + Bucket remoteBucket = storage.get(bpoBucket, Storage.BucketGetOption.fields(BucketField.IAMCONFIGURATION)); + + assertTrue(remoteBucket.getIamConfiguration().getBucketPolicyOnly().getEnabled()); + assertNotNull(remoteBucket.getIamConfiguration().getBucketPolicyOnly().getLockedTime()); + + } finally { + storage.delete(bpoBucket); + } + } + + @Test + public void testAddBucketPolicyOnlyToExistingBucket() { + String bpoBucket = RemoteStorageHelper.generateBucketName(); + try { + Bucket remoteBucket = storage.create(Bucket.newBuilder(bpoBucket).build()); + remoteBucket.toBuilder().setIamConfiguration( + BucketInfo.IamConfiguration.newBuilder().setBucketPolicyOnly( + BucketInfo.BucketPolicyOnly.newBuilder() + .setEnabled(true) + .build()) + .build()) + .build().update(); + } finally { + storage.delete(bpoBucket); + } + } } diff --git a/google-cloud-clients/pom.xml b/google-cloud-clients/pom.xml index c01bdbea913f..f9088c39c7ee 100644 --- a/google-cloud-clients/pom.xml +++ b/google-cloud-clients/pom.xml @@ -228,7 +228,7 @@ com.google.apis google-api-services-storage - v1-rev20181013-1.27.0 + v1-rev20181109-1.27.0 com.google.apis From c8e19c04b2f0ad211eb2a3569d97b9a20fd990f4 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 28 Jan 2019 10:44:50 -0800 Subject: [PATCH 2/4] [Storage] Bucket Policy Only samples (#4408) * Humble beginnings for BPO samples with tests * Update samples per client changes --- .../storage/snippets/StorageSnippets.java | 335 ++++++++++++++---- .../storage/snippets/ITStorageSnippets.java | 15 + 2 files changed, 284 insertions(+), 66 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 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()); + } } From b4250c466c3b1a6e279817eb7e691b464dca1bc2 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 6 Feb 2019 11:31:27 -0800 Subject: [PATCH 3/4] Fix format issue --- .../storage/snippets/StorageSnippets.java | 291 +++++------------- 1 file changed, 77 insertions(+), 214 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 b82184df6721..86325ebc7910 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 @@ -71,9 +71,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; @@ -82,9 +80,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) { @@ -94,9 +90,7 @@ 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) { @@ -113,9 +107,7 @@ 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"] @@ -128,9 +120,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 +133,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 +147,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"] @@ -177,9 +163,7 @@ 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); @@ -231,9 +215,7 @@ 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"] @@ -262,9 +244,7 @@ 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) { @@ -280,9 +260,7 @@ 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/"] @@ -298,9 +276,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) { @@ -311,9 +287,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"] @@ -408,9 +382,7 @@ 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"] @@ -427,9 +399,7 @@ 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"] @@ -451,9 +421,7 @@ 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"] @@ -470,9 +438,7 @@ 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"] @@ -493,9 +459,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"] @@ -550,9 +514,7 @@ 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"] @@ -565,9 +527,7 @@ 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"] @@ -597,9 +557,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"] @@ -616,9 +574,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"] @@ -636,9 +592,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"] @@ -694,9 +648,7 @@ 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"] @@ -710,9 +662,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"] @@ -727,9 +677,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"] @@ -746,9 +694,7 @@ 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"] @@ -765,9 +711,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"] @@ -781,9 +725,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"] @@ -798,9 +740,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) { @@ -810,9 +750,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"] @@ -823,9 +761,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) { @@ -840,9 +776,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) { @@ -852,9 +786,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) { @@ -864,9 +796,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) { @@ -879,9 +809,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) { @@ -891,9 +819,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) { @@ -908,9 +834,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) { @@ -921,9 +845,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) { @@ -934,9 +856,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) { @@ -949,9 +869,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"] @@ -964,9 +882,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"] @@ -979,9 +895,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"] @@ -999,9 +913,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"] @@ -1014,9 +926,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"] @@ -1029,9 +939,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"] @@ -1044,9 +952,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"] @@ -1062,9 +968,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 @@ -1080,9 +984,7 @@ 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 @@ -1100,9 +1002,7 @@ 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 @@ -1120,9 +1020,7 @@ 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 @@ -1138,9 +1036,7 @@ 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] @@ -1164,9 +1060,7 @@ 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 { @@ -1194,9 +1088,7 @@ 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 @@ -1220,9 +1112,7 @@ 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 @@ -1276,9 +1166,7 @@ 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] @@ -1304,9 +1192,7 @@ 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] @@ -1330,9 +1216,7 @@ 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 @@ -1355,9 +1239,7 @@ 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 @@ -1378,9 +1260,7 @@ 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 @@ -1397,9 +1277,7 @@ 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 @@ -1416,9 +1294,7 @@ 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 @@ -1439,9 +1315,7 @@ 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 @@ -1461,9 +1335,7 @@ 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 @@ -1483,9 +1355,7 @@ 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 @@ -1505,9 +1375,7 @@ 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 @@ -1527,9 +1395,7 @@ public Blob releaseTemporaryHold(String bucketName, String blobName) throws Stor return blob; } - /** - * Example of how to enable Bucket Policy Only for a bucket - */ + /** 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 @@ -1538,19 +1404,18 @@ public Bucket enableBucketPolicyOnly(String bucketName) throws StorageException // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; - BucketInfo.IamConfiguration iamConfiguration = BucketInfo.IamConfiguration.newBuilder().setIsBucketPolicyOnlyEnabled( - true).build(); + BucketInfo.IamConfiguration iamConfiguration = + BucketInfo.IamConfiguration.newBuilder().setIsBucketPolicyOnlyEnabled(true).build(); Bucket bucket = - storage.update(BucketInfo.newBuilder(bucketName).setIamConfiguration(iamConfiguration).build()); + 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 - */ + /** 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 @@ -1559,19 +1424,18 @@ public Bucket disableBucketPolicyOnly(String bucketName) throws StorageException // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; - BucketInfo.IamConfiguration iamConfiguration = BucketInfo.IamConfiguration.newBuilder().setIsBucketPolicyOnlyEnabled( - false).build(); + BucketInfo.IamConfiguration iamConfiguration = + BucketInfo.IamConfiguration.newBuilder().setIsBucketPolicyOnlyEnabled(false).build(); Bucket bucket = - storage.update(BucketInfo.newBuilder(bucketName).setIamConfiguration(iamConfiguration).build()); + 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 - */ + /** 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 @@ -1595,5 +1459,4 @@ public Bucket getBucketPolicyOnly(String bucketName) throws StorageException { // [END storage_get_bucket_policy_only] return bucket; } - } From 688c28560259759ba243a7dac0a12fc307932f27 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 6 Feb 2019 12:14:02 -0800 Subject: [PATCH 4/4] Lint fix --- .../google/cloud/examples/storage/snippets/StorageSnippets.java | 1 - 1 file changed, 1 deletion(-) 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 86325ebc7910..ccec623a1812 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,7 +56,6 @@ 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;