From 5f916fbba789edd33c2b4195db885d2b0191f860 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Jun 2023 18:17:25 -0400 Subject: [PATCH] docs(samples): add storage_grpc_quickstart samples (#2041) --- README.md | 2 + .../cloud/storage/GrpcStorageOptions.java | 3 -- .../storage/QuickstartGrpcDpSample.java | 45 +++++++++++++++++++ .../example/storage/QuickstartGrpcSample.java | 45 +++++++++++++++++++ .../example/storage/QuickstartSampleIT.java | 18 +++++++- 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 samples/snippets/src/main/java/com/example/storage/QuickstartGrpcDpSample.java create mode 100644 samples/snippets/src/main/java/com/example/storage/QuickstartGrpcSample.java diff --git a/README.md b/README.md index 74f8f5f063..4079c42bd3 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java index 1b69e67386..d623745a20 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java @@ -371,9 +371,6 @@ public Builder setTerminationAwaitDuration(Duration terminationAwaitDuration) { /** * Option which signifies the client should attempt to connect to gcs via Direct Path. * - *

In order to use direct path, both this option must be true and the environment variable - * (not system property) {@code GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS} must be true. - * *

NOTEThere is no need to specify a new endpoint via {@link #setHost(String)} as the * underlying code will translate the normal {@code https://storage.googleapis.com:443} into the * proper Direct Path URI for you. diff --git a/samples/snippets/src/main/java/com/example/storage/QuickstartGrpcDpSample.java b/samples/snippets/src/main/java/com/example/storage/QuickstartGrpcDpSample.java new file mode 100644 index 0000000000..a657300c0f --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/QuickstartGrpcDpSample.java @@ -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 + 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 via DirectPath + Bucket bucket = storage.create(BucketInfo.of(bucketName)); + + System.out.printf("Bucket %s created.%n", bucket.getName()); + } + } +} +// [END storage_grpc_quickstart_dp] diff --git a/samples/snippets/src/main/java/com/example/storage/QuickstartGrpcSample.java b/samples/snippets/src/main/java/com/example/storage/QuickstartGrpcSample.java new file mode 100644 index 0000000000..84c737ba97 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/QuickstartGrpcSample.java @@ -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(); + + // 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] diff --git a/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java b/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java index 4c6a1e42a6..0fb5c0581c 100644 --- a/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java +++ b/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java @@ -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 @@ -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)); + } }