Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(samples): add storage_grpc_quickstart samples #2041

Merged
merged 6 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.16.0')
implementation platform('com.google.cloud:libraries-bom:26.17.0')

implementation 'com.google.cloud:google-cloud-storage'
```
Expand Down Expand Up @@ -224,6 +224,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) |
| Generate Signed Post Policy V4 | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/GenerateSignedPostPolicyV4.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/GenerateSignedPostPolicyV4.java) |
| Get Service Account | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/GetServiceAccount.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/GetServiceAccount.java) |
| Quickstart Grpc Dp Sample | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/QuickstartGrpcDpSample.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/QuickstartGrpcDpSample.java) |
| Quickstart Grpc Sample | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/QuickstartGrpcSample.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/QuickstartGrpcSample.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) |
| 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) |
| Add Bucket Iam Conditional Binding | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/AddBucketIamConditionalBinding.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/AddBucketIamConditionalBinding.java) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2023 Google Inc.
*
* 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;

// [START storage_grpc_quickstart_dp]
// Imports the Google Cloud client library
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class QuickstartGrpcDpSample {
public static void main(String... args) throws Exception {

// Create an instance of options which will use the Google Cloud Storage gRPC API for all
// operations
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
StorageOptions options = StorageOptions.grpc().setAttemptDirectPath(true).build();

// Instantiates a client in a try-with-resource to automatically cleanup underlying resources
try (Storage storage = options.getService()) {
// The name for the new bucket
String bucketName = args[0]; // "my-new-bucket";

// Creates the new bucket using a request to the gRPC API
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
Bucket bucket = storage.create(BucketInfo.of(bucketName));

System.out.printf("Bucket %s created.%n", bucket.getName());
}
}
}
// [END storage_grpc_quickstart_dp]
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2023 Google Inc.
*
* 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;

// [START storage_grpc_quickstart]
// Imports the Google Cloud client library
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class QuickstartGrpcSample {
public static void main(String... args) throws Exception {

// Create an instance of options which will use the Google Cloud Storage gRPC API for all
// operations
StorageOptions options = StorageOptions.grpc().build();
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved

// Instantiates a client in a try-with-resource to automatically cleanup underlying resources
try (Storage storage = options.getService()) {
// The name for the new bucket
String bucketName = args[0]; // "my-new-bucket";

// Creates the new bucket using a request to the gRPC API
Bucket bucket = storage.create(BucketInfo.of(bucketName));

System.out.printf("Bucket %s created.%n", bucket.getName());
}
}
}
// [END storage_grpc_quickstart]
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public class QuickstartSampleIT {

private String bucketName;

private static final void deleteBucket(String bucketName) {
private static void deleteBucket(String bucketName) {
Storage storage = StorageOptions.getDefaultInstance().getService();
storage.delete(bucketName);
}

@Before
public void setUp() {
bucketName = "my-new-bucket-" + UUID.randomUUID().toString();
bucketName = "my-new-bucket-" + UUID.randomUUID();
}

@After
Expand All @@ -59,4 +59,18 @@ public void testQuickstart() throws Exception {
String got = stdOutCaptureRule.getCapturedOutputAsUtf8String();
assertThat(got).contains(String.format("Bucket %s created.", bucketName));
}

@Test
public void testQuickstartGrpc() throws Exception {
QuickstartGrpcSample.main(bucketName);
String got = stdOutCaptureRule.getCapturedOutputAsUtf8String();
assertThat(got).contains(String.format("Bucket %s created.", bucketName));
}

@Test
public void testQuickstartGrpcDp() throws Exception {
QuickstartGrpcDpSample.main(bucketName);
String got = stdOutCaptureRule.getCapturedOutputAsUtf8String();
assertThat(got).contains(String.format("Bucket %s created.", bucketName));
}
}