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 88780cce0956..45ca7d14e759 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 @@ -908,6 +908,21 @@ public Acl updateBlobAcl(String bucketName, String blobName, long blobGeneration return acl; } + /** + * Example of updating a blob to be public-read. + */ + // [TARGET createAcl(BlobId, Acl)] + // [VARIABLE "my_unique_bucket"] + // [VARIABLE "my_blob_name"] + // [VARIABLE 42] + public Acl blobToPublicRead(String bucketName, String blobName, long blobGeneration) { + // [START storageMakePublic] + BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration); + Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER)); + // [END storageMakePublic] + return acl; + } + /** * Example of listing the ACL entries for a blob. */ 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 d215b2e5b908..98a8a10021dc 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 @@ -27,6 +27,7 @@ import com.google.cloud.Page; import com.google.cloud.storage.Acl; +import com.google.cloud.storage.Acl.User; import com.google.cloud.storage.Blob; import com.google.cloud.storage.BlobId; import com.google.cloud.storage.BlobInfo; @@ -319,6 +320,14 @@ public void testBlobAcl() { Set acls = Sets.newHashSet( storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration())); assertTrue(acls.contains(updatedAcl)); + + updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration()); + assertEquals(Acl.Role.READER, updatedAcl.getRole()); + assertEquals(User.ofAllUsers(), updatedAcl.getEntity()); + acls = Sets.newHashSet( + storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration())); + assertTrue(acls.contains(updatedAcl)); + assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration())); assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration())); // test non-existing blob diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index 1cb268e81d7d..286f384934c2 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -2456,6 +2456,15 @@ public static Builder newBuilder() { * Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllAuthenticatedUsers(), Role.READER)); * } * + *

Example of updating a blob to be public-read. + *

 {@code
+   * String bucketName = "my_unique_bucket";
+   * String blobName = "my_blob_name";
+   * long blobGeneration = 42;
+   * BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
+   * Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER));
+   * }
+ * * @throws StorageException upon failure */ Acl createAcl(BlobId blob, Acl acl);