|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2020 Google LLC. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 21 | + |
| 22 | +if (count($argv) != 3) { |
| 23 | + return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__)); |
| 24 | +} |
| 25 | +list($_, $projectId, $secretId) = $argv; |
| 26 | + |
| 27 | +// [START secretmanager_quickstart] |
| 28 | +// Import the Secret Manager client library. |
| 29 | +use Google\Cloud\SecretManager\V1Beta1\Replication; |
| 30 | +use Google\Cloud\SecretManager\V1Beta1\Replication\Automatic; |
| 31 | +use Google\Cloud\SecretManager\V1Beta1\Secret; |
| 32 | +use Google\Cloud\SecretManager\V1Beta1\SecretManagerServiceClient; |
| 33 | +use Google\Cloud\SecretManager\V1Beta1\SecretPayload; |
| 34 | + |
| 35 | +/** Uncomment and populate these variables in your code */ |
| 36 | +// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project'); |
| 37 | +// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret'); |
| 38 | + |
| 39 | +// Create the Secret Manager client. |
| 40 | +$client = new SecretManagerServiceClient(); |
| 41 | + |
| 42 | +// Build the parent name from the project. |
| 43 | +$parent = $client->projectName($projectId); |
| 44 | + |
| 45 | +// Create the parent secret. |
| 46 | +$secret = $client->createSecret($parent, $secretId, [ |
| 47 | + 'secret' => new Secret([ |
| 48 | + 'replication' => new Replication([ |
| 49 | + 'automatic' => new Automatic(), |
| 50 | + ]), |
| 51 | + ]), |
| 52 | +]); |
| 53 | + |
| 54 | +// Add the secret version. |
| 55 | +$version = $client->addSecretVersion($secret->getName(), new SecretPayload([ |
| 56 | + 'data' => 'hello world', |
| 57 | +])); |
| 58 | + |
| 59 | +// Access the secret version. |
| 60 | +$response = $client->accessSecretVersion($version->getName()); |
| 61 | + |
| 62 | +// Print the secret payload. |
| 63 | +// |
| 64 | +// WARNING: Do not print the secret in a production environment - this |
| 65 | +// snippet is showing how to access the secret material. |
| 66 | +$payload = $response->getPayload()->getData(); |
| 67 | +printf('Plaintext: %s' . PHP_EOL, $payload); |
| 68 | +// [END secretmanager_quickstart] |
0 commit comments