forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.js
372 lines (314 loc) · 11.1 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* Copyright 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.
*/
/**
* This application demonstrates how to perform basic operations on files with
* the Google Cloud Storage API.
*
* For more information, see the README.md under /storage and the documentation
* at https://cloud.google.com/storage/docs.
*/
'use strict';
const Storage = require('@google-cloud/storage');
// [START storage_list_files]
function listFiles (bucketName) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(bucketName);
// Lists files in the bucket
return bucket.getFiles()
.then((results) => {
const files = results[0];
console.log('Files:');
files.forEach((file) => console.log(file.name));
return files;
});
}
// [END storage_list_files]
// [START storage_list_files_with_prefix]
function listFilesByPrefix (bucketName, prefix, delimiter) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(bucketName);
/**
* 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
*/
const options = {
prefix: prefix
};
if (delimiter) {
options.delimiter = delimiter;
}
// Lists files in the bucket, filtered by a prefix
return bucket.getFiles(options)
.then((results) => {
const files = results[0];
console.log('Files:');
files.forEach((file) => console.log(file.name));
return files;
});
}
// [END storage_list_files_with_prefix]
// [START storage_upload_file]
function uploadFile (bucketName, fileName) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(bucketName);
// Uploads a local file to the bucket, e.g. "./local/path/to/file.txt"
return bucket.upload(fileName)
.then((results) => {
const file = results[0];
console.log(`File ${file.name} uploaded.`);
return file;
});
}
// [END storage_upload_file]
// [START storage_download_file]
function downloadFile (bucketName, srcFileName, destFileName) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(bucketName);
// References an existing file, e.g. "file.txt"
const file = bucket.file(srcFileName);
const options = {
// The path to which the file should be downloaded, e.g. "./file.txt"
destination: destFileName
};
// Downloads the file
return file.download(options)
.then(() => {
console.log(`File ${file.name} downloaded to ${destFileName}.`);
});
}
// [END storage_download_file]
// [START storage_delete_file]
function deleteFile (bucketName, fileName) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(bucketName);
// References an existing file, e.g. "file.txt"
const file = bucket.file(fileName);
// Deletes the file from the bucket
return file.delete()
.then(() => {
console.log(`File ${fileName} deleted.`);
});
}
// [END storage_delete_file]
// [START storage_get_metadata]
function getMetadata (bucketName, fileName) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(bucketName);
// References an existing file, e.g. "file.txt"
const file = bucket.file(fileName);
// Gets the metadata for the file
return file.getMetadata()
.then((results) => {
const metadata = results[0];
console.log(`File: ${metadata.name}`);
console.log(`Bucket: ${metadata.bucket}`);
console.log(`Storage class: ${metadata.storageClass}`);
console.log(`ID: ${metadata.id}`);
console.log(`Size: ${metadata.size}`);
console.log(`Updated: ${metadata.updated}`);
console.log(`Generation: ${metadata.generation}`);
console.log(`Metageneration: ${metadata.metageneration}`);
console.log(`Etag: ${metadata.etag}`);
console.log(`Owner: ${metadata.owner}`);
console.log(`Component count: ${metadata.component_count}`);
console.log(`Crc32c: ${metadata.crc32c}`);
console.log(`md5Hash: ${metadata.md5Hash}`);
console.log(`Cache-control: ${metadata.cacheControl}`);
console.log(`Content-type: ${metadata.contentType}`);
console.log(`Content-disposition: ${metadata.contentDisposition}`);
console.log(`Content-encoding: ${metadata.contentEncoding}`);
console.log(`Content-language: ${metadata.contentLanguage}`);
console.log(`Metadata: ${metadata.metadata}`);
return metadata;
});
}
// [END storage_get_metadata]
// [START storage_make_public]
function makePublic (bucketName, fileName) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(bucketName);
// References an existing file, e.g. "file.txt"
const file = bucket.file(fileName);
// Makes the file public
return file.makePublic()
.then(() => {
console.log(`File ${file.name} is now public.`);
});
}
// [END storage_make_public]
// [START storage_generate_signed_url]
function generateSignedUrl (bucketName, fileName) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(bucketName);
// References an existing file, e.g. "file.txt"
const file = bucket.file(fileName);
// These options will allow temporary read access to the file
const options = {
action: 'read',
expires: '03-17-2025'
};
// Get a signed URL for the file
return file.getSignedUrl(options)
.then((results) => {
const url = results[0];
console.log(`The signed url for ${file.name} is ${url}.`);
return url;
});
}
// [END storage_generate_signed_url]
// [START storage_move_file]
function moveFile (bucketName, srcFileName, destFileName) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(bucketName);
// References an existing file, e.g. "file.txt"
const file = bucket.file(srcFileName);
// Moves the file within the bucket
return file.move(destFileName)
.then(() => {
console.log(`File ${file.name} moved to ${destFileName}.`);
});
}
// [END storage_move_file]
// [START storage_copy_file]
function copyFile (srcBucketName, srcFileName, destBucketName, destFileName) {
// Instantiates a client
const storage = Storage();
// References an existing bucket, e.g. "my-bucket"
const bucket = storage.bucket(srcBucketName);
// References an existing file, e.g. "file.txt"
const file = bucket.file(srcFileName);
// References another existing bucket, e.g. "my-other-bucket"
const destBucket = storage.bucket(destBucketName);
// Creates a reference to a destination file, e.g. "file.txt"
const destFile = destBucket.file(destFileName);
// Copies the file to the other bucket
return file.copy(destFile)
.then((results) => {
const file = results[0];
console.log(`File ${srcFileName} copied to ${file.name} in ${destBucket.name}.`);
return file;
});
}
// [END storage_copy_file]
require(`yargs`)
.demand(1)
.command(
`list <bucketName> [prefix] [delimiter]`,
`Lists files in a bucket, optionally filtering by a prefix.`,
{},
(opts) => {
if (opts.prefix) {
listFilesByPrefix(opts.bucketName, opts.prefix, opts.delimiter);
} else {
listFiles(opts.bucketName);
}
}
)
.command(
`upload <bucketName> <srcFileName>`,
`Uploads a local file to a bucket.`,
{},
(opts) => uploadFile(opts.bucketName, opts.srcFileName)
)
.command(
`download <bucketName> <srcFileName> <destFileName>`,
`Downloads a file from a bucket.`,
{},
(opts) => downloadFile(opts.bucketName, opts.srcFileName, opts.destFileName)
)
.command(
`delete <bucketName> <fileName>`,
`Deletes a file from a bucket.`,
{},
(opts) => deleteFile(opts.bucketName, opts.fileName)
)
.command(
`get-metadata <bucketName> <fileName>`,
`Gets the metadata for a file.`,
{},
(opts) => getMetadata(opts.bucketName, opts.fileName)
)
.command(
`make-public <bucketName> <fileName>`,
`Makes a file public.`,
{},
(opts) => makePublic(opts.bucketName, opts.fileName)
)
.command(
`generate-signed-url <bucketName> <fileName>`,
`Generates a signed URL for a file.`,
{},
(opts) => generateSignedUrl(opts.bucketName, opts.fileName)
)
.command(
`move <bucketName> <srcFileName> <destFileName>`,
`Moves a file to a new location within the same bucket, i.e. rename the file.`,
{},
(opts) => moveFile(opts.bucketName, opts.srcFileName, opts.destFileName)
)
.command(
`copy <srcBucketName> <srcFileName> <destBucketName> <destFileName>`,
`Copies a file in a bucket to another bucket.`,
{},
(opts) => copyFile(opts.srcBucketName, opts.srcFileName, opts.destBucketName, opts.destFileName)
)
.example(`node $0 list my-bucket`, `Lists files in "my-bucket".`)
.example(`node $0 list my-bucket public/`, `Lists files in "my-bucket" filtered by prefix "public/".`)
.example(`node $0 upload my-bucket ./file.txt`, `Uploads "./file.txt" to "my-bucket".`)
.example(`node $0 download my-bucket file.txt ./file.txt`, `Downloads "gs://my-bucket/file.txt" to "./file.txt".`)
.example(`node $0 delete my-bucket file.txt`, `Deletes "gs://my-bucket/file.txt".`)
.example(`node $0 get-metadata my-bucket file.txt`, `Gets the metadata for "gs://my-bucket/file.txt".`)
.example(`node $0 make-public my-bucket file.txt`, `Makes "gs://my-bucket/file.txt" public.`)
.example(`node $0 move my-bucket file.txt file2.txt`, `Renames "gs://my-bucket/file.txt" to "gs://my-bucket/file2.txt".`)
.example(`node $0 copy my-bucket file.txt my-other-bucket file.txt`, `Copies "gs://my-bucket/file.txt" to "gs://my-other-bucket/file.txt".`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/storage/docs`)
.help()
.strict()
.argv;