From a60caced9584855f12cdb7cac8ad7606ba32a60a Mon Sep 17 00:00:00 2001 From: Sydney Munro <97561403+sydney-munro@users.noreply.github.com> Date: Wed, 20 Jul 2022 09:45:12 -0700 Subject: [PATCH] docs: Refactor Custom Dual Region sample to work with API changes (#1516) * docs: Refactor Custom Dual Region sample to work with API changes --- .../bucket/CreateBucketDualRegion.java | 40 +++++++++++++++---- .../bucket/CreateBucketDualRegionTest.java | 3 +- 2 files changed, 34 insertions(+), 9 deletions(-) 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 index e5a3fccb0..292aeaa11 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketDualRegion.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketDualRegion.java @@ -20,35 +20,59 @@ import com.google.cloud.storage.Bucket; import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.BucketInfo.CustomPlacementConfig; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; +import java.util.Arrays; public class CreateBucketDualRegion { public static void createBucketDualRegion( - String projectId, String bucketName, String firstRegion, String secondRegion) { - // The ID of your GCP project + String projectId, + String bucketName, + String location, + String firstRegion, + String secondRegion) { + // The ID of your GCP project. // String projectId = "your-project-id"; - // The ID to give your GCS bucket + // The ID to give your GCS bucket. // String bucketName = "your-unique-bucket-name"; + // The location your dual regions will be located in. + // String location = "US"; + // 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; + // See this documentation for other valid locations and regions: + // https://cloud.google.com/storage/docs/locations Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); - Bucket bucket = - storage.create(BucketInfo.newBuilder(bucketName).setLocation(dualRegion).build()); + CustomPlacementConfig config = + CustomPlacementConfig.newBuilder() + .setDataLocations(Arrays.asList(firstRegion, secondRegion)) + .build(); + + BucketInfo bucketInfo = + BucketInfo.newBuilder(bucketName) + .setLocation(location) + .setCustomPlacementConfig(config) + .build(); + + Bucket bucket = storage.create(bucketInfo); System.out.println( - "Created bucket " + bucket.getName() + " in location " + bucket.getLocation()); + "Created bucket " + + bucket.getName() + + " in location " + + bucket.getLocation() + + " with regions " + + bucket.getCustomPlacementConfig().getDataLocations().toString()); } } // [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 index 8d5222f17..d4910c67c 100644 --- a/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketDualRegionTest.java +++ b/samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketDualRegionTest.java @@ -31,7 +31,8 @@ public class CreateBucketDualRegionTest extends TestBase { public void testCreateBucketDualRegion() { assertNotNull("Unable to determine Project ID", PROJECT_ID); String newBucket = RemoteStorageHelper.generateBucketName(); - CreateBucketDualRegion.createBucketDualRegion(PROJECT_ID, newBucket, "US-EAST1", "US-WEST1"); + CreateBucketDualRegion.createBucketDualRegion( + PROJECT_ID, newBucket, "US", "US-EAST1", "US-WEST1"); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("US-WEST1"); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("US-EAST1"); assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Created bucket");