|
| 1 | +/* |
| 2 | + * Copyright 2023 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.storage; |
| 18 | + |
| 19 | +// [START storage_quickstart_grpc] |
| 20 | +// Imports the Google Cloud client library |
| 21 | +import com.google.cloud.storage.Bucket; |
| 22 | +import com.google.cloud.storage.BucketInfo; |
| 23 | +import com.google.cloud.storage.Storage; |
| 24 | +import com.google.cloud.storage.StorageOptions; |
| 25 | + |
| 26 | +public class QuickstartGrpcSample { |
| 27 | + public static void main(String... args) throws Exception { |
| 28 | + |
| 29 | + StorageOptions options = StorageOptions.grpc() |
| 30 | + // When running on GCE, set attemptDirectPath to true for increased performance |
| 31 | + .setAttemptDirectPath(false) |
| 32 | + .build(); |
| 33 | + |
| 34 | + // Instantiates a client in a try-with-resource to automatically cleanup underlying resources |
| 35 | + try (Storage storage = options.getService()) { |
| 36 | + // The name for the new bucket |
| 37 | + String bucketName = args[0]; // "my-new-bucket"; |
| 38 | + |
| 39 | + // Creates the new bucket |
| 40 | + Bucket bucket = storage.create(BucketInfo.of(bucketName)); |
| 41 | + |
| 42 | + System.out.printf("Bucket %s created.%n", bucket.getName()); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +// [END storage_quickstart_grpc] |
0 commit comments