Skip to content
4 changes: 3 additions & 1 deletion .kokoro/nightly/samples.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ env_vars: {
value: "true"
}

# This service account we want to be any valid account not used for
# GOOGLE_APPLICATION_CREDENTIALS in the tests
env_vars: {
key: "IT_SERVICE_ACCOUNT_EMAIL"
value: "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com"
value: "samples@java-docs-samples-testing.iam.gserviceaccount.com"
}
4 changes: 3 additions & 1 deletion .kokoro/presubmit/samples.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ env_vars: {
value: "java-docs-samples-service-account"
}

# This service account we want to be any valid account not used for
# GOOGLE_APPLICATION_CREDENTIALS in the tests
env_vars: {
key: "IT_SERVICE_ACCOUNT_EMAIL"
value: "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com"
value: "samples@java-docs-samples-testing.iam.gserviceaccount.com"
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ implementation 'com.google.cloud:google-cloud-storage'
If you are using Gradle without BOM, add this to your dependencies

```Groovy
implementation 'com.google.cloud:google-cloud-storage:2.4.2'
implementation 'com.google.cloud:google-cloud-storage:2.4.3'
```

If you are using SBT, add this to your dependencies

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "2.4.2"
libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "2.4.3"
```

## Authentication
Expand Down Expand Up @@ -235,6 +235,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |



Expand Down
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]
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]
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();
}
}
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");
}
}