Skip to content

Commit

Permalink
docs(samples): add storage_grpc_quickstart samples (#2041)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWhitehead authored Jun 22, 2023
1 parent bea275d commit 5f916fb
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Expand Up @@ -371,9 +371,6 @@ public Builder setTerminationAwaitDuration(Duration terminationAwaitDuration) {
/**
* Option which signifies the client should attempt to connect to gcs via Direct Path.
*
* <p>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.
*
* <p><i>NOTE</i>There 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.
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
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]
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();

// 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));
}
}

0 comments on commit 5f916fb

Please sign in to comment.