|
| 1 | +/** |
| 2 | + * Copyright 2017, Google, Inc. |
| 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 | + * http://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 | + |
| 16 | +/** |
| 17 | + * Demonstrates how to authenticate to Google Cloud Platform APIs using the |
| 18 | + * Google Cloud Client Libraries. |
| 19 | + */ |
| 20 | + |
| 21 | +'use strict'; |
| 22 | + |
| 23 | +function authCloudImplicit () { |
| 24 | + // [START auth_cloud_implicit] |
| 25 | + // Imports the Google Cloud client library. |
| 26 | + const Storage = require('@google-cloud/storage'); |
| 27 | + |
| 28 | + // Instantiates a client. If you don't specify credentials when constructing |
| 29 | + // the client, the client library will look for credentials in the |
| 30 | + // environment. |
| 31 | + const storage = Storage(); |
| 32 | + |
| 33 | + // Makes an authenticated API request. |
| 34 | + storage |
| 35 | + .getBuckets() |
| 36 | + .then((results) => { |
| 37 | + const buckets = results[0]; |
| 38 | + |
| 39 | + console.log('Buckets:'); |
| 40 | + buckets.forEach((bucket) => { |
| 41 | + console.log(bucket.name); |
| 42 | + }); |
| 43 | + }) |
| 44 | + .catch((err) => { |
| 45 | + console.error('ERROR:', err); |
| 46 | + }); |
| 47 | + // [END auth_cloud_implicit] |
| 48 | +} |
| 49 | + |
| 50 | +function authCloudExplicit () { |
| 51 | + // [START auth_cloud_explicit] |
| 52 | + // Imports the Google Cloud client library. |
| 53 | + const Storage = require('@google-cloud/storage'); |
| 54 | + |
| 55 | + // Instantiates a client. Explicitly use service account credentials by |
| 56 | + // specifying the private key file. All clients in google-cloud-node have this |
| 57 | + // helper, see https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/latest/guides/authentication |
| 58 | + const storage = Storage({ |
| 59 | + keyFilename: '/path/to/keyfile.json' |
| 60 | + }); |
| 61 | + |
| 62 | + // Makes an authenticated API request. |
| 63 | + storage |
| 64 | + .getBuckets() |
| 65 | + .then((results) => { |
| 66 | + const buckets = results[0]; |
| 67 | + |
| 68 | + console.log('Buckets:'); |
| 69 | + buckets.forEach((bucket) => { |
| 70 | + console.log(bucket.name); |
| 71 | + }); |
| 72 | + }) |
| 73 | + .catch((err) => { |
| 74 | + console.error('ERROR:', err); |
| 75 | + }); |
| 76 | + // [END auth_cloud_explicit] |
| 77 | +} |
| 78 | + |
| 79 | +const cli = require(`yargs`) |
| 80 | + .demand(1) |
| 81 | + .command( |
| 82 | + `auth-cloud-implicit`, |
| 83 | + `Loads credentials implicitly.`, |
| 84 | + {}, |
| 85 | + authCloudImplicit |
| 86 | + ) |
| 87 | + .command( |
| 88 | + `auth-cloud-explicit`, |
| 89 | + `Loads credentials implicitly.`, |
| 90 | + {}, |
| 91 | + authCloudExplicit |
| 92 | + ) |
| 93 | + .example(`node $0 implicit`, `Loads credentials implicitly.`) |
| 94 | + .example(`node $0 explicit`, `Loads credentials explicitly.`) |
| 95 | + .wrap(120) |
| 96 | + .recommendCommands() |
| 97 | + .epilogue(`For more information, see https://cloud.google.com/docs/authentication`) |
| 98 | + .help() |
| 99 | + .strict(); |
| 100 | + |
| 101 | +if (module === require.main) { |
| 102 | + cli.parse(process.argv.slice(2)); |
| 103 | +} |
0 commit comments