Skip to content

Commit 32fe388

Browse files
docs: Adding Samples for printing all Acls for a file and for a specific user (#1288)
* docs: Adding Samples for printing all Acls for a file and for a specific user * Addressing PR comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 9b5d041 commit 32fe388

File tree

5 files changed

+211
-2
lines changed

5 files changed

+211
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ implementation 'com.google.cloud:google-cloud-storage'
5656
If you are using Gradle without BOM, add this to your dependencies
5757

5858
```Groovy
59-
implementation 'com.google.cloud:google-cloud-storage:2.4.4'
59+
implementation 'com.google.cloud:google-cloud-storage:2.4.5'
6060
```
6161

6262
If you are using SBT, add this to your dependencies
6363

6464
```Scala
65-
libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "2.4.4"
65+
libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "2.4.5"
6666
```
6767

6868
## Authentication
@@ -236,6 +236,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/
236236
| Remove Bucket Default Owner | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultOwner.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultOwner.java) |
237237
| Remove Bucket Owner | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketOwner.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketOwner.java) |
238238
| Add File Owner | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/object/AddFileOwner.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/object/AddFileOwner.java) |
239+
| Print File Acl | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/object/PrintFileAcl.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/object/PrintFileAcl.java) |
240+
| Print File Acl For User | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/object/PrintFileAclForUser.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/object/PrintFileAclForUser.java) |
239241
| Remove File Owner | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/object/RemoveFileOwner.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/object/RemoveFileOwner.java) |
240242

241243

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.object;
18+
19+
// [START storage_print_file_acl]
20+
21+
import com.google.cloud.storage.Acl;
22+
import com.google.cloud.storage.Blob;
23+
import com.google.cloud.storage.BlobId;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.StorageOptions;
26+
import java.util.List;
27+
28+
public class PrintFileAcl {
29+
30+
public static void printFileAcl(String bucketName, String blobName) {
31+
32+
// The ID to give your GCS bucket
33+
// String bucketName = "your-unique-bucket-name";
34+
35+
// The name of the blob/file that you wish to view Acls of
36+
// String blobName = "your-blob-name";
37+
38+
Storage storage = StorageOptions.newBuilder().build().getService();
39+
Blob blob = storage.get(BlobId.of(bucketName, blobName));
40+
List<Acl> blobAcls = blob.getAcl();
41+
42+
for (Acl acl : blobAcls) {
43+
44+
// This will give you the role.
45+
// See https://cloud.google.com/storage/docs/access-control/lists#permissions
46+
String role = acl.getRole().name();
47+
48+
// This will give you the Entity type (i.e. User, Group, Project etc.)
49+
// See https://cloud.google.com/storage/docs/access-control/lists#scopes
50+
String entityType = acl.getEntity().getType().name();
51+
52+
System.out.printf("%s: %s %n", role, entityType);
53+
}
54+
}
55+
}
56+
// [END storage_print_file_acl]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.object;
18+
19+
// [START storage_print_file_acl_for_user]
20+
21+
import com.google.cloud.storage.Acl;
22+
import com.google.cloud.storage.Acl.User;
23+
import com.google.cloud.storage.Blob;
24+
import com.google.cloud.storage.BlobId;
25+
import com.google.cloud.storage.Storage;
26+
import com.google.cloud.storage.StorageOptions;
27+
28+
public class PrintFileAclForUser {
29+
30+
public static void printFileAclForUser(String bucketName, String blobName, String userEmail) {
31+
32+
// The ID to give your GCS bucket
33+
// String bucketName = "your-unique-bucket-name";
34+
35+
// The name of the blob/file that you wish to view Acls of
36+
// String blobName = "your-blob-name";
37+
38+
// The email of the user whose acl is being retrieved.
39+
// String userEmail = "[email protected]"
40+
41+
Storage storage = StorageOptions.newBuilder().build().getService();
42+
Blob blob = storage.get(BlobId.of(bucketName, blobName));
43+
Acl blobAcl = blob.getAcl(new User(userEmail));
44+
if (blobAcl != null) {
45+
String userRole = blobAcl.getRole().name();
46+
System.out.println("User " + userEmail + " has role " + userRole);
47+
} else {
48+
System.out.println("User " + userEmail + " not found");
49+
}
50+
}
51+
}
52+
// [END storage_print_file_acl_for_user]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.object;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import com.example.storage.TestBase;
23+
import com.google.cloud.storage.Acl;
24+
import com.google.cloud.storage.Acl.Entity;
25+
import com.google.cloud.storage.Acl.Role;
26+
import com.google.cloud.storage.Acl.User;
27+
import org.junit.Test;
28+
29+
public class PrintFileAclForUserTest extends TestBase {
30+
31+
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");
32+
33+
@Test
34+
public void testPrintBucketAclByUser() {
35+
// Check for user email before the actual test.
36+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
37+
38+
Entity testUser = new User(IT_SERVICE_ACCOUNT_EMAIL);
39+
blob.createAcl(Acl.of(testUser, Role.READER));
40+
PrintFileAclForUser.printFileAclForUser(bucketName, blobName, IT_SERVICE_ACCOUNT_EMAIL);
41+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
42+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(Role.READER.name());
43+
}
44+
45+
@Test
46+
public void testUserNotFound() {
47+
// Check for user email before the actual test.
48+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
49+
50+
// Delete Acl just in case to make sure the User ACL is not present
51+
blob.deleteAcl(new User(IT_SERVICE_ACCOUNT_EMAIL));
52+
PrintFileAclForUser.printFileAclForUser(bucketName, blobName, IT_SERVICE_ACCOUNT_EMAIL);
53+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
54+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("not found");
55+
}
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.object;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import com.example.storage.TestBase;
23+
import com.google.cloud.storage.Acl;
24+
import com.google.cloud.storage.Acl.Entity;
25+
import com.google.cloud.storage.Acl.Role;
26+
import com.google.cloud.storage.Acl.User;
27+
import org.junit.Test;
28+
29+
public class PrintFileAclTest extends TestBase {
30+
31+
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");
32+
33+
@Test
34+
public void testPrintFileAcls() {
35+
// Check for user email before the actual test.
36+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
37+
38+
Entity testUser = new User(IT_SERVICE_ACCOUNT_EMAIL);
39+
blob.createAcl(Acl.of(testUser, Role.READER));
40+
PrintFileAcl.printFileAcl(bucketName, blobName);
41+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("READER: USER");
42+
}
43+
}

0 commit comments

Comments
 (0)