Skip to content

Commit 6fad19c

Browse files
docs: Adding Samples for Adding/Removing File Owners (#1273)
* docs: Adding Samples for Adding/Removing File Owners * 🦉 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 a9eb831 commit 6fad19c

File tree

7 files changed

+220
-2
lines changed

7 files changed

+220
-2
lines changed

.kokoro/nightly/samples.cfg

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ env_vars: {
3737
value: "true"
3838
}
3939

40+
# This service account we want to be any valid account not used for
41+
# GOOGLE_APPLICATION_CREDENTIALS in the tests
4042
env_vars: {
4143
key: "IT_SERVICE_ACCOUNT_EMAIL"
42-
value: "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com"
44+
value: "samples@java-docs-samples-testing.iam.gserviceaccount.com"
4345
}

.kokoro/presubmit/samples.cfg

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ env_vars: {
3232
value: "java-docs-samples-service-account"
3333
}
3434

35+
# This service account we want to be any valid account not used for
36+
# GOOGLE_APPLICATION_CREDENTIALS in the tests
3537
env_vars: {
3638
key: "IT_SERVICE_ACCOUNT_EMAIL"
37-
value: "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com"
39+
value: "samples@java-docs-samples-testing.iam.gserviceaccount.com"
3840
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/
235235
| 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) |
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) |
238+
| 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+
| 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) |
238240

239241

240242

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_add_file_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.Blob;
25+
import com.google.cloud.storage.BlobId;
26+
import com.google.cloud.storage.Storage;
27+
import com.google.cloud.storage.StorageOptions;
28+
29+
public class AddFileOwner {
30+
31+
public static void addFileOwner(String bucketName, String userEmail, String blobName) {
32+
33+
// The ID to give your GCS bucket
34+
// String bucketName = "your-unique-bucket-name";
35+
36+
// Email of the user you wish to add as a file owner
37+
// String userEmail = "someuser@domain.com"
38+
39+
// The name of the blob/file that you wish to modify permissions on
40+
// String blobName = "your-blob-name";
41+
42+
Storage storage = StorageOptions.newBuilder().build().getService();
43+
Blob blob = storage.get(BlobId.of(bucketName, blobName));
44+
Acl newOwner = Acl.of(new User(userEmail), Role.OWNER);
45+
46+
blob.createAcl(newOwner);
47+
System.out.println(
48+
"Added user "
49+
+ userEmail
50+
+ " as an owner on file "
51+
+ blobName
52+
+ " in bucket "
53+
+ bucketName);
54+
}
55+
}
56+
// [END storage_add_file_owner]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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_remove_file_owner]
20+
21+
import com.google.cloud.storage.Acl.User;
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+
27+
public class RemoveFileOwner {
28+
29+
public static void removeFileOwner(String bucketName, String userEmail, String blobName) {
30+
31+
// The ID to give your GCS bucket
32+
// String bucketName = "your-unique-bucket-name";
33+
34+
// Email of the user you wish to remove as a file owner
35+
// String userEmail = "someuser@domain.com"
36+
37+
// The name of the blob/file that you wish to modify permissions on
38+
// String blobName = "your-blob-name";
39+
40+
Storage storage = StorageOptions.newBuilder().build().getService();
41+
Blob blob = storage.get(BlobId.of(bucketName, blobName));
42+
User ownerToRemove = new User(userEmail);
43+
44+
boolean success = blob.deleteAcl(ownerToRemove);
45+
if (success) {
46+
System.out.println(
47+
"Removed user "
48+
+ userEmail
49+
+ " as an owner on file "
50+
+ blobName
51+
+ " in bucket "
52+
+ bucketName);
53+
} else {
54+
System.out.println("User " + userEmail + " was not found");
55+
}
56+
}
57+
}
58+
// [END storage_remove_file_owner]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.User;
24+
import org.junit.Test;
25+
26+
public class AddFileOwnerTest extends TestBase {
27+
28+
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");
29+
30+
@Test
31+
public void testAddFileOwner() {
32+
// Check for user email before the actual test.
33+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
34+
35+
// Add Ownership to the file.
36+
AddFileOwner.addFileOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL, blobName);
37+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
38+
assertThat(blob.getAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNotNull();
39+
}
40+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.Role;
25+
import com.google.cloud.storage.Acl.User;
26+
import org.junit.Test;
27+
28+
public class RemoveFileOwnerTest extends TestBase {
29+
30+
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");
31+
32+
@Test
33+
public void testRemoveFileOwner() {
34+
// Check for user email before the actual test.
35+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
36+
37+
// Add User as Owner
38+
Acl newFileOwner = Acl.of(new User(IT_SERVICE_ACCOUNT_EMAIL), Role.OWNER);
39+
blob.createAcl(newFileOwner);
40+
41+
// Remove User as owner
42+
RemoveFileOwner.removeFileOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL, blobName);
43+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
44+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Removed user");
45+
assertThat(blob.getAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNull();
46+
}
47+
48+
@Test
49+
public void testUserNotFound() {
50+
// Check for user email before the actual test.
51+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
52+
53+
// Remove User without Owner Permissions
54+
RemoveFileOwner.removeFileOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL, blobName);
55+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
56+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("was not found");
57+
}
58+
}

0 commit comments

Comments
 (0)