Skip to content

Commit 3f83c1b

Browse files
author
Jerjou Cheng
committed
Example for getting ACL of blob for specific user
1 parent 9f0486e commit 3f83c1b

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,21 @@ public Acl getBlobAcl(String bucketName, String blobName, long blobGeneration) {
871871
return acl;
872872
}
873873

874+
/**
875+
* Example of getting the ACL entry for a specific user on a blob.
876+
*/
877+
// [TARGET getAcl(BlobId, Entity)]
878+
// [VARIABLE "my_unique_bucket"]
879+
// [VARIABLE "my_blob_name"]
880+
// [VARIABLE "google-cloud-java-tests@java-docs-samples-tests.iam.gserviceaccount.com"]
881+
public Acl getBlobAcl(String bucketName, String blobName, String userEmail) {
882+
// [START storagePrintFileAclForUser]
883+
BlobId blobId = BlobId.of(bucketName, blobName);
884+
Acl acl = storage.getAcl(blobId, new User(userEmail));
885+
// [END storagePrintFileAclForUser]
886+
return acl;
887+
}
888+
874889
/**
875890
* Example of deleting the ACL entry for an entity on a blob.
876891
*/

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,20 @@ public void testBlobAcl() {
332332
storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
333333
assertTrue(acls.contains(updatedAcl));
334334

335+
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL));
336+
storage.createAcl(BlobId.of(BUCKET, blobName), Acl.of(new User(USER_EMAIL), Role.READER));
337+
Acl userAcl = storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL);
338+
assertNotNull(userAcl);
339+
assertEquals(USER_EMAIL, ((User)userAcl.getEntity()).getEmail());
340+
335341
updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration());
336342
assertEquals(Acl.Role.READER, updatedAcl.getRole());
337343
assertEquals(User.ofAllUsers(), updatedAcl.getEntity());
338344
acls = Sets.newHashSet(
339345
storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
340346
assertTrue(acls.contains(updatedAcl));
341347

348+
assertNotNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
342349
assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
343350
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
344351
// test non-existing blob

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,15 @@ public static Builder newBuilder() {
24252425
* Acl acl = storage.getAcl(blobId, User.ofAllAuthenticatedUsers());
24262426
* }</pre>
24272427
*
2428+
* <p>Example of getting the ACL entry for a specific user on a blob.
2429+
* <pre> {@code
2430+
* String bucketName = "my_unique_bucket";
2431+
* String blobName = "my_blob_name";
2432+
* String userEmail = "google-cloud-java-tests@java-docs-samples-tests.iam.gserviceaccount.com";
2433+
* BlobId blobId = BlobId.of(bucketName, blobName);
2434+
* Acl acl = storage.getAcl(blobId, new User(userEmail));
2435+
* }</pre>
2436+
*
24282437
* @throws StorageException upon failure
24292438
*/
24302439
Acl getAcl(BlobId blob, Entity entity);

0 commit comments

Comments
 (0)