Skip to content

Commit

Permalink
docs: Refactor Custom Dual Region sample to work with API changes (#1516
Browse files Browse the repository at this point in the history
)

* docs: Refactor Custom Dual Region sample to work with API changes
  • Loading branch information
sydney-munro committed Jul 20, 2022
1 parent f424103 commit a60cace
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Expand Up @@ -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]
Expand Up @@ -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");
Expand Down

0 comments on commit a60cace

Please sign in to comment.