|
| 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); |
0 commit comments