|
23 | 23 |
|
24 | 24 | declare(strict_types=1); |
25 | 25 |
|
26 | | -require_once __DIR__ . '/../vendor/autoload.php'; |
27 | | - |
28 | | -if (count($argv) != 4) { |
29 | | - return printf("Usage: php %s PROJECT_ID SECRET_ID VERSION_ID\n", basename(__FILE__)); |
30 | | -} |
31 | | -list($_, $projectId, $secretId, $versionId) = $argv; |
| 26 | +namespace Google\Cloud\Samples\SecretManager; |
32 | 27 |
|
33 | 28 | // [START secretmanager_access_secret_version] |
34 | 29 | // Import the Secret Manager client library. |
35 | 30 | use Google\Cloud\SecretManager\V1\SecretManagerServiceClient; |
36 | 31 |
|
37 | | -/** Uncomment and populate these variables in your code */ |
38 | | -// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project'); |
39 | | -// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret'); |
40 | | -// $versionId = 'YOUR_VERSION_ID' (e.g. 'latest' or '5'); |
41 | | - |
42 | | -// Create the Secret Manager client. |
43 | | -$client = new SecretManagerServiceClient(); |
44 | | - |
45 | | -// Build the resource name of the secret version. |
46 | | -$name = $client->secretVersionName($projectId, $secretId, $versionId); |
47 | | - |
48 | | -// Access the secret version. |
49 | | -$response = $client->accessSecretVersion($name); |
50 | | - |
51 | | -// Print the secret payload. |
52 | | -// |
53 | | -// WARNING: Do not print the secret in a production environment - this |
54 | | -// snippet is showing how to access the secret material. |
55 | | -$payload = $response->getPayload()->getData(); |
56 | | -printf('Plaintext: %s', $payload); |
| 32 | +/** |
| 33 | + * @param string $projectId Your Google Cloud Project ID (e.g. 'my-project') |
| 34 | + * @param string $secretId Your secret ID (e.g. 'my-secret') |
| 35 | + * @param string $versionId Your version ID (e.g. 'latest' or '5'); |
| 36 | + */ |
| 37 | +function access_secret_version(string $projectId, string $secretId, string $versionId): void |
| 38 | +{ |
| 39 | + // Create the Secret Manager client. |
| 40 | + $client = new SecretManagerServiceClient(); |
| 41 | + |
| 42 | + // Build the resource name of the secret version. |
| 43 | + $name = $client->secretVersionName($projectId, $secretId, $versionId); |
| 44 | + |
| 45 | + // Access the secret version. |
| 46 | + $response = $client->accessSecretVersion($name); |
| 47 | + |
| 48 | + // Print the secret payload. |
| 49 | + // |
| 50 | + // WARNING: Do not print the secret in a production environment - this |
| 51 | + // snippet is showing how to access the secret material. |
| 52 | + $payload = $response->getPayload()->getData(); |
| 53 | + printf('Plaintext: %s', $payload); |
| 54 | +} |
57 | 55 | // [END secretmanager_access_secret_version] |
| 56 | + |
| 57 | +// The following 2 lines are only needed to execute the samples on the CLI |
| 58 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 59 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments