diff --git a/README.md b/README.md index 5f0b84919..f6208c767 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/ | Change Default Storage Class | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/ChangeDefaultStorageClass.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/ChangeDefaultStorageClass.java) | | Configure Bucket Cors | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/ConfigureBucketCors.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/ConfigureBucketCors.java) | | Create Bucket | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucket.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/CreateBucket.java) | +| Create Bucket Dual Region | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketDualRegion.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/CreateBucketDualRegion.java) | | Create Bucket Pub Sub Notification | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.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/CreateBucketPubSubNotification.java) | | Create Bucket With Storage Class And Location | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketWithStorageClassAndLocation.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/CreateBucketWithStorageClassAndLocation.java) | | Create Bucket With Turbo Replication | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketWithTurboReplication.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/CreateBucketWithTurboReplication.java) | diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketDualRegion.java b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketDualRegion.java new file mode 100644 index 000000000..e5a3fccb0 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketDualRegion.java @@ -0,0 +1,54 @@ +/* + * 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_create_bucket_dual_region] + +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 CreateBucketDualRegion { + + public static void createBucketDualRegion( + String projectId, String bucketName, String firstRegion, String secondRegion) { + // The ID of your GCP project + // String projectId = "your-project-id"; + + // The ID to give your GCS bucket + // String bucketName = "your-unique-bucket-name"; + + // One of the regions the dual region bucket is to be created in. + // String firstRegion = "US-EAST1"; + + // The second region the dual region bucket is to be created in. + // String secondRegion = "US-WEST1"; + + // Construct the dual region ie. "US-EAST1+US-WEST1" + String dualRegion = firstRegion + "+" + secondRegion; + + Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); + + Bucket bucket = + storage.create(BucketInfo.newBuilder(bucketName).setLocation(dualRegion).build()); + + System.out.println( + "Created bucket " + bucket.getName() + " in location " + bucket.getLocation()); + } +} +// [END storage_create_bucket_dual_region] diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketDualRegionTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketDualRegionTest.java new file mode 100644 index 000000000..8d5222f17 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketDualRegionTest.java @@ -0,0 +1,39 @@ +/* + * 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.testing.RemoteStorageHelper; +import org.junit.Test; + +public class CreateBucketDualRegionTest extends TestBase { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + + @Test + public void testCreateBucketDualRegion() { + assertNotNull("Unable to determine Project ID", PROJECT_ID); + String newBucket = RemoteStorageHelper.generateBucketName(); + CreateBucketDualRegion.createBucketDualRegion(PROJECT_ID, newBucket, "US-EAST1", "US-WEST1"); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("US-WEST1"); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("US-EAST1"); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Created bucket"); + } +}