forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuckets.js
126 lines (111 loc) · 3.81 KB
/
buckets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2015-2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
// [START all]
// [START setup]
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
var Storage = require('@google-cloud/storage');
// Instantiate a storage client
var storage = Storage();
// [END setup]
// [START create_bucket]
/**
* Creates a new bucket with the given name.
*
* @param {string} name The name of the new bucket.
* @param {function} cb The callback function.
*/
function createBucket (name, callback) {
var bucket = storage.bucket(name);
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
bucket.create(function (err, bucket) {
if (err) {
return callback(err);
}
console.log('Created bucket: %s', name);
return callback(null, bucket);
});
}
// [END create_bucket]
// [START list_buckets]
/**
* List all of the authenticated project's buckets.
*
* @param {function} cb The callback function.
*/
function listBuckets (callback) {
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage
storage.getBuckets(function (err, buckets) {
if (err) {
return callback(err);
}
console.log('Found %d bucket(s)!', buckets.length);
return callback(null, buckets);
});
}
// [END list_buckets]
// [START delete_bucket]
/**
* Deletes the specified bucket.
*
* @param {string} name The name of the bucket to delete.
* @param {function} cb The callback function.
*/
function deleteBucket (name, callback) {
var bucket = storage.bucket(name);
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
bucket.delete(function (err) {
if (err) {
return callback(err);
}
console.log('Deleted bucket: %s', name);
return callback(null);
});
}
// [END delete_bucket]
// [END all]
// The command-line program
var cli = require('yargs');
var utils = require('../utils');
var program = module.exports = {
createBucket: createBucket,
listBuckets: listBuckets,
deleteBucket: deleteBucket,
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};
cli
.demand(1)
.command('create <bucket>', 'Create a new bucket with the given name.', {}, function (options) {
program.createBucket(options.bucket, utils.makeHandler(false));
})
.command('list', 'List all buckets in the authenticated project.', {}, function () {
program.listBuckets(utils.makeHandler(true, 'name'));
})
.command('delete <bucket>', 'Delete the specified bucket.', {}, function (options) {
program.deleteBucket(options.bucket, utils.makeHandler(false));
})
.example('node $0 create my-bucket', 'Create a new bucket named "my-bucket".')
.example('node $0 list', 'List all buckets in the authenticated project.')
.example('node $0 delete my-bucket', 'Delete "my-bucket".')
.wrap(80)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/storage/docs');
if (module === require.main) {
program.main(process.argv.slice(2));
}