|
20 | 20 |
|
21 | 21 | import com.google.cloud.storage.Bucket; |
22 | 22 | import com.google.cloud.storage.BucketInfo; |
| 23 | +import com.google.cloud.storage.BucketInfo.CustomPlacementConfig; |
23 | 24 | import com.google.cloud.storage.Storage; |
24 | 25 | import com.google.cloud.storage.StorageOptions; |
| 26 | +import java.util.Arrays; |
25 | 27 |
|
26 | 28 | public class CreateBucketDualRegion { |
27 | 29 |
|
28 | 30 | 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. |
31 | 37 | // String projectId = "your-project-id"; |
32 | 38 |
|
33 | | - // The ID to give your GCS bucket |
| 39 | + // The ID to give your GCS bucket. |
34 | 40 | // String bucketName = "your-unique-bucket-name"; |
35 | 41 |
|
| 42 | + // The location your dual regions will be located in. |
| 43 | + // String location = "US"; |
| 44 | + |
36 | 45 | // One of the regions the dual region bucket is to be created in. |
37 | 46 | // String firstRegion = "US-EAST1"; |
38 | 47 |
|
39 | 48 | // The second region the dual region bucket is to be created in. |
40 | 49 | // String secondRegion = "US-WEST1"; |
41 | 50 |
|
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 |
44 | 53 |
|
45 | 54 | Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); |
46 | 55 |
|
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); |
49 | 68 |
|
50 | 69 | 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()); |
52 | 76 | } |
53 | 77 | } |
54 | 78 | // [END storage_create_bucket_dual_region] |
0 commit comments