Skip to content

Commit a60cace

Browse files
authored
docs: Refactor Custom Dual Region sample to work with API changes (#1516)
* docs: Refactor Custom Dual Region sample to work with API changes
1 parent f424103 commit a60cace

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketDualRegion.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,59 @@
2020

2121
import com.google.cloud.storage.Bucket;
2222
import com.google.cloud.storage.BucketInfo;
23+
import com.google.cloud.storage.BucketInfo.CustomPlacementConfig;
2324
import com.google.cloud.storage.Storage;
2425
import com.google.cloud.storage.StorageOptions;
26+
import java.util.Arrays;
2527

2628
public class CreateBucketDualRegion {
2729

2830
public static void createBucketDualRegion(
29-
String projectId, String bucketName, String firstRegion, String secondRegion) {
30-
// The ID of your GCP project
31+
String projectId,
32+
String bucketName,
33+
String location,
34+
String firstRegion,
35+
String secondRegion) {
36+
// The ID of your GCP project.
3137
// String projectId = "your-project-id";
3238

33-
// The ID to give your GCS bucket
39+
// The ID to give your GCS bucket.
3440
// String bucketName = "your-unique-bucket-name";
3541

42+
// The location your dual regions will be located in.
43+
// String location = "US";
44+
3645
// One of the regions the dual region bucket is to be created in.
3746
// String firstRegion = "US-EAST1";
3847

3948
// The second region the dual region bucket is to be created in.
4049
// String secondRegion = "US-WEST1";
4150

42-
// Construct the dual region ie. "US-EAST1+US-WEST1"
43-
String dualRegion = firstRegion + "+" + secondRegion;
51+
// See this documentation for other valid locations and regions:
52+
// https://cloud.google.com/storage/docs/locations
4453

4554
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
4655

47-
Bucket bucket =
48-
storage.create(BucketInfo.newBuilder(bucketName).setLocation(dualRegion).build());
56+
CustomPlacementConfig config =
57+
CustomPlacementConfig.newBuilder()
58+
.setDataLocations(Arrays.asList(firstRegion, secondRegion))
59+
.build();
60+
61+
BucketInfo bucketInfo =
62+
BucketInfo.newBuilder(bucketName)
63+
.setLocation(location)
64+
.setCustomPlacementConfig(config)
65+
.build();
66+
67+
Bucket bucket = storage.create(bucketInfo);
4968

5069
System.out.println(
51-
"Created bucket " + bucket.getName() + " in location " + bucket.getLocation());
70+
"Created bucket "
71+
+ bucket.getName()
72+
+ " in location "
73+
+ bucket.getLocation()
74+
+ " with regions "
75+
+ bucket.getCustomPlacementConfig().getDataLocations().toString());
5276
}
5377
}
5478
// [END storage_create_bucket_dual_region]

samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketDualRegionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public class CreateBucketDualRegionTest extends TestBase {
3131
public void testCreateBucketDualRegion() {
3232
assertNotNull("Unable to determine Project ID", PROJECT_ID);
3333
String newBucket = RemoteStorageHelper.generateBucketName();
34-
CreateBucketDualRegion.createBucketDualRegion(PROJECT_ID, newBucket, "US-EAST1", "US-WEST1");
34+
CreateBucketDualRegion.createBucketDualRegion(
35+
PROJECT_ID, newBucket, "US", "US-EAST1", "US-WEST1");
3536
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("US-WEST1");
3637
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("US-EAST1");
3738
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Created bucket");

0 commit comments

Comments
 (0)