Skip to content

Commit a10e640

Browse files
feat: samples for dual region bucket creation (GoogleCloudPlatform#1659)
1 parent fec4680 commit a10e640

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

storage/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-storage": "^1.20.1",
3+
"google/cloud-storage": "^1.28.0",
44
"paragonie/random_compat": "^9.0.0"
55
},
66
"require-dev": {

storage/src/create_bucket_dual_region.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,25 @@
3030
* Create a new bucket with a custom default storage class and location.
3131
*
3232
* @param string $bucketName The name of your Cloud Storage bucket.
33-
* @param string $location1 First location for the bucket's regions. Case-insensitive.
34-
* @param string $location2 Second location for the bucket's regions. Case-insensitive.
33+
* @param string $location Location for the bucket's regions. Case-insensitive.
34+
* @param string $region1 First region for the bucket's regions. Case-insensitive.
35+
* @param string $region2 Second region for the bucket's regions. Case-insensitive.
3536
*/
36-
function create_bucket_dual_region($bucketName, $location1, $location2)
37+
function create_bucket_dual_region($bucketName, $location, $region1, $region2)
3738
{
3839
// $bucketName = 'my-bucket';
39-
// $location1 = 'US-EAST1';
40-
// $location2 = 'US-WEST1';
40+
// $location = 'US';
41+
// $region1 = 'US-EAST1';
42+
// $region2 = 'US-WEST1';
4143

4244
$storage = new StorageClient();
4345
$bucket = $storage->createBucket($bucketName, [
44-
'location' => "${location1}+${location2}",
46+
'location' => $location,
47+
'customPlacementConfig' => [
48+
'dataLocations' => [$region1, $region2],
49+
],
4550
]);
46-
47-
printf("Created dual-region bucket '%s' in '%s+%s'", $bucket->name(), $location1, $location2);
51+
printf("Bucket '%s' created in '%s' and '%s'", $bucket->name(), $region1, $region2);
4852
}
4953
# [END storage_create_bucket_dual_region]
5054

storage/test/storageTest.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -674,23 +674,38 @@ public function testCreateBucketClassLocation()
674674

675675
public function testCreateBucketDualRegion()
676676
{
677-
$location1 = 'US-EAST1';
678-
$location2 = 'US-WEST1';
677+
$location = 'US';
678+
$region1 = 'US-EAST1';
679+
$region2 = 'US-WEST1';
679680

680681
$bucketName = uniqid('samples-create-bucket-dual-region-');
681682
$output = self::runFunctionSnippet('create_bucket_dual_region', [
682683
$bucketName,
683-
$location1,
684-
$location2
684+
$location,
685+
$region1,
686+
$region2
685687
]);
686688

687689
$bucket = self::$storage->bucket($bucketName);
690+
$info = $bucket->reload();
688691
$exists = $bucket->exists();
689692
$bucket->delete();
690693

691694
$this->assertTrue($exists);
692-
$this->assertStringContainsString('Created dual-region bucket', $output);
693-
$this->assertStringContainsString("${location1}+${location2}", $output);
695+
$this->assertEquals(
696+
sprintf(
697+
"Bucket '%s' created in '%s' and '%s'",
698+
$bucketName,
699+
$region1,
700+
$region2
701+
),
702+
$output
703+
);
704+
$this->assertEquals($location, $info['location']);
705+
$this->assertArrayHasKey('customPlacementConfig', $info);
706+
$this->assertArrayHasKey('dataLocations', $info['customPlacementConfig']);
707+
$this->assertContains($region1, $info['customPlacementConfig']['dataLocations']);
708+
$this->assertContains($region2, $info['customPlacementConfig']['dataLocations']);
694709
}
695710

696711
public function testObjectCsekToCmek()

0 commit comments

Comments
 (0)