|
| 1 | +/** |
| 2 | + * @fileoverview Sample code that grabs default credentials from the |
| 3 | + * environment, then uses it to make an api call. |
| 4 | + */ |
| 5 | + |
| 6 | +var google = require('googleapis'); |
| 7 | + |
| 8 | +/** |
| 9 | + * Fetches a list of the buckets under the given project id. |
| 10 | + * |
| 11 | + * @param {String} projectId - the project id that owns the buckets. |
| 12 | + * @param {requestCallback} callback - a function to be called when the server |
| 13 | + * responds with the list of buckets. |
| 14 | + */ |
| 15 | +// [START list_buckets] |
| 16 | +function listBuckets(projectId, callback) { |
| 17 | + google.auth.getApplicationDefault(function(error, authClient) { |
| 18 | + if (error) { |
| 19 | + return callback(error); |
| 20 | + } |
| 21 | + |
| 22 | + // Depending on the environment that provides the default credentials |
| 23 | + // (eg Compute Engine, App Engine), the credentials may require us to |
| 24 | + // specify the scopes we need explicitly. |
| 25 | + // Check for this case, and inject the Cloud Storage scope if required. |
| 26 | + if (authClient.createScopedRequired && |
| 27 | + authClient.createScopedRequired()) { |
| 28 | + authClient = authClient.createScoped( |
| 29 | + ['https://www.googleapis.com/auth/devstorage.read_write']); |
| 30 | + } |
| 31 | + |
| 32 | + // Create the service object. |
| 33 | + var storage = google.storage('v1'); |
| 34 | + // Make the api call to list the buckets. |
| 35 | + storage.buckets.list({ |
| 36 | + auth: authClient, |
| 37 | + project: projectId |
| 38 | + }, callback); |
| 39 | + }); |
| 40 | +} |
| 41 | +// [END list_buckets] |
| 42 | + |
| 43 | +module.exports = { |
| 44 | + listBuckets: listBuckets |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Callback for remote requests. |
| 49 | + * |
| 50 | + * @callback requestCallback |
| 51 | + * @param {Object} error - if there's an error with the request, this is pass |
| 52 | + * through to the callback. |
| 53 | + * @param {Object} response - the response for the request. |
| 54 | + */ |
0 commit comments