Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Refactor Custom Dual Region sample to work with API changes #1516

Merged
merged 3 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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