This repository was archived by the owner on Mar 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathfiles.test.js
More file actions
683 lines (610 loc) · 22.4 KB
/
files.test.js
File metadata and controls
683 lines (610 loc) · 22.4 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Copyright 2019 Google LLC
//
// 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';
const fs = require('fs');
const path = require('path');
const {Storage} = require('@google-cloud/storage');
const {assert} = require('chai');
const {before, after, it, describe} = require('mocha');
const cp = require('child_process');
const fetch = require('node-fetch');
const uuid = require('uuid');
const {promisify} = require('util');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const storage = new Storage();
const cwd = path.join(__dirname, '..');
const bucketName = generateName();
const bucket = storage.bucket(bucketName);
const softDeleteBucketName = generateName();
const softDeleteBucket = storage.bucket(softDeleteBucketName);
const objectRetentionBucketName = generateName();
const objectRetentionBucket = storage.bucket(objectRetentionBucketName);
const fileContents = 'these-are-my-contents';
const fileName = 'test.txt';
const memoryFileName = 'testmemory.txt';
const movedFileName = 'test2.txt';
const copiedFileName = 'test3.txt';
const renamedFileName = 'test4.txt';
const signedFileName = 'signed-upload.txt';
const kmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_US;
const filePath = path.join(cwd, 'resources', fileName);
const folderPath = path.join(cwd, 'resources');
const downloadFilePath = path.join(cwd, 'downloaded.txt');
const startByte = 0;
const endByte = 20;
const doesNotExistPrecondition = 0;
const fileContent = fs.readFileSync(filePath, 'utf-8');
describe('file', () => {
before(async () => {
await bucket.create();
});
after(async () => {
await promisify(fs.unlink)(downloadFilePath).catch(console.error);
// Try deleting all files twice, just to make sure
await bucket.deleteFiles({force: true}).catch(console.error);
await bucket.deleteFiles({force: true}).catch(console.error);
await bucket.delete().catch(console.error);
});
it('should upload a file', async () => {
const output = execSync(
`node uploadFile.js ${bucketName} ${filePath} ${fileName} ${doesNotExistPrecondition}`
);
assert.match(output, new RegExp(`${filePath} uploaded to ${bucketName}`));
const [exists] = await bucket.file(fileName).exists();
assert.strictEqual(exists, true);
});
it('should upload a file from memory', async () => {
const output = execSync(
`node uploadFromMemory.js ${bucketName} ${fileContents} ${memoryFileName}`
);
assert.match(
output,
new RegExp(
`${memoryFileName} with contents ${fileContents} uploaded to ${bucketName}.`
)
);
const [exists] = await bucket.file(memoryFileName).exists();
assert.strictEqual(exists, true);
});
it('should upload a file without authentication', async () => {
const output = execSync(
`node uploadWithoutAuthentication.js ${bucketName} ${fileContents} ${fileName} ${doesNotExistPrecondition}`
);
assert.match(output, new RegExp(`${fileName} uploaded to ${bucketName}`));
const [exists] = await bucket.file(fileName).exists();
assert.strictEqual(exists, true);
});
it('should upload a file without authentication using signed url strategy', async () => {
const output = execSync(
`node uploadWithoutAuthenticationSignedUrl.js ${bucketName} ${fileContents} ${fileName}`
);
assert.match(output, new RegExp(`${fileName} uploaded to ${bucketName}`));
const [exists] = await bucket.file(fileName).exists();
assert.strictEqual(exists, true);
});
it('should upload a file using a stream', async () => {
const output = execSync(
`node streamFileUpload.js ${bucketName} ${fileName} ${fileContents}`
);
assert.match(output, new RegExp(`${fileName} uploaded to ${bucketName}`));
const [exists] = await bucket.file(fileName).exists();
assert.strictEqual(exists, true);
const response = await bucket.file(fileName).download();
assert.strictEqual(response[0].toString(), fileContents);
});
it('should upload a file with a kms key', async () => {
const [metadata] = await bucket.file(fileName).getMetadata();
const output = execSync(
`node uploadFileWithKmsKey.js ${bucketName} ${filePath} ${kmsKeyName} ${metadata.generation}`
);
assert.include(
output,
`${filePath} uploaded to ${bucketName} using ${kmsKeyName}`
);
const [exists] = await bucket.file(fileName).exists();
assert.strictEqual(exists, true);
});
it('should upload a local directory', done => {
const output = execSync(
`node uploadDirectory.js ${bucketName} ${folderPath}`
);
const fileList = [];
getFileList(folderPath);
function getFileList(directory) {
const items = fs.readdirSync(directory);
items.forEach(item => {
const fullPath = path.join(directory, item);
const stat = fs.lstatSync(fullPath);
if (stat.isFile()) {
fileList.push(fullPath);
} else {
getFileList(fullPath);
}
});
}
assert.match(
output,
new RegExp(
`${fileList.length} files uploaded to ${bucketName} successfully.`
)
);
Promise.all(
fileList.map(file =>
bucket
.file(
path.relative(path.dirname(folderPath), file).replace(/\\/g, '/')
)
.exists()
)
).then(resps => {
const ctr = resps.reduce((acc, cur) => {
return acc + cur[0];
}, 0);
assert.strictEqual(ctr, fileList.length);
done();
}, assert.ifError);
});
it('should download a file', () => {
const output = execSync(
`node downloadFile.js ${bucketName} ${fileName} ${downloadFilePath}`
);
assert.match(
output,
new RegExp(
`gs://${bucketName}/${fileName} downloaded to ${downloadFilePath}.`
)
);
fs.statSync(downloadFilePath);
});
it('should download a file into memory', () => {
const output = execSync(
`node downloadIntoMemory.js ${bucketName} ${memoryFileName}`
);
assert.match(
output,
new RegExp(
`Contents of gs://${bucketName}/${memoryFileName} are ${fileContents}.`
)
);
});
it('should download a file using a stream', () => {
const output = execSync(
`node streamFileDownload.js ${bucketName} ${fileName} ${downloadFilePath}`
);
assert.match(
output,
new RegExp(
`gs://${bucketName}/${fileName} downloaded to ${downloadFilePath}.`
)
);
fs.statSync(downloadFilePath);
});
it('should download a file using a given byte range', () => {
const output = execSync(
`node downloadByteRange.js ${bucketName} ${fileName} ${startByte} ${endByte} ${downloadFilePath}`
);
assert.match(
output,
new RegExp(
`gs://${bucketName}/${fileName} downloaded to ${downloadFilePath} from byte ${startByte} to byte ${endByte}.`
)
);
fs.statSync(downloadFilePath);
});
it('should move a file', async () => {
const output = execSync(
`node moveFile.js ${bucketName} ${fileName} ${movedFileName} ${doesNotExistPrecondition}`
);
assert.include(
output,
`gs://${bucketName}/${fileName} moved to gs://${bucketName}/${movedFileName}`
);
const [exists] = await bucket.file(movedFileName).exists();
assert.strictEqual(exists, true);
});
it('should atomically move a file', async () => {
const movedFileName = 'test1.txt';
const file = bucket.file(fileName);
await file.save(fileName);
const output = execSync(
`node moveFileAtomic.js ${bucketName} ${fileName} ${movedFileName} ${doesNotExistPrecondition}`
);
assert.include(
output,
`gs://${bucketName}/${fileName} moved to gs://${bucketName}/${movedFileName}`
);
const [[destExists], [sourceExists]] = await Promise.all([
bucket.file(movedFileName).exists(),
bucket.file(fileName).exists(),
]);
assert.strictEqual(destExists, true);
assert.strictEqual(sourceExists, false);
});
it('should copy a file', async () => {
const output = execSync(
`node copyFile.js ${bucketName} ${movedFileName} ${bucketName} ${copiedFileName} ${doesNotExistPrecondition}`
);
assert.include(
output,
`gs://${bucketName}/${movedFileName} copied to gs://${bucketName}/${copiedFileName}`
);
const [exists] = await bucket.file(copiedFileName).exists();
assert.strictEqual(exists, true);
});
it('should list files', () => {
const output = execSync(`node listFiles.js ${bucketName}`);
assert.match(output, /Files:/);
assert.match(output, new RegExp(movedFileName));
assert.match(output, new RegExp(copiedFileName));
});
it('should list files by a prefix', () => {
let output = execSync(`node listFilesByPrefix.js ${bucketName} test "/"`);
assert.match(output, /Files:/);
assert.match(output, new RegExp(movedFileName));
assert.match(output, new RegExp(copiedFileName));
output = execSync(`node listFilesByPrefix.js ${bucketName} foo`);
assert.match(output, /Files:/);
assert.notMatch(output, new RegExp(movedFileName));
assert.notMatch(output, new RegExp(copiedFileName));
});
it('should list files with pagination', () => {
const output = execSync(`node listFilesPaginate.js ${bucketName}`);
assert.match(output, /Files:/);
assert.match(output, new RegExp(movedFileName));
assert.match(output, new RegExp(copiedFileName));
});
it('should rename a file', async () => {
const output = execSync(
`node renameFile.js ${bucketName} ${movedFileName} ${renamedFileName}`
);
assert.match(
output,
new RegExp(
`gs://${bucketName}/${movedFileName} renamed to gs://${bucketName}/${renamedFileName}.`
)
);
const [exists] = await bucket.file(renamedFileName).exists();
assert.strictEqual(exists, true);
const [oldFileExists] = await bucket.file(movedFileName).exists();
assert.strictEqual(oldFileExists, false);
});
describe('public data', () => {
let GOOGLE_APPLICATION_CREDENTIALS;
let GOOGLE_CLOUD_PROJECT;
const publicFileName = 'public.txt';
const downloadPublicFilePath = path.join(cwd, 'public-downloaded.txt');
before(async () => {
// CI authentication is done with ADC. Cache it here, restore it `after`
// Incase of sample fails it's restore from here.
await bucket.file(publicFileName).save('public data');
GOOGLE_APPLICATION_CREDENTIALS =
process.env.GOOGLE_APPLICATION_CREDENTIALS;
GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
});
after(async () => {
await promisify(fs.unlink)(downloadPublicFilePath).catch(console.error);
process.env.GOOGLE_APPLICATION_CREDENTIALS =
GOOGLE_APPLICATION_CREDENTIALS;
process.env.GOOGLE_CLOUD_PROJECT = GOOGLE_CLOUD_PROJECT;
await bucket.file(publicFileName).delete();
});
/**
* TODO: Re-enable once the test environment allows public IAM roles.
* Currently disabled to avoid 403 errors when adding 'allUsers' or
* 'allAuthenticatedUsers' permissions.
*/
it.skip('should make a file public', () => {
const output = execSync(
`node makePublic.js ${bucketName} ${publicFileName}`
);
assert.match(
output,
new RegExp(`gs://${bucketName}/${publicFileName} is now public`)
);
});
it('should download public file', () => {
const output = execSync(
`node downloadPublicFile.js ${bucketName} ${publicFileName} ${downloadPublicFilePath}`
);
assert.include(
output,
`Downloaded public file ${publicFileName} from bucket name ${bucketName} to ${downloadPublicFilePath}`
);
fs.statSync(downloadPublicFilePath);
});
});
it('should generate a v2 signed URL for a file', async () => {
const output = await execSync(
`node generateSignedUrl ${bucketName} ${copiedFileName}`
);
assert.match(
output,
new RegExp(`The signed url for ${copiedFileName} is `)
);
});
it('should generate a v4 signed URL and read a file', async () => {
const output = await execSync(
`node generateV4ReadSignedUrl.js ${bucketName} ${copiedFileName}`
);
const expected = /URL:\n([^\s]+)/;
assert.match(output, expected);
const match = output.match(expected);
const res = await fetch(match[1]);
const text = await res.text();
assert.strictEqual(text, fileContent);
});
it('should generate a v4 signed URL and upload a file', async () => {
const output = execSync(
`node generateV4UploadSignedUrl.js ${bucketName} ${signedFileName}`
);
const expected = /URL:\n([^\s]+)/;
assert.match(output, expected);
const match = output.match(expected);
const req = {
method: 'PUT',
headers: {'Content-Type': 'application/octet-stream'},
body: fileContent,
};
await fetch(match[1], req);
await new Promise((resolve, reject) => {
let remoteContent = '';
bucket
.file(signedFileName)
.createReadStream()
.on('response', res => {
assert.strictEqual(
res.headers['content-type'],
'application/octet-stream'
);
})
.on('data', buf => (remoteContent += buf.toString()))
.on('end', () => {
assert.strictEqual(remoteContent, fileContent);
resolve();
})
.on('error', reject);
});
});
it('should generate a v4 signed policy', async () => {
const output = execSync(
`node generateV4SignedPolicy.js ${bucketName} ${signedFileName}`
);
assert.include(
output,
`<form action="https://storage.googleapis.com/${bucketName}/`
);
assert.include(output, `<input name="key" value="${signedFileName}"`);
assert.include(output, '<input name="x-goog-signature"');
assert.include(output, '<input name="x-goog-date"');
assert.include(output, '<input name="x-goog-credential"');
assert.include(
output,
'<input name="x-goog-algorithm" value="GOOG4-RSA-SHA256"'
);
assert.include(output, '<input name="policy"');
assert.include(output, '<input name="x-goog-meta-test" value="data"');
assert.include(output, '<input type="file" name="file"/>');
});
it('should get metadata for a file', () => {
const output = execSync(
`node getMetadata.js ${bucketName} ${copiedFileName}`
);
assert.include(output, `Bucket: ${bucketName}`);
assert.include(output, `Name: ${copiedFileName}`);
});
it('should set metadata for a file', async () => {
const [metadata] = await bucket.file(copiedFileName).getMetadata();
// used in sample
const userMetadata = {
description: 'file description...',
modified: '1900-01-01',
};
const output = execSync(
`node fileSetMetadata.js ${bucketName} ${copiedFileName} ${metadata.metageneration} `
);
assert.match(
output,
new RegExp(`description: '${userMetadata.description}'`)
);
assert.match(output, new RegExp(`modified: '${userMetadata.modified}'`));
});
it('should set storage class for a file', async () => {
const output = execSync(
`node fileChangeStorageClass.js ${bucketName} ${copiedFileName} standard ${doesNotExistPrecondition}`
);
assert.include(output, `${copiedFileName} has been set to standard`);
const [metadata] = await storage
.bucket(bucketName)
.file(copiedFileName)
.getMetadata();
assert.strictEqual(metadata.storageClass, 'STANDARD');
});
it('should combine multiple files into one new file', async () => {
const firstFileName = 'file-one.txt';
const secondFileName = 'file-two.txt';
const destinationFileName = 'file-one-two.txt';
const files = [
{file: bucket.file(firstFileName), contents: '123'},
{file: bucket.file(secondFileName), contents: '456'},
];
await Promise.all(files.map(file => createFileAsync(file)));
const destinationFile = bucket.file(destinationFileName);
const output = execSync(
`node composeFile.js ${bucketName} ${firstFileName} ${secondFileName} ${destinationFileName}`
);
assert.include(
output,
`New composite file ${destinationFileName} was created by combining ${firstFileName} and ${secondFileName}`
);
const [contents] = await destinationFile.download();
assert.strictEqual(
contents.toString(),
files.map(x => x.contents).join('')
);
});
it('should delete a file', async () => {
const [metadata] = await bucket.file(copiedFileName).getMetadata();
const output = execSync(
`node deleteFile.js ${bucketName} ${copiedFileName} ${metadata.generation}`
);
assert.match(
output,
new RegExp(`gs://${bucketName}/${copiedFileName} deleted`)
);
const [exists] = await bucket.file(copiedFileName).exists();
assert.strictEqual(exists, false);
});
describe('file archived generations', () => {
const bucketNameWithVersioning = generateName();
const fileName = 'file-one.txt';
const bucketWithVersioning = storage.bucket(bucketNameWithVersioning);
before(async () => {
const versionedFile = bucketWithVersioning.file(fileName);
const filesToCreate = [
{file: versionedFile, contents: '123'},
{file: versionedFile, contents: '456'},
];
await storage.createBucket(bucketNameWithVersioning, {
versioning: {
enabled: true,
},
});
await Promise.all(filesToCreate.map(file => createFileAsync(file)));
});
after(async () => {
await bucketWithVersioning.deleteFiles({
versions: true,
force: true,
});
await bucketWithVersioning.delete();
});
it('should list file with old versions', async () => {
const output = execSync(
`node listFilesWithOldVersions.js ${bucketNameWithVersioning}`
);
assert.notEqual(output.indexOf(fileName), output.lastIndexOf(fileName));
});
it('should copy file with old versions', async () => {
const destFileName = 'file-two.txt';
const [files] = await bucketWithVersioning.getFiles({versions: true});
const generation = files[0].metadata.generation;
const output = execSync(
`node copyOldVersionOfFile.js ${bucketNameWithVersioning} ${fileName} ${bucketNameWithVersioning} ${destFileName} ${generation}`
);
assert.match(
output,
new RegExp(
`Generation ${generation} of file ${fileName} in bucket ${bucketNameWithVersioning} was copied to ${destFileName} in bucket ${bucketNameWithVersioning}`
)
);
const [exists] = await bucketWithVersioning.file(destFileName).exists();
assert.strictEqual(exists, true);
});
it('should delete a file with customized retry settings', () => {
const output = execSync(
`node configureRetries.js ${bucketName} ${fileName}`
);
assert.match(
output,
new RegExp(`File ${fileName} deleted with a customized retry strategy.`)
);
});
it('should delete file with versions', async () => {
const [files] = await bucketWithVersioning.getFiles({versions: true});
const generation = files[0].metadata.generation;
const output = execSync(
`node deleteOldVersionOfFile.js ${bucketNameWithVersioning} ${fileName} ${generation}`
);
assert.match(
output,
new RegExp(
`Generation ${generation} of file ${fileName} was deleted from ${bucketNameWithVersioning}`
)
);
const [exists] = await bucketWithVersioning
.file(fileName, {
generation,
})
.exists();
assert.strictEqual(exists, false);
});
});
describe('Object Retention', () => {
before(async () => {
await storage.createBucket(objectRetentionBucketName, {
enableObjectRetention: true,
});
});
it('should create a file with unlocked retention and then override it', async () => {
const output = execSync(
`node setObjectRetentionPolicy.js ${objectRetentionBucketName} ${fileName} ${fileContent}`
);
assert.include(output, 'Retention policy for file');
const file = objectRetentionBucket.file(fileName);
const [metadata] = await file.getMetadata();
assert(metadata.retention.retainUntilTime);
assert(metadata.retention.mode.toUpperCase(), 'UNLOCKED');
});
});
describe('Object Soft Delete', () => {
let generation;
before(async () => {
await storage.createBucket(softDeleteBucketName, {
softDeletePolicy: {
retentionDurationSeconds: 604800,
},
});
const file = softDeleteBucket.file(fileName);
await file.save(fileName);
const [metadata] = await softDeleteBucket.file(fileName).getMetadata();
generation = metadata.generation;
await file.delete();
});
after(async () => {
await softDeleteBucket.deleteFiles();
await softDeleteBucket.delete();
});
it('should list soft deleted objects', async () => {
const output = await execSync(
`node listSoftDeletedObjects.js ${softDeleteBucketName}`
);
assert.match(output, /Files:/);
assert.match(output, new RegExp(fileName));
});
it('should list soft deleted object versions', async () => {
const output = await execSync(
`node listSoftDeletedObjectVersions.js ${softDeleteBucketName} ${fileName}`
);
assert.match(output, /Files:/);
assert.match(
output,
new RegExp(`Name: ${fileName}, Generation: ${generation}`)
);
});
it('should restore soft deleted object', async () => {
const output = await execSync(
`node restoreSoftDeletedObject.js ${softDeleteBucketName} ${fileName} ${generation}`
);
assert.include(output, `Soft deleted object ${fileName} was restored`);
const [exists] = await softDeleteBucket.file(fileName).exists();
assert.strictEqual(exists, true);
});
});
});
function generateName() {
return `nodejs-storage-samples-${uuid.v4()}`;
}
function createFileAsync(fileObject) {
return fileObject.file.save(fileObject.contents);
}