forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.js
358 lines (322 loc) · 12.2 KB
/
files.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// 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 list_files]
/**
* Lists files in a bucket.
*
* @param {string} name The name of the bucket.
* @param {function} cb The callback function.
*/
function listFiles (name, callback) {
var bucket = storage.bucket(name);
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
bucket.getFiles(function (err, files) {
if (err) {
return callback(err);
}
console.log('Found %d file(s)!', files.length);
return callback(null, files);
});
}
// [END list_files]
// [START list_files_with_prefix]
/**
* Lists files in a bucket that match a certain prefix.
*
* This can be used to list all blobs in a "folder", e.g. "public/".
*
* The delimiter argument can be used to restrict the results to only the
* "files" in the given "folder". Without the delimiter, the entire tree under
* the prefix is returned. For example, given these blobs:
*
* /a/1.txt
* /a/b/2.txt
*
* If you just specify prefix = '/a', you'll get back:
*
* /a/1.txt
* /a/b/2.txt
*
* However, if you specify prefix='/a' and delimiter='/', you'll get back:
*
* /a/1.txt
*
* @param {object} options Configuration options.
* @param {string} options.bucket The name of the bucket.
* @param {string} options.prefix Filter results to objects whose names begin
* with this prefix.
* @param {string} [options.delimiter] Optional. Results will contain only
* objects whose names, aside from the prefix, do not contain delimiter.
* @param {function} cb The callback function.
*/
function listFilesByPrefix (options, callback) {
var bucket = storage.bucket(options.bucket);
var config = {
prefix: options.prefix
};
if (options.delimiter) {
config.delimiter = options.delimiter;
}
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
bucket.getFiles(config, function (err, files) {
if (err) {
return callback(err);
}
console.log('Found %d file(s)!', files.length);
return callback(null, files);
});
}
// [END list_files_with_prefix]
// [START upload_file]
/**
* Upload a file to a bucket.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The name of the bucket.
* @param {string} options.srcFile The name of the file.
* @param {function} cb The callback function.
*/
function uploadFile (options, callback) {
var bucket = storage.bucket(options.bucket);
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
bucket.upload(options.srcFile, function (err, file) {
if (err) {
return callback(err);
}
console.log('Uploaded gs://%s/%s', options.bucket, options.srcFile);
return callback(null, file);
});
}
// [END upload_file]
// [START download_file]
/**
* Download a file from a bucket.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The name of the bucket.
* @param {string} options.srcFile The source file name.
* @param {string} options.destFile The destination file name.
* @param {function} cb The callback function.
*/
function downloadFile (options, callback) {
var file = storage.bucket(options.bucket).file(options.srcFile);
var config = {
destination: options.destFile
};
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/file
file.download(config, function (err) {
if (err) {
return callback(err);
}
console.log('Downloaded gs://%s/%s to %s', options.bucket, options.srcFile, options.destFile);
return callback(null);
});
}
// [END download_file]
// [START delete_file]
/**
* Delete a file from a bucket.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The name of the bucket.
* @param {string} options.file The name of the file to delete.
* @param {function} cb The callback function.
*/
function deleteFile (options, callback) {
var file = storage.bucket(options.bucket).file(options.file);
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/file
file.delete(function (err) {
if (err) {
return callback(err);
}
console.log('Deleted gs://%s/%s', options.bucket, options.file);
return callback(null);
});
}
// [END delete_file]
// [START get_metadata]
/**
* Get a file's metadata.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The name of the bucket.
* @param {string} options.file The name of the file.
* @param {function} cb The callback function.
*/
function getMetadata (options, callback) {
var file = storage.bucket(options.bucket).file(options.file);
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/file
file.getMetadata(function (err, metadata) {
if (err) {
return callback(err);
}
console.log('Got metadata for gs://%s/%s', options.bucket, options.file);
return callback(null, metadata);
});
}
// [END get_metadata]
// [START public]
/**
* Make a file public.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The name of the bucket.
* @param {string} options.file The name of the file to make public.
* @param {function} cb The callback function.
*/
function makePublic (options, callback) {
var file = storage.bucket(options.bucket).file(options.file);
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/file
file.makePublic(function (err) {
if (err) {
return callback(err);
}
console.log('Made gs://%s/%s public!', options.bucket, options.file);
return callback(null);
});
}
// [END public]
// [START move_file]
/**
* Move a file to a new location within the same bucket, i.e. rename the file.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The name of the bucket.
* @param {string} options.srcFile The source file name.
* @param {string} options.destFile The destination file name.
* @param {function} cb The callback function.
*/
function moveFile (options, callback) {
var file = storage.bucket(options.bucket).file(options.srcFile);
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/file
file.move(options.destFile, function (err, file) {
if (err) {
return callback(err);
}
console.log('Renamed gs://%s/%s to gs://%s/%s', options.bucket, options.srcFile, options.bucket, options.destFile);
return callback(null, file);
});
}
// [END move_file]
// [START copy_file]
/**
* Copy a file to a new bucket with a new name.
*
* @param {object} options Configuration options.
* @param {string} options.srcBucket The name of the bucket.
* @param {string} options.srcFile The source file name.
* @param {string} options.destBucket The destination bucket name.
* @param {string} options.destFile The destination file name.
* @param {function} cb The callback function.
*/
function copyFile (options, callback) {
var file = storage.bucket(options.srcBucket).file(options.srcFile);
var copy = storage.bucket(options.destBucket).file(options.destFile);
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/file
file.copy(copy, function (err, file) {
if (err) {
return callback(err);
}
console.log('Copied gs://%s/%s to gs://%s/%s', options.srcBucket, options.srcFile, options.destBucket, options.destFile);
return callback(null, file);
});
}
// [END copy_file]
// [END all]
// The command-line program
var cli = require('yargs');
var utils = require('../utils');
var program = module.exports = {
listFiles: listFiles,
listFilesByPrefix: listFilesByPrefix,
uploadFile: uploadFile,
downloadFile: downloadFile,
deleteFile: deleteFile,
getMetadata: getMetadata,
makePublic: makePublic,
moveFile: moveFile,
copyFile: copyFile,
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};
cli
.demand(1)
.command('list <bucket> [options]', 'List files in a bucket, optionally filtering by a prefix.', {
prefix: {
alias: 'p',
requiresArg: true,
type: 'string',
description: 'Filter files by a prefix.'
},
delimiter: {
alias: 'd',
requiresArg: true,
type: 'string',
description: 'Specify a delimiter.'
}
}, function (options) {
if (options.prefix) {
program.listFilesByPrefix(utils.pick(options, ['bucket', 'prefix', 'delimiter']), utils.makeHandler(true, 'name'));
} else {
program.listFiles(options.bucket, utils.makeHandler(true, 'name'));
}
})
.command('upload <bucket> <srcFile>', 'Upload a local file to a bucket.', {}, function (options) {
program.uploadFile(utils.pick(options, ['bucket', 'srcFile']), utils.makeHandler(false));
})
.command('download <bucket> <srcFile> <destFile>', 'Download a file from a bucket.', {}, function (options) {
program.downloadFile(utils.pick(options, ['bucket', 'srcFile', 'destFile']), utils.makeHandler(false));
})
.command('delete <bucket> <file>', 'Delete a file from a bucket.', {}, function (options) {
program.deleteFile(utils.pick(options, ['bucket', 'file']), utils.makeHandler(false));
})
.command('getMetadata <bucket> <file>', 'Get metadata for a file in a bucket.', {}, function (options) {
program.getMetadata(utils.pick(options, ['bucket', 'file']), utils.makeHandler());
})
.command('makePublic <bucket> <file>', 'Make a file public in a bucket.', {}, function (options) {
program.makePublic(utils.pick(options, ['bucket', 'file']), utils.makeHandler(false));
})
.command('move <bucket> <srcFile> <destFile>', 'Move a file to a new location within the same bucket, i.e. rename the file.', {}, function (options) {
program.moveFile(utils.pick(options, ['bucket', 'srcFile', 'destFile']), utils.makeHandler(false));
})
.command('copy <srcBucket> <srcFile> <destBucket> <destFile>', 'Copy a file in a bucket to another bucket.', {}, function (options) {
program.copyFile(utils.pick(options, ['srcBucket', 'srcFile', 'destBucket', 'destFile']), utils.makeHandler(false));
})
.example('node $0 list my-bucket', 'List files in "my-bucket".')
.example('node $0 list my-bucket -p public/', 'List files in "my-bucket" filtered by prefix "public/".')
.example('node $0 upload my-bucket ./file.txt', 'Upload "./file.txt" to "my-bucket".')
.example('node $0 download my-bucket file.txt ./file.txt', 'Download "gs://my-bucket/file.txt" to "./file.txt".')
.example('node $0 delete my-bucket file.txt', 'Delete "gs://my-bucket/file.txt".')
.example('node $0 getMetadata my-bucket file.txt', 'Get metadata for "gs://my-bucket/file.txt".')
.example('node $0 makePublic my-bucket file.txt', 'Make "gs://my-bucket/file.txt" public.')
.example('node $0 move my-bucket file.txt file2.txt', 'Rename "gs://my-bucket/file.txt" to "gs://my-bucket/file2.txt".')
.example('node $0 copy my-bucket file.txt my-other-bucket file.txt', 'Copy "gs://my-bucket/file.txt" to "gs://my-other-bucket/file.txt".')
.wrap(100)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/storage/docs');
if (module === require.main) {
program.main(process.argv.slice(2));
}