Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Adding Samples for Creating Dual Region Buckets (#1341)
* docs: Adding Samples for Dual Region Buckets * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
83d5ee5
commit 9396061
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketDualRegion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
39 changes: 39 additions & 0 deletions
39
samples/snippets/src/test/java/com/example/storage/bucket/CreateBucketDualRegionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |