|
| 1 | +// Copyright 2015-2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +// [START all] |
| 17 | +// [START setup] |
| 18 | +// By default, gcloud will authenticate using the service account file specified |
| 19 | +// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the |
| 20 | +// project specified by the GCLOUD_PROJECT environment variable. See |
| 21 | +// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication |
| 22 | +var gcloud = require('gcloud'); |
| 23 | + |
| 24 | +// Get a reference to the storage component |
| 25 | +var storage = gcloud.storage(); |
| 26 | +// [END setup] |
| 27 | + |
| 28 | +// [START create] |
| 29 | +/** |
| 30 | + * Creates a new bucket with the given name. |
| 31 | + * |
| 32 | + * @param {string} name The name of the new bucket. |
| 33 | + * @param {function} cb The callback function. |
| 34 | + */ |
| 35 | +function createBucketExample (name, callback) { |
| 36 | + if (!name) { |
| 37 | + return callback(new Error('"name" is required!')); |
| 38 | + } |
| 39 | + |
| 40 | + // See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage?method=createBucket |
| 41 | + storage.createBucket(name, function (err, bucket, apiResponse) { |
| 42 | + if (err) { |
| 43 | + return callback(err); |
| 44 | + } |
| 45 | + |
| 46 | + console.log('Created bucket: ' + name); |
| 47 | + return callback(null, bucket); |
| 48 | + }); |
| 49 | +} |
| 50 | +// [END create] |
| 51 | + |
| 52 | +// [START list] |
| 53 | +/** |
| 54 | + * Fetches all of the current project's buckets. |
| 55 | + * |
| 56 | + * @param {function} cb The callback function. |
| 57 | + */ |
| 58 | +function listBucketsExample (callback) { |
| 59 | + // See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage?method=getBuckets |
| 60 | + storage.getBuckets(function (err, buckets, apiResponse) { |
| 61 | + if (err) { |
| 62 | + return callback(err); |
| 63 | + } |
| 64 | + |
| 65 | + console.log('Found ' + buckets.length + ' buckets!'); |
| 66 | + return callback(null, buckets, apiResponse); |
| 67 | + }); |
| 68 | +} |
| 69 | +// [END list] |
| 70 | + |
| 71 | +// [START delete] |
| 72 | +/** |
| 73 | + * Deletes the specified bucket. |
| 74 | + * |
| 75 | + * @param {string} name The name of the bucket to delete. |
| 76 | + * @param {function} cb The callback function. |
| 77 | + */ |
| 78 | +function deleteBucketExample (name, callback) { |
| 79 | + if (!name) { |
| 80 | + return callback(new Error('"name" is required!')); |
| 81 | + } |
| 82 | + |
| 83 | + // See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage?method=bucket |
| 84 | + var bucket = storage.bucket(name); |
| 85 | + |
| 86 | + // See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/bucket?method=delete |
| 87 | + bucket.delete(function (err, apiResponse) { |
| 88 | + if (err) { |
| 89 | + return callback(err); |
| 90 | + } |
| 91 | + |
| 92 | + console.log('Deleted bucket: ' + name); |
| 93 | + return callback(null, apiResponse); |
| 94 | + }); |
| 95 | +} |
| 96 | +// [END delete] |
| 97 | + |
| 98 | +// [START usage] |
| 99 | +function printUsage () { |
| 100 | + console.log('Usage: node buckets [COMMAND] [ARGS...]'); |
| 101 | + console.log('\nCommands:\n'); |
| 102 | + console.log('\tcreate [BUCKET_NAME]'); |
| 103 | + console.log('\tlist'); |
| 104 | + console.log('\tdelete [BUCKET_NAME]'); |
| 105 | +} |
| 106 | +// [END usage] |
| 107 | + |
| 108 | +// Run the command-line program |
| 109 | +function main (args, cb) { |
| 110 | + var command = args.shift(); |
| 111 | + if (command === 'create') { |
| 112 | + createBucketExample(args[0], cb); |
| 113 | + } else if (command === 'list') { |
| 114 | + listBucketsExample(cb); |
| 115 | + } else if (command === 'delete') { |
| 116 | + deleteBucketExample(args[0], cb); |
| 117 | + } else { |
| 118 | + printUsage(); |
| 119 | + cb(); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +if (module === require.main) { |
| 124 | + main(process.argv.slice(2), console.log); |
| 125 | +} |
| 126 | +// [END all] |
| 127 | + |
| 128 | +exports.createBucketExample = createBucketExample; |
| 129 | +exports.listBucketsExample = listBucketsExample; |
| 130 | +exports.deleteBucketExample = deleteBucketExample; |
| 131 | +exports.printUsage = printUsage; |
| 132 | +exports.main = main; |
0 commit comments