Skip to content

Commit f7afed1

Browse files
author
Jerjou Cheng
committed
Sample for making GCS object public-read
1 parent 81278dc commit f7afed1

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java

+15
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,21 @@ public Acl updateBlobAcl(String bucketName, String blobName, long blobGeneration
908908
return acl;
909909
}
910910

911+
/**
912+
* Example of updating a blob to be public-read.
913+
*/
914+
// [TARGET createAcl(BlobId, Acl)]
915+
// [VARIABLE "my_unique_bucket"]
916+
// [VARIABLE "my_blob_name"]
917+
// [VARIABLE 42]
918+
public Acl blobToPublicRead(String bucketName, String blobName, long blobGeneration) {
919+
// [START storageMakePublic]
920+
BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
921+
Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER));
922+
// [END storageMakePublic]
923+
return acl;
924+
}
925+
911926
/**
912927
* Example of listing the ACL entries for a blob.
913928
*/

google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.google.cloud.Page;
2929
import com.google.cloud.storage.Acl;
30+
import com.google.cloud.storage.Acl.User;
3031
import com.google.cloud.storage.Blob;
3132
import com.google.cloud.storage.BlobId;
3233
import com.google.cloud.storage.BlobInfo;
@@ -319,6 +320,14 @@ public void testBlobAcl() {
319320
Set<Acl> acls = Sets.newHashSet(
320321
storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
321322
assertTrue(acls.contains(updatedAcl));
323+
324+
updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration());
325+
assertEquals(Acl.Role.READER, updatedAcl.getRole());
326+
assertEquals(User.ofAllUsers(), updatedAcl.getEntity());
327+
acls = Sets.newHashSet(
328+
storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
329+
assertTrue(acls.contains(updatedAcl));
330+
322331
assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
323332
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
324333
// test non-existing blob

google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java

+9
Original file line numberDiff line numberDiff line change
@@ -2456,6 +2456,15 @@ public static Builder newBuilder() {
24562456
* Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllAuthenticatedUsers(), Role.READER));
24572457
* }</pre>
24582458
*
2459+
* <p>Example of updating a blob to be public-read.
2460+
* <pre> {@code
2461+
* String bucketName = "my_unique_bucket";
2462+
* String blobName = "my_blob_name";
2463+
* long blobGeneration = 42;
2464+
* BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
2465+
* Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER));
2466+
* }</pre>
2467+
*
24592468
* @throws StorageException upon failure
24602469
*/
24612470
Acl createAcl(BlobId blob, Acl acl);

0 commit comments

Comments
 (0)