Skip to content

Commit

Permalink
Sample for making GCS object public-read
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerjou Cheng authored and jerjou committed Nov 11, 2016
1 parent 89f83f3 commit 32c717f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Expand Up @@ -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.
*/
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -319,6 +320,14 @@ public void testBlobAcl() {
Set<Acl> 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
Expand Down
Expand Up @@ -2456,6 +2456,15 @@ public static Builder newBuilder() {
* Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllAuthenticatedUsers(), Role.READER));
* }</pre>
*
* <p>Example of updating a blob to be public-read.
* <pre> {@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));
* }</pre>
*
* @throws StorageException upon failure
*/
Acl createAcl(BlobId blob, Acl acl);
Expand Down

0 comments on commit 32c717f

Please sign in to comment.