Skip to content

Commit 7223626

Browse files
docs: Adding Samples for Add/Remove Bucket Default Owner (#1260)
* docs: Adding Samples for Add/Remove Bucket Default Owner * 🦉 Updates from OwlBot 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 d82333b commit 7223626

File tree

6 files changed

+192
-2
lines changed

6 files changed

+192
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/
229229
| --------------------------- | --------------------------------- | ------ |
230230
| Configure Retries | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.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/ConfigureRetries.java) |
231231
| Quickstart Sample | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/QuickstartSample.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/QuickstartSample.java) |
232+
| Add Bucket Default Owner | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/AddBucketDefaultOwner.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/AddBucketDefaultOwner.java) |
232233
| Print Bucket Acl | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.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/PrintBucketAcl.java) |
233234
| Print Bucket Acl Filter By User | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.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/PrintBucketAclFilterByUser.java) |
235+
| 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) |
234236

235237

236238

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.bucket;
18+
19+
// [START storage_add_bucket_default_owner]
20+
21+
import com.google.cloud.storage.Acl;
22+
import com.google.cloud.storage.Acl.Role;
23+
import com.google.cloud.storage.Acl.User;
24+
import com.google.cloud.storage.Bucket;
25+
import com.google.cloud.storage.Storage;
26+
import com.google.cloud.storage.StorageOptions;
27+
28+
public class AddBucketDefaultOwner {
29+
30+
public static void addBucketDefaultOwner(String bucketName, String userEmail) {
31+
32+
// The ID to give your GCS bucket
33+
// String bucketName = "your-unique-bucket-name";
34+
35+
// Email of the user you wish to remove as a default owner
36+
// String userEmail = "[email protected]"
37+
38+
Storage storage = StorageOptions.newBuilder().build().getService();
39+
Bucket bucket = storage.get(bucketName);
40+
Acl newDefaultOwner = Acl.of(new User(userEmail), Role.OWNER);
41+
42+
bucket.createDefaultAcl(newDefaultOwner);
43+
System.out.println("Added user " + userEmail + " as an owner on " + bucketName);
44+
}
45+
}
46+
// [END storage_add_bucket_default_owner]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.bucket;
18+
19+
// [START storage_remove_bucket_default_owner]
20+
21+
import com.google.cloud.storage.Acl.User;
22+
import com.google.cloud.storage.Bucket;
23+
import com.google.cloud.storage.Storage;
24+
import com.google.cloud.storage.StorageOptions;
25+
26+
public class RemoveBucketDefaultOwner {
27+
28+
public static void removeBucketDefaultOwner(String bucketName, String userEmail) {
29+
30+
// The ID to give your GCS bucket
31+
// String bucketName = "your-unique-bucket-name";
32+
33+
// Email of the user you wish to remove as a default owner
34+
// String userEmail = "[email protected]"
35+
36+
Storage storage = StorageOptions.newBuilder().build().getService();
37+
Bucket bucket = storage.get(bucketName);
38+
User userToRemove = new User(userEmail);
39+
40+
boolean success = bucket.deleteDefaultAcl(userToRemove);
41+
if (success) {
42+
System.out.println("Removed user " + userEmail + " as an owner on " + bucketName);
43+
} else {
44+
System.out.println("User " + userEmail + " was not found");
45+
}
46+
}
47+
}
48+
// [END storage_remove_bucket_default_owner]

samples/snippets/src/test/java/com/example/storage/TestBase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.cloud.storage.Blob;
2020
import com.google.cloud.storage.BlobInfo;
21+
import com.google.cloud.storage.Bucket;
2122
import com.google.cloud.storage.BucketInfo;
2223
import com.google.cloud.storage.Storage;
2324
import com.google.cloud.storage.StorageOptions;
@@ -34,15 +35,15 @@ public abstract class TestBase {
3435
protected String bucketName;
3536
protected Storage storage;
3637
protected String blobName;
37-
38+
protected Bucket bucket;
3839
protected Blob blob;
3940

4041
@Before
4142
public void setUp() {
4243
blobName = "blob";
4344
bucketName = RemoteStorageHelper.generateBucketName();
4445
storage = StorageOptions.getDefaultInstance().getService();
45-
storage.create(BucketInfo.of(bucketName));
46+
bucket = storage.create(BucketInfo.of(bucketName));
4647
blob = storage.create(BlobInfo.newBuilder(bucketName, blobName).build());
4748
}
4849

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.bucket;
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.User;
24+
import org.junit.Test;
25+
26+
public class AddBucketDefaultOwnerTest extends TestBase {
27+
28+
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");
29+
30+
@Test
31+
public void testAddBucketDefaultOwner() {
32+
// Check for user email before the actual test.
33+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
34+
35+
AddBucketDefaultOwner.addBucketDefaultOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL);
36+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
37+
assertThat(bucket.getDefaultAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNotNull();
38+
}
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.bucket;
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.Role;
25+
import com.google.cloud.storage.Acl.User;
26+
import org.junit.Test;
27+
28+
public class RemoveBucketDefaultOwnerTest extends TestBase {
29+
30+
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");
31+
32+
@Test
33+
public void testRemoveBucketDefaultOwner() {
34+
// Check for user email before the actual test.
35+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
36+
// Add User as Default Owner
37+
Acl newDefaultOwner = Acl.of(new User(IT_SERVICE_ACCOUNT_EMAIL), Role.OWNER);
38+
bucket.createDefaultAcl(newDefaultOwner);
39+
40+
// Remove User as Default owner
41+
RemoveBucketDefaultOwner.removeBucketDefaultOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL);
42+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
43+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Removed user");
44+
assertThat(bucket.getDefaultAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNull();
45+
}
46+
47+
@Test
48+
public void testUserNotFound() {
49+
// Remove User without Default Owner Permissions
50+
RemoveBucketDefaultOwner.removeBucketDefaultOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL);
51+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
52+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("was not found");
53+
}
54+
}

0 commit comments

Comments
 (0)