Skip to content

Commit fb4e5fc

Browse files
authored
feat(StorageControl): samples for anywhereCache (#2143)
1 parent 8a7d6cc commit fb4e5fc

9 files changed

+632
-1
lines changed

storagecontrol/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"google/cloud-storage-control": "1.3.0"
44
},
55
"require-dev": {
6-
"google/cloud-storage": "^1.41.3"
6+
"google/cloud-storage": "^1.48.1"
77
}
88
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageControl;
26+
27+
# [START storage_control_create_anywhere_cache]
28+
use Google\Cloud\Storage\Control\V2\AnywhereCache;
29+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
30+
use Google\Cloud\Storage\Control\V2\CreateAnywhereCacheRequest;
31+
32+
/**
33+
* Creates an Anywhere Cache instance.
34+
*
35+
* @param string $bucketName The name of your Cloud Storage bucket.
36+
* (e.g. 'my-bucket')
37+
* @param string $zone The zone in which the cache instance is running.
38+
* (e.g. 'us-east1-b')
39+
*/
40+
function create_anywhere_cache(string $bucketName, string $zone): void
41+
{
42+
$storageControlClient = new StorageControlClient();
43+
44+
// Set project to "_" to signify global bucket
45+
$formattedName = $storageControlClient->bucketName('_', $bucketName);
46+
47+
$anywhereCache = new AnywhereCache([
48+
'zone' => $zone,
49+
]);
50+
51+
$request = new CreateAnywhereCacheRequest([
52+
'parent' => $formattedName,
53+
'anywhere_cache' => $anywhereCache,
54+
]);
55+
56+
// Start a create operation and block until it completes. Real applications
57+
// may want to setup a callback, wait on a coroutine, or poll until it
58+
// completes.
59+
$operation = $storageControlClient->createAnywhereCache($request);
60+
61+
printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName());
62+
$operation->pollUntilComplete([
63+
'totalPollTimeoutMillis' => 5400000,
64+
'initialPollDelayMillis' => 1000, // Start with 1 second delay
65+
'pollDelayMultiplier' => 2, // Double delay each time
66+
'maxPollDelayMillis' => 60000, // Max 60 seconds delay between polls
67+
]);
68+
69+
/** @var AnywhereCache $anywhereCacheResult */
70+
$anywhereCacheResult = $operation->getResult();
71+
printf('Created anywhere cache: %s' . PHP_EOL, $anywhereCacheResult->getName());
72+
}
73+
# [END storage_control_create_anywhere_cache]
74+
75+
// The following 2 lines are only needed to run the samples
76+
require_once __DIR__ . '/../../testing/sample_helpers.php';
77+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageControl;
26+
27+
# [START storage_control_disable_anywhere_cache]
28+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
29+
use Google\Cloud\Storage\Control\V2\DisableAnywhereCacheRequest;
30+
31+
/**
32+
* Disables an Anywhere Cache instance.
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* (e.g. 'my-bucket')
36+
* @param string $anywhereCacheId Uniquely identifies the cache.
37+
* (e.g. 'us-east1-b')
38+
*/
39+
function disable_anywhere_cache(string $bucketName, string $anywhereCacheId): void
40+
{
41+
$storageControlClient = new StorageControlClient();
42+
43+
// Set project to "_" to signify global bucket
44+
$formattedName = $storageControlClient->anywhereCacheName('_', $bucketName, $anywhereCacheId);
45+
46+
$request = new DisableAnywhereCacheRequest([
47+
'name' => $formattedName
48+
]);
49+
50+
$response = $storageControlClient->disableAnywhereCache($request);
51+
52+
printf('Disabled anywhere cache: %s', $response->getName());
53+
}
54+
# [END storage_control_disable_anywhere_cache]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageControl;
26+
27+
# [START storage_control_get_anywhere_cache]
28+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
29+
use Google\Cloud\Storage\Control\V2\GetAnywhereCacheRequest;
30+
31+
/**
32+
* Gets an Anywhere Cache instance.
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* (e.g. 'my-bucket')
36+
* @param string $anywhereCacheId Uniquely identifies the cache.
37+
* (e.g. 'us-east1-b')
38+
*/
39+
function get_anywhere_cache(string $bucketName, string $anywhereCacheId): void
40+
{
41+
$storageControlClient = new StorageControlClient();
42+
43+
// Set project to "_" to signify global bucket
44+
$formattedName = $storageControlClient->anywhereCacheName('_', $bucketName, $anywhereCacheId);
45+
46+
$request = new GetAnywhereCacheRequest([
47+
'name' => $formattedName,
48+
]);
49+
50+
$response = $storageControlClient->getAnywhereCache($request);
51+
52+
printf('Got anywhere cache: %s', $response->getName());
53+
}
54+
# [END storage_control_get_anywhere_cache]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageControl;
26+
27+
# [START storage_control_list_anywhere_caches]
28+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
29+
use Google\Cloud\Storage\Control\V2\ListAnywhereCachesRequest;
30+
31+
/**
32+
* Lists Anywhere Cache instances for a given bucket.
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* (e.g. 'my-bucket')
36+
*/
37+
function list_anywhere_caches(string $bucketName): void
38+
{
39+
$storageControlClient = new StorageControlClient();
40+
41+
// Set project to "_" to signify global bucket
42+
$formattedName = $storageControlClient->bucketName('_', $bucketName);
43+
44+
$request = new ListAnywhereCachesRequest([
45+
'parent' => $formattedName,
46+
]);
47+
48+
$response = $storageControlClient->listAnywhereCaches($request);
49+
50+
foreach ($response as $anywhereCache) {
51+
printf('Anywhere cache name: %s' . PHP_EOL, $anywhereCache->getName());
52+
}
53+
}
54+
# [END storage_control_list_anywhere_caches]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageControl;
26+
27+
# [START storage_control_pause_anywhere_cache]
28+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
29+
use Google\Cloud\Storage\Control\V2\PauseAnywhereCacheRequest;
30+
31+
/**
32+
* Pauses an Anywhere Cache instance.
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* (e.g. 'my-bucket')
36+
* @param string $anywhereCacheId Uniquely identifies the cache.
37+
* (e.g. 'us-east1-b')
38+
*/
39+
function pause_anywhere_cache(string $bucketName, string $anywhereCacheId): void
40+
{
41+
$storageControlClient = new StorageControlClient();
42+
43+
// Set project to "_" to signify global bucket
44+
$formattedName = $storageControlClient->anywhereCacheName('_', $bucketName, $anywhereCacheId);
45+
46+
$request = new PauseAnywhereCacheRequest([
47+
'name' => $formattedName,
48+
]);
49+
50+
$response = $storageControlClient->pauseAnywhereCache($request);
51+
52+
printf('Paused anywhere cache: %s', $response->getName());
53+
}
54+
# [END storage_control_pause_anywhere_cache]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\StorageControl;
26+
27+
# [START storage_control_resume_anywhere_cache]
28+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
29+
use Google\Cloud\Storage\Control\V2\ResumeAnywhereCacheRequest;
30+
31+
/**
32+
* Resumes a disabled or paused Anywhere Cache instance.
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* (e.g. 'my-bucket')
36+
* @param string $anywhereCacheId Uniquely identifies the cache.
37+
* (e.g. 'us-east1-b')
38+
*/
39+
function resume_anywhere_cache(string $bucketName, string $anywhereCacheId): void
40+
{
41+
$storageControlClient = new StorageControlClient();
42+
43+
// Set project to "_" to signify global bucket
44+
$formattedName = $storageControlClient->anywhereCacheName('_', $bucketName, $anywhereCacheId);
45+
46+
$request = new ResumeAnywhereCacheRequest([
47+
'name' => $formattedName
48+
]);
49+
50+
$response = $storageControlClient->resumeAnywhereCache($request);
51+
52+
printf('Resumed anywhere cache: %s', $response->getName());
53+
}
54+
# [END storage_control_resume_anywhere_cache]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)