Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
a9eb831
commit 6fad19c
Showing
7 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
samples/snippets/src/main/java/com/example/storage/object/AddFileOwner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.storage.object; | ||
|
||
// [START storage_add_file_owner] | ||
|
||
import com.google.cloud.storage.Acl; | ||
import com.google.cloud.storage.Acl.Role; | ||
import com.google.cloud.storage.Acl.User; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
|
||
public class AddFileOwner { | ||
|
||
public static void addFileOwner(String bucketName, String userEmail, String blobName) { | ||
|
||
// The ID to give your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
// Email of the user you wish to add as a file owner | ||
// String userEmail = "someuser@domain.com" | ||
|
||
// The name of the blob/file that you wish to modify permissions on | ||
// String blobName = "your-blob-name"; | ||
|
||
Storage storage = StorageOptions.newBuilder().build().getService(); | ||
Blob blob = storage.get(BlobId.of(bucketName, blobName)); | ||
Acl newOwner = Acl.of(new User(userEmail), Role.OWNER); | ||
|
||
blob.createAcl(newOwner); | ||
System.out.println( | ||
"Added user " | ||
+ userEmail | ||
+ " as an owner on file " | ||
+ blobName | ||
+ " in bucket " | ||
+ bucketName); | ||
} | ||
} | ||
// [END storage_add_file_owner] |
58 changes: 58 additions & 0 deletions
58
samples/snippets/src/main/java/com/example/storage/object/RemoveFileOwner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.storage.object; | ||
|
||
// [START storage_remove_file_owner] | ||
|
||
import com.google.cloud.storage.Acl.User; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
|
||
public class RemoveFileOwner { | ||
|
||
public static void removeFileOwner(String bucketName, String userEmail, String blobName) { | ||
|
||
// The ID to give your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
// Email of the user you wish to remove as a file owner | ||
// String userEmail = "someuser@domain.com" | ||
|
||
// The name of the blob/file that you wish to modify permissions on | ||
// String blobName = "your-blob-name"; | ||
|
||
Storage storage = StorageOptions.newBuilder().build().getService(); | ||
Blob blob = storage.get(BlobId.of(bucketName, blobName)); | ||
User ownerToRemove = new User(userEmail); | ||
|
||
boolean success = blob.deleteAcl(ownerToRemove); | ||
if (success) { | ||
System.out.println( | ||
"Removed user " | ||
+ userEmail | ||
+ " as an owner on file " | ||
+ blobName | ||
+ " in bucket " | ||
+ bucketName); | ||
} else { | ||
System.out.println("User " + userEmail + " was not found"); | ||
} | ||
} | ||
} | ||
// [END storage_remove_file_owner] |
40 changes: 40 additions & 0 deletions
40
samples/snippets/src/test/java/com/example/storage/object/AddFileOwnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.storage.object; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.example.storage.TestBase; | ||
import com.google.cloud.storage.Acl.User; | ||
import org.junit.Test; | ||
|
||
public class AddFileOwnerTest extends TestBase { | ||
|
||
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); | ||
|
||
@Test | ||
public void testAddFileOwner() { | ||
// Check for user email before the actual test. | ||
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); | ||
|
||
// Add Ownership to the file. | ||
AddFileOwner.addFileOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL, blobName); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); | ||
assertThat(blob.getAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNotNull(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
samples/snippets/src/test/java/com/example/storage/object/RemoveFileOwnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.storage.object; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.example.storage.TestBase; | ||
import com.google.cloud.storage.Acl; | ||
import com.google.cloud.storage.Acl.Role; | ||
import com.google.cloud.storage.Acl.User; | ||
import org.junit.Test; | ||
|
||
public class RemoveFileOwnerTest extends TestBase { | ||
|
||
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); | ||
|
||
@Test | ||
public void testRemoveFileOwner() { | ||
// Check for user email before the actual test. | ||
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); | ||
|
||
// Add User as Owner | ||
Acl newFileOwner = Acl.of(new User(IT_SERVICE_ACCOUNT_EMAIL), Role.OWNER); | ||
blob.createAcl(newFileOwner); | ||
|
||
// Remove User as owner | ||
RemoveFileOwner.removeFileOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL, blobName); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Removed user"); | ||
assertThat(blob.getAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNull(); | ||
} | ||
|
||
@Test | ||
public void testUserNotFound() { | ||
// Check for user email before the actual test. | ||
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); | ||
|
||
// Remove User without Owner Permissions | ||
RemoveFileOwner.removeFileOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL, blobName); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("was not found"); | ||
} | ||
} |