-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathcopyPart.js
More file actions
841 lines (794 loc) · 38.4 KB
/
copyPart.js
File metadata and controls
841 lines (794 loc) · 38.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
const assert = require('assert');
const crypto = require('crypto');
const { CreateBucketCommand,
PutObjectCommand,
GetObjectCommand,
HeadObjectCommand,
CreateMultipartUploadCommand,
UploadPartCommand,
UploadPartCopyCommand,
CompleteMultipartUploadCommand,
AbortMultipartUploadCommand,
PutObjectAclCommand
} = require('@aws-sdk/client-s3');
const withV4 = require('../support/withV4');
const BucketUtility = require('../../lib/utility/bucket-util');
const { createEncryptedBucketPromise } =
require('../../lib/utility/createEncryptedBucket');
const { fakeMetadataTransition, fakeMetadataArchive } = require('../utils/init');
const { hasColdStorage } = require('../../lib/utility/test-utils');
const sourceBucketName = 'supersourcebucket81033016532';
const sourceObjName = 'supersourceobject';
const destBucketName = 'destinationbucket815502017';
const destObjName = 'copycatobject';
const content = 'I am the best content ever';
const otherAccountBucketUtility = new BucketUtility('lisa', {});
const otherAccountS3 = otherAccountBucketUtility.s3;
// in constants, we set 110 MB as the max part size for testing purposes
const oneHundredMBPlus11 = 110100481;
function checkNoError(err) {
assert.equal(err, null,
`Expected success, got error ${JSON.stringify(err)}`);
}
function checkError(err, code) {
assert.notEqual(err, null, 'Expected failure but got success');
assert.strictEqual(err.name, code);
}
describe('Object Part Copy', () => {
withV4(sigCfg => {
let bucketUtil;
let s3;
let etag;
let uploadId;
beforeEach(() => {
bucketUtil = new BucketUtility('default', sigCfg);
s3 = bucketUtil.s3;
s3.createBucketPromise = params => s3.send(new CreateBucketCommand(params));
if (process.env.ENABLE_KMS_ENCRYPTION === 'true') {
s3.createBucketPromise = createEncryptedBucketPromise;
}
return s3.createBucketPromise({ Bucket: sourceBucketName })
.catch(err => {
process.stdout.write(`Error creating source bucket: ${err}\n`);
throw err;
}).then(() =>
s3.createBucketPromise({ Bucket: destBucketName })
).catch(err => {
process.stdout.write(`Error creating dest bucket: ${err}\n`);
throw err;
})
.then(() => s3.send(new PutObjectCommand({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: content,
})))
.then(res => {
etag = res.ETag;
return s3.send(new HeadObjectCommand({
Bucket: sourceBucketName,
Key: sourceObjName,
}));
}).then(() => s3.send(new CreateMultipartUploadCommand({
Bucket: destBucketName,
Key: destObjName,
})).then(initiateRes => {
uploadId = initiateRes.UploadId;
})).catch(err => {
process.stdout.write(`Error in outer beforeEach: ${err}\n`);
throw err;
});
});
afterEach(() => bucketUtil.empty(sourceBucketName)
.then(() => bucketUtil.empty(destBucketName))
.then(() => s3.send(new AbortMultipartUploadCommand({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
}))).catch(err => {
if (err.name !== 'NoSuchUpload') {
process.stdout.write(`Error in afterEach: ${err}\n`);
throw err;
}
})
.then(() => bucketUtil.deleteMany([sourceBucketName,
destBucketName]))
);
it('should copy a part from a source bucket to a different ' +
'destination bucket', () => s3.send(new UploadPartCopyCommand({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, etag);
assert(res.CopyPartResult.LastModified);
}));
it('should copy a part from a source bucket to a different ' +
'destination bucket and complete the MPU', () => s3.send(new UploadPartCopyCommand({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, etag);
assert(res.CopyPartResult.LastModified);
return s3.send(new CompleteMultipartUploadCommand({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: etag, PartNumber: 1 },
],
},
})).then(res => {
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
// AWS confirmed final ETag for MPU
assert.strictEqual(res.ETag,
'"db77ebbae9e9f5a244a26b86193ad818-1"');
});
}));
it('should return InvalidArgument error given invalid range', () => s3.send(new PutObjectCommand({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: Buffer.alloc(oneHundredMBPlus11, 'packing'),
})).then(() => s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
CopySourceRange: 'bad-range-parameter',
})).catch(err => {
checkError(err, 'InvalidArgument');
})));
it('should return EntityTooLarge error if attempt to copy ' +
'object larger than max and do not specify smaller ' +
'range in request', () => s3.send(new PutObjectCommand({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: Buffer.alloc(oneHundredMBPlus11, 'packing'),
})).then(() => s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
}))).catch(err => {
checkError(err, 'EntityTooLarge');
}));
it('should return EntityTooLarge error if attempt to copy ' +
'object larger than max and specify too large ' +
'range in request', () => s3.send(new PutObjectCommand({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: Buffer.alloc(oneHundredMBPlus11, 'packing'),
})).then(() => s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
CopySourceRange: `bytes=0-${oneHundredMBPlus11}`,
}))).catch(err => {
checkError(err, 'EntityTooLarge');
}));
it('should succeed if attempt to copy ' +
'object larger than max but specify acceptable ' +
'range in request', () => s3.send(new PutObjectCommand({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: Buffer.alloc(oneHundredMBPlus11, 'packing'),
})).then(() => s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
CopySourceRange: 'bytes=0-100',
}))).catch(err => {
checkNoError(err);
}));
it('should copy a 0 byte object part from a source bucket to a ' +
'different destination bucket and complete the MPU', () => {
const emptyFileETag = '"d41d8cd98f00b204e9800998ecf8427e"';
return s3.send(new PutObjectCommand({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: '',
})).then(() => s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, emptyFileETag);
assert(res.CopyPartResult.LastModified);
return s3.send(new CompleteMultipartUploadCommand({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: emptyFileETag, PartNumber: 1 },
],
},
})).then(res => {
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
// AWS confirmed final ETag for MPU
assert.strictEqual(res.ETag,'"59adb24ef3cdbe0297f05b395827453f-1"');
});
}));
});
it('should copy a part using a range header from a source bucket ' +
'to a different destination bucket and complete the MPU', () => {
const rangeETag = '"ac1be00f1f162e20d58099eec2ea1c70"';
// AWS confirmed final ETag for MPU
const finalMpuETag = '"bff2a6af3adfd8e107a06de01d487176-1"';
return s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
CopySourceRange: 'bytes=0-3',
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, rangeETag);
assert(res.CopyPartResult.LastModified);
return s3.send(new CompleteMultipartUploadCommand({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: rangeETag, PartNumber: 1 },
],
},
})).then(res => {
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
assert.strictEqual(res.ETag, finalMpuETag);
return s3.send(new GetObjectCommand({
Bucket: destBucketName,
Key: destObjName,
})).then(async res => {
assert.strictEqual(res.ETag, finalMpuETag);
assert.strictEqual(res.ContentLength, 4);
const body = await res.Body.transformToString();
assert.strictEqual(body, 'I am');
});
});
});
});
describe('When copy source was put by MPU', () => {
let sourceMpuId;
const sourceMpuKey = 'sourceMpuKey';
// total hash for sourceMpuKey when MPU completed
// (confirmed with AWS)
const totalMpuObjectHash =
'"9b0de95bd76728c778b9e25fd7ce2ef7"';
beforeEach(() => {
const parts = [];
const md5HashPart = crypto.createHash('md5');
const partBuff = Buffer.alloc(5242880);
md5HashPart.update(partBuff);
const partHash = md5HashPart.digest('hex');
const otherMd5HashPart = crypto.createHash('md5');
const otherPartBuff = Buffer.alloc(5242880, 1);
otherMd5HashPart.update(otherPartBuff);
const otherPartHash = otherMd5HashPart.digest('hex');
return s3.send(new CreateMultipartUploadCommand({
Bucket: sourceBucketName,
Key: sourceMpuKey,
})).then(initiateRes => {
sourceMpuId = initiateRes.UploadId;
}).catch(err => {
process.stdout.write(`Error initiating MPU in MPU beforeEach: ${err}\n`);
throw err;
}).then(() => {
const partUploads = [];
// Concurrent uploads help trigger flakiness with "TimeoutError:
// socket hang up" due to a keep-alive race: the server closes
// an idle connection just as the client picks it from the pool.
const uploadWithRetry = (params, attempt = 0) =>
s3.send(new UploadPartCommand(params)).catch(err => {
if (attempt < 3) {
process.stdout.write(`Retrying UploadPart ${params.PartNumber} `
+ `(attempt ${attempt + 1}/3): ${err}\n`);
return uploadWithRetry(params, attempt + 1);
}
throw err;
});
for (let i = 1; i < 10; i++) {
const partBuffHere = i % 2 ? partBuff : otherPartBuff;
const partHashHere = i % 2 ? partHash : otherPartHash;
partUploads.push(uploadWithRetry({
Bucket: sourceBucketName,
Key: sourceMpuKey,
PartNumber: i,
UploadId: sourceMpuId,
Body: partBuffHere,
}));
parts.push({
ETag: partHashHere,
PartNumber: i,
});
}
process.stdout.write('about to put parts\n');
return Promise.all(partUploads);
}).catch(err => {
process.stdout.write(`Error putting parts in MPU beforeEach: ${err}\n`);
throw err;
}).then(() => {
process.stdout.write('completing mpu\n');
return s3.send(new CompleteMultipartUploadCommand({
Bucket: sourceBucketName,
Key: sourceMpuKey,
UploadId: sourceMpuId,
MultipartUpload: {
Parts: parts,
},
}));
}).then(() => {
process.stdout.write('finished completing mpu\n');
}).catch(err => {
process.stdout.write(`Error in MPU beforeEach: ${err}\n`);
throw err;
});
});
afterEach(() => s3.send(new AbortMultipartUploadCommand({
Bucket: sourceBucketName,
Key: sourceMpuKey,
UploadId: sourceMpuId,
})).catch(err => {
if (err.name !== 'NoSuchUpload'
&& err.name !== 'NoSuchBucket') {
process.stdout.write(`Error in afterEach: ${err}\n`);
throw err;
}
}));
it('should copy a part from a source bucket to a different ' +
'destination bucket', () => {
process.stdout.write('Entered first mpu test\n');
return s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 1,
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag,
totalMpuObjectHash);
assert(res.CopyPartResult.LastModified);
});
});
it('should copy two parts from a source bucket to a different ' +
'destination bucket and complete the MPU', () => {
process.stdout.write('Putting first part in MPU test\n');
return s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 1,
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, totalMpuObjectHash);
assert(res.CopyPartResult.LastModified);
}).then(() => {
process.stdout.write('Putting second part in MPU test\n');
return s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 2,
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, totalMpuObjectHash);
assert(res.CopyPartResult.LastModified);
}).then(() => {
process.stdout.write('Completing MPU\n');
return s3.send(new CompleteMultipartUploadCommand({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: totalMpuObjectHash, PartNumber: 1 },
{ ETag: totalMpuObjectHash, PartNumber: 2 },
],
},
})).then(res => {
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
// combined ETag returned by AWS (combination of part ETags
// with number of parts at the end)
assert.strictEqual(res.ETag,
'"5bba96810ff449d94aa8f5c5a859b0cb-2"');
}).catch(err => {
checkNoError(err);
});
});
});
});
it('should copy two parts with range headers from a source ' +
'bucket to a different destination bucket and ' +
'complete the MPU', () => {
process.stdout.write('Putting first part in MPU range test\n');
const part1ETag = '"b1e0d096c8f0670c5367d131e392b84a"';
const part2ETag = '"a2468d5c0ec2d4d5fc13b73beb63080a"';
// combined ETag returned by AWS (combination of part ETags
// with number of parts at the end)
const finalCombinedETag =
'"e08ede4e8b942e18537cb2289f613ae3-2"';
return s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 1,
UploadId: uploadId,
CopySourceRange: 'bytes=5242890-15242880',
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, part1ETag);
assert(res.CopyPartResult.LastModified);
}).then(() => {
process.stdout.write('Putting second part in MPU test\n');
return s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 2,
UploadId: uploadId,
CopySourceRange: 'bytes=15242891-30242991',
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, part2ETag);
assert(res.CopyPartResult.LastModified);
}).then(() => {
process.stdout.write('Completing MPU\n');
return s3.send(new CompleteMultipartUploadCommand({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: part1ETag, PartNumber: 1 },
{ ETag: part2ETag, PartNumber: 2 },
],
},
})).then(res => {
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
assert.strictEqual(res.ETag, finalCombinedETag);
}).then(() => {
process.stdout.write('Getting new object\n');
return s3.send(new GetObjectCommand({
Bucket: destBucketName,
Key: destObjName,
})).then(res => {
assert.strictEqual(res.ContentLength, 25000092);
assert.strictEqual(res.ETag, finalCombinedETag);
})
.catch(err => {
checkNoError(err);
});
});
});
});
});
it('should overwrite an existing part by copying a part', () => {
// AWS response etag for this completed MPU
const finalObjETag = '"db77ebbae9e9f5a244a26b86193ad818-1"';
process.stdout.write('Putting first part in MPU test\n');
return s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 1,
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, totalMpuObjectHash);
assert(res.CopyPartResult.LastModified);
}).then(() => {
process.stdout.write('Overwriting first part in MPU test\n');
return s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId
})
).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, etag);
assert(res.CopyPartResult.LastModified);
process.stdout.write('Completing MPU\n');
return s3.send(new CompleteMultipartUploadCommand({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: etag, PartNumber: 1 },
],
},
})
).then(res => {
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
assert.strictEqual(res.ETag, finalObjETag);
}).then(() => {
process.stdout.write('Getting object put by MPU with ' +
'overwrite part\n');
return s3.send(new GetObjectCommand({
Bucket: destBucketName,
Key: destObjName,
})).then(res => {
assert.strictEqual(res.ETag, finalObjETag);
}).catch(err => {
checkNoError(err);
});
});
});
});
});
it('should not corrupt object if overwriting an existing part by copying a part ' +
'while the MPU is being completed', async () => {
const finalObjETag = '"db77ebbae9e9f5a244a26b86193ad818-1"';
process.stdout.write('Putting first part in MPU test\n');
const randomDestObjName = `copycatobject${Math.floor(Math.random() * 100000)}`;
const initiateRes = await s3.send(new CreateMultipartUploadCommand({
Bucket: destBucketName,
Key: randomDestObjName,
}));
const uploadId = initiateRes.UploadId;
const res = await s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: randomDestObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
}));
assert.strictEqual(res.CopyPartResult.ETag, etag);
assert(res.CopyPartResult.LastModified);
process.stdout.write(
'Overwriting first part in MPU test and completing MPU at the same time\n',
);
const [completeRes, uploadRes] = await Promise.all([
s3.send(new CompleteMultipartUploadCommand({
Bucket: destBucketName,
Key: randomDestObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [{ ETag: etag, PartNumber: 1 }],
},
})).catch(err => {
throw err;
}),
s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: randomDestObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
}))
.catch(err => {
const completeMPUFinishedEarlier =
err.name === 'NoSuchKey' || err.name === 'NoSuchUpload';
if (completeMPUFinishedEarlier) {
return Promise.resolve(null);
}
throw err;
}),
],
);
if (uploadRes !== null) {
assert.strictEqual(uploadRes.CopyPartResult.ETag, etag);
assert(uploadRes.CopyPartResult.LastModified);
}
assert.strictEqual(completeRes.Bucket, destBucketName);
assert.strictEqual(completeRes.Key, randomDestObjName);
assert.strictEqual(completeRes.ETag, finalObjETag);
process.stdout.write(
'Getting object put by MPU with overwrite part\n',
);
const resGet = await s3
.send(new GetObjectCommand({
Bucket: destBucketName,
Key: randomDestObjName,
}));
assert.strictEqual(resGet.ETag, finalObjETag);
});
});
it('should return an error if no such upload initiated',
() => s3.send(new UploadPartCopyCommand({ Bucket: destBucketName, Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: 'madeupuploadid444233232',
})).catch(err => {
checkError(err, 'NoSuchUpload');
}));
it('should return an error if attempt to copy from nonexistent bucket',
() => s3.send(new UploadPartCopyCommand({ Bucket: destBucketName, Key: destObjName,
CopySource: `nobucket453234/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
})).catch(err => {
checkError(err, 'NoSuchBucket');
}));
it('should return an error if attempt to copy to nonexistent bucket',
() => s3.send(new UploadPartCopyCommand({ Bucket: 'nobucket453234', Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
})).catch(err => {
checkError(err, 'NoSuchBucket');
}));
it('should return an error if attempt to copy nonexistent object',
() => s3.send(new UploadPartCopyCommand({ Bucket: destBucketName, Key: destObjName,
CopySource: `${sourceBucketName}/nokey`,
PartNumber: 1,
UploadId: uploadId,
})).catch(err => {
checkError(err, 'NoSuchKey');
}));
it('should return an error if use invalid part number',
() => s3.send(new UploadPartCopyCommand({ Bucket: destBucketName, Key: destObjName,
CopySource: `${sourceBucketName}/nokey`,
PartNumber: 10001,
UploadId: uploadId,
})).catch(err => {
checkError(err, 'InvalidArgument');
}));
const describeColdStorage = hasColdStorage ? describe : describe.skip;
describeColdStorage('with cold storage', () => {
it('should not copy a part of a cold object', done => {
const archive = {
archiveInfo: {
archiveId: '97a71dfe-49c1-4cca-840a-69199e0b0322',
archiveVersion: 5577006791947779
},
};
fakeMetadataArchive(sourceBucketName, sourceObjName, undefined, archive, err => {
assert.ifError(err);
s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
})).then(() => {
done(new Error('Expected failure but got success'));
}).catch(err => {
assert.strictEqual(err.$metadata.httpStatusCode, 403);
done();
});
});
});
it('should copy a part of an object when it\'s transitioning to cold', done => {
fakeMetadataTransition(sourceBucketName, sourceObjName, undefined, err => {
assert.ifError(err);
s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, etag);
assert(res.CopyPartResult.LastModified);
done();
}).catch(err => {
checkNoError(err);
done(err);
});
});
});
it('should copy a part of a restored object', done => {
const archiveCompleted = {
archiveInfo: {},
restoreRequestedAt: new Date(0),
restoreRequestedDays: 5,
restoreCompletedAt: new Date(10),
restoreWillExpireAt: new Date(10 + (5 * 24 * 60 * 60 * 1000)),
};
fakeMetadataArchive(sourceBucketName, sourceObjName, undefined, archiveCompleted, err => {
assert.ifError(err);
s3.send(new UploadPartCopyCommand({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
})).then(res => {
assert.strictEqual(res.CopyPartResult.ETag, etag);
assert(res.CopyPartResult.LastModified);
done();
}).catch(err => {
checkNoError(err);
done(err);
});
});
});
});
describe('copying parts by another account', () => {
const otherAccountBucket = 'otheraccountbucket42342342342';
const otherAccountKey = 'key';
let otherAccountUploadId;
beforeEach(() => {
process.stdout.write('In other account before each\n');
return otherAccountS3.send(new CreateBucketCommand({ Bucket:
otherAccountBucket }))
.catch(err => {
process.stdout.write('Error creating other account ' +
`bucket: ${err}\n`);
throw err;
}).then(() => {
process.stdout.write('Initiating other account MPU\n');
return otherAccountS3.send(new CreateMultipartUploadCommand({
Bucket: otherAccountBucket,
Key: otherAccountKey,
}));
}).then(initiateRes => {
otherAccountUploadId = initiateRes.UploadId;
}).catch(err => {
process.stdout.write('Error in other account ' +
`beforeEach: ${err}\n`);
throw err;
});
});
afterEach(() => otherAccountBucketUtility.empty(otherAccountBucket)
.then(() => otherAccountS3.send(new AbortMultipartUploadCommand({
Bucket: otherAccountBucket,
Key: otherAccountKey,
UploadId: otherAccountUploadId,
})))
.catch(err => {
if (err.name !== 'NoSuchUpload') {
process.stdout.write('Error in other account ' +
`afterEach: ${err}\n`);
throw err;
}
}).then(() => {
otherAccountBucketUtility.deleteOne(otherAccountBucket);
})
);
it('should not allow an account without read persmission on the ' +
'source object to copy the object', () => otherAccountS3.send(new UploadPartCopyCommand(
{ Bucket: otherAccountBucket,
Key: otherAccountKey,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: otherAccountUploadId,
})).catch(
err => {
checkError(err, 'AccessDenied');
}));
it('should not allow an account without write persmission on the ' +
'destination bucket to upload part copy the object', () => {
otherAccountS3.send(new PutObjectCommand({ Bucket: otherAccountBucket,
Key: otherAccountKey, Body: '' })).then(() => otherAccountS3.send(
new UploadPartCopyCommand({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${otherAccountBucket}/${otherAccountKey}`,
PartNumber: 1,
UploadId: uploadId,
})).catch(err => checkError(err, 'AccessDenied')));
});
it('should allow an account with read permission on the ' +
'source object and write permission on the destination ' +
'bucket to upload part copy the object', () => s3.send(new PutObjectAclCommand(
{ Bucket: sourceBucketName,
Key: sourceObjName, ACL: 'public-read' })).then(() => otherAccountS3.send(new UploadPartCopyCommand(
{ Bucket: otherAccountBucket,
Key: otherAccountKey,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: otherAccountUploadId,
})).catch(err => {
checkNoError(err);
}
)));
});
});
});