|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +async function main(name = 'projects/my-project/secrets/my-secret/versions/1') { |
| 18 | + // [START secretmanager_access_secret_version] |
| 19 | + /** |
| 20 | + * TODO(developer): Uncomment these variables before running the sample. |
| 21 | + */ |
| 22 | + // const name = 'projects/my-project/secrets/my-secret/versions/5'; |
| 23 | + // const name = 'projects/my-project/secrets/my-secret/versions/latest'; |
| 24 | + |
| 25 | + // Imports the Secret Manager library |
| 26 | + const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); |
| 27 | + |
| 28 | + // Instantiates a client |
| 29 | + const client = new SecretManagerServiceClient(); |
| 30 | + |
| 31 | + async function accessSecretVersion() { |
| 32 | + const [version] = await client.accessSecretVersion({ |
| 33 | + name: name, |
| 34 | + }); |
| 35 | + |
| 36 | + // Extract the payload as a string. |
| 37 | + const payload = version.payload.data.toString(); |
| 38 | + |
| 39 | + // WARNING: Do not print the secret in a production environment - this |
| 40 | + // snippet is showing how to access the secret material. |
| 41 | + console.info(`Payload: ${payload}`); |
| 42 | + } |
| 43 | + |
| 44 | + accessSecretVersion(); |
| 45 | + // [END secretmanager_access_secret_version] |
| 46 | +} |
| 47 | + |
| 48 | +const args = process.argv.slice(2); |
| 49 | +main(...args).catch(console.error); |
0 commit comments