Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .kokoro/presubmit/samples.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ env_vars: {
env_vars: {
key: "SECRET_MANAGER_KEYS"
value: "java-docs-samples-service-account"
}
}

env_vars: {
key: "IT_SERVICE_ACCOUNT_EMAIL"
value: "java-docs-samples-testing@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.1'
implementation 'com.google.cloud:google-cloud-storage:2.4.2'
```

If you are using SBT, add this to your dependencies

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

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



Expand Down
1 change: 1 addition & 0 deletions owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
'.kokoro/nightly/integration.cfg',
'.kokoro/nightly/java11-integration.cfg',
'.kokoro/presubmit/integration.cfg',
'.kokoro/presubmit/samples.cfg',
'CONTRIBUTING.md'
])
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.bucket;

// [START storage_print_bucket_acl]

import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.util.List;

public class PrintBucketAcl {

public static void printBucketAcl(String bucketName) {

// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";

Storage storage = StorageOptions.newBuilder().build().getService();
Bucket bucket = storage.get(bucketName);
List<Acl> bucketAcls = bucket.getAcl();

for (Acl acl : bucketAcls) {

// This will give you the role.
// See https://cloud.google.com/storage/docs/access-control/lists#permissions
String role = acl.getRole().name();

// This will give you the Entity type (i.e. User, Group, Project etc.)
// See https://cloud.google.com/storage/docs/access-control/lists#scopes
String entityType = acl.getEntity().getType().name();

System.out.printf("%s: %s \n", role, entityType);
}
}
}
// [END storage_print_bucket_acl]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.bucket;

// [START storage_print_bucket_acl_for_user]

import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Acl.User;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class PrintBucketAclFilterByUser {

public static void printBucketAclFilterByUser(String bucketName, String userEmail) {

// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The email of the user whose acl is being retrieved.
// String userEmail = "[email protected]"

Storage storage = StorageOptions.newBuilder().build().getService();
Bucket bucket = storage.get(bucketName);

Acl userAcl = bucket.getAcl(new User(userEmail));
String userRole = userAcl.getRole().name();
System.out.println("User " + userEmail + " has role " + userRole);
}
}

// [END storage_print_bucket_acl_for_user]
53 changes: 53 additions & 0 deletions samples/snippets/src/test/java/com/example/storage/TestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.cloud.testing.junit4.StdOutCaptureRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;

public abstract class TestBase {

@Rule public StdOutCaptureRule stdOut = new StdOutCaptureRule();

protected String bucketName;
protected Storage storage;
protected String blobName;

protected Blob blob;

@Before
public void setUp() {
blobName = "blob";
bucketName = RemoteStorageHelper.generateBucketName();
storage = StorageOptions.getDefaultInstance().getService();
storage.create(BucketInfo.of(bucketName));
blob = storage.create(BlobInfo.newBuilder(bucketName, blobName).build());
}

@After
public void tearDown() {
RemoteStorageHelper.forceDelete(storage, bucketName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.bucket;

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.Entity;
import com.google.cloud.storage.Acl.Role;
import com.google.cloud.storage.Acl.User;
import org.junit.Test;

public class PrintBucketAclFilterByUserTest extends TestBase {

public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");

@Test
public void testPrintBucketAclByUser() {
// Check for user email before the actual test.
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);

Entity testUser = new User(IT_SERVICE_ACCOUNT_EMAIL);
storage.createAcl(bucketName, Acl.of(testUser, Role.READER));
PrintBucketAclFilterByUser.printBucketAclFilterByUser(bucketName, IT_SERVICE_ACCOUNT_EMAIL);
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(Role.READER.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.bucket;

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.Entity;
import com.google.cloud.storage.Acl.Role;
import com.google.cloud.storage.Acl.User;
import org.junit.Test;

public class PrintBucketAclTest extends TestBase {

public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");

@Test
public void testPrintBucketAcls() {
// Check for user email before the actual test.
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);

Entity testUser = new User(IT_SERVICE_ACCOUNT_EMAIL);
storage.createAcl(bucketName, Acl.of(testUser, Role.READER));
PrintBucketAcl.printBucketAcl(bucketName);
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("READER: USER");
}
}