-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathcopyPart.js
More file actions
876 lines (835 loc) · 37.1 KB
/
copyPart.js
File metadata and controls
876 lines (835 loc) · 37.1 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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
const { promisify } = require('util');
const assert = require('assert');
const crypto = require('crypto');
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 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.code, 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 = promisify(s3.createBucket);
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.putObject({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: content,
}).promise())
.then(res => {
etag = res.ETag;
return s3.headObject({
Bucket: sourceBucketName,
Key: sourceObjName,
}).promise();
}).then(() =>
s3.createMultipartUpload({
Bucket: destBucketName,
Key: destObjName,
}).promise()).then(iniateRes => {
uploadId = iniateRes.UploadId;
}).catch(err => {
process.stdout.write(`Error in outer beforeEach: ${err}\n`);
throw err;
});
});
afterEach(() => bucketUtil.empty(sourceBucketName)
.then(() => bucketUtil.empty(destBucketName))
.then(() => s3.abortMultipartUpload({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
}).promise())
.catch(err => {
if (err.code !== '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', done => {
s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
},
(err, res) => {
checkNoError(err);
assert.strictEqual(res.ETag, etag);
assert(res.LastModified);
done();
});
});
it('should copy a part from a source bucket to a different ' +
'destination bucket and complete the MPU', done => {
s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
},
(err, res) => {
checkNoError(err);
assert.strictEqual(res.ETag, etag);
assert(res.LastModified);
s3.completeMultipartUpload({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: etag, PartNumber: 1 },
],
},
}, (err, res) => {
checkNoError(err);
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
// AWS confirmed final ETag for MPU
assert.strictEqual(res.ETag,
'"db77ebbae9e9f5a244a26b86193ad818-1"');
done();
});
});
});
it('should return InvalidArgument error given invalid range', done => {
s3.putObject({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: Buffer.alloc(oneHundredMBPlus11, 'packing'),
}, err => {
checkNoError(err);
s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
CopySourceRange: 'bad-range-parameter',
},
err => {
checkError(err, 'InvalidArgument');
done();
});
});
});
it('should return EntityTooLarge error if attempt to copy ' +
'object larger than max and do not specify smaller ' +
'range in request', done => {
s3.putObject({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: Buffer.alloc(oneHundredMBPlus11, 'packing'),
}, err => {
checkNoError(err);
s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
},
err => {
checkError(err, 'EntityTooLarge');
done();
});
});
});
it('should return EntityTooLarge error if attempt to copy ' +
'object larger than max and specify too large ' +
'range in request', done => {
s3.putObject({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: Buffer.alloc(oneHundredMBPlus11, 'packing'),
}, err => {
checkNoError(err);
s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
CopySourceRange: `bytes=0-${oneHundredMBPlus11}`,
},
err => {
checkError(err, 'EntityTooLarge');
done();
});
});
});
it('should succeed if attempt to copy ' +
'object larger than max but specify acceptable ' +
'range in request', done => {
s3.putObject({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: Buffer.alloc(oneHundredMBPlus11, 'packing'),
}, err => {
checkNoError(err);
s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
CopySourceRange: 'bytes=0-100',
},
err => {
checkNoError(err);
done();
});
});
});
it('should copy a 0 byte object part from a source bucket to a ' +
'different destination bucket and complete the MPU', done => {
const emptyFileETag = '"d41d8cd98f00b204e9800998ecf8427e"';
s3.putObject({
Bucket: sourceBucketName,
Key: sourceObjName,
Body: '',
}, () => {
s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
},
(err, res) => {
checkNoError(err);
assert.strictEqual(res.ETag, emptyFileETag);
assert(res.LastModified);
s3.completeMultipartUpload({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: emptyFileETag, PartNumber: 1 },
],
},
}, (err, res) => {
checkNoError(err);
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
// AWS confirmed final ETag for MPU
assert.strictEqual(res.ETag,
'"59adb24ef3cdbe0297f05b395827453f-1"');
done();
});
});
});
});
it('should copy a part using a range header from a source bucket ' +
'to a different destination bucket and complete the MPU', done => {
const rangeETag = '"ac1be00f1f162e20d58099eec2ea1c70"';
// AWS confirmed final ETag for MPU
const finalMpuETag = '"bff2a6af3adfd8e107a06de01d487176-1"';
s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
CopySourceRange: 'bytes=0-3',
UploadId: uploadId,
},
(err, res) => {
checkNoError(err);
assert.strictEqual(res.ETag, rangeETag);
assert(res.LastModified);
s3.completeMultipartUpload({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: rangeETag, PartNumber: 1 },
],
},
}, (err, res) => {
checkNoError(err);
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
assert.strictEqual(res.ETag, finalMpuETag);
s3.getObject({
Bucket: destBucketName,
Key: destObjName,
}, (err, res) => {
checkNoError(err);
assert.strictEqual(res.ETag, finalMpuETag);
assert.strictEqual(res.ContentLength, 4);
assert.strictEqual(res.Body.toString(), 'I am');
done();
});
});
});
});
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.createMultipartUpload({
Bucket: sourceBucketName,
Key: sourceMpuKey,
}).promise().then(iniateRes => {
sourceMpuId = iniateRes.UploadId;
}).catch(err => {
process.stdout.write(`Error initiating MPU ' +
'in MPU beforeEach: ${err}\n`);
throw err;
}).then(() => {
const partUploads = [];
for (let i = 1; i < 10; i++) {
const partBuffHere = i % 2 ? partBuff : otherPartBuff;
const partHashHere = i % 2 ? partHash : otherPartHash;
partUploads.push(s3.uploadPart({
Bucket: sourceBucketName,
Key: sourceMpuKey,
PartNumber: i,
UploadId: sourceMpuId,
Body: partBuffHere,
}).promise());
parts.push({
ETag: partHashHere,
PartNumber: i,
});
}
process.stdout.write('about to put parts');
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');
return s3.completeMultipartUpload({
Bucket: sourceBucketName,
Key: sourceMpuKey,
UploadId: sourceMpuId,
MultipartUpload: {
Parts: parts,
},
}).promise();
}).then(() => {
process.stdout.write('finished completing mpu');
}).catch(err => {
process.stdout.write(`Error in MPU beforeEach: ${err}\n`);
throw err;
});
});
afterEach(() => s3.abortMultipartUpload({
Bucket: sourceBucketName,
Key: sourceMpuKey,
UploadId: sourceMpuId,
}).promise().catch(err => {
if (err.code !== 'NoSuchUpload'
&& err.code !== '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', done => {
process.stdout.write('Entered first mpu test');
return s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 1,
UploadId: uploadId,
},
(err, res) => {
checkNoError(err);
assert.strictEqual(res.ETag,
totalMpuObjectHash);
assert(res.LastModified);
done();
});
});
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');
return s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 1,
UploadId: uploadId,
}).promise().then(res => {
assert.strictEqual(res.ETag, totalMpuObjectHash);
assert(res.LastModified);
}).then(() => {
process.stdout.write('Putting second part in MPU test');
return s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 2,
UploadId: uploadId,
}).promise().then(res => {
assert.strictEqual(res.ETag, totalMpuObjectHash);
assert(res.LastModified);
}).then(() => {
process.stdout.write('Completing MPU');
return s3.completeMultipartUpload({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: totalMpuObjectHash, PartNumber: 1 },
{ ETag: totalMpuObjectHash, PartNumber: 2 },
],
},
}).promise();
}).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');
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.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 1,
UploadId: uploadId,
CopySourceRange: 'bytes=5242890-15242880',
}).promise().then(res => {
assert.strictEqual(res.ETag, part1ETag);
assert(res.LastModified);
}).then(() => {
process.stdout.write('Putting second part in MPU test');
return s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 2,
UploadId: uploadId,
CopySourceRange: 'bytes=15242891-30242991',
}).promise().then(res => {
assert.strictEqual(res.ETag, part2ETag);
assert(res.LastModified);
}).then(() => {
process.stdout.write('Completing MPU');
return s3.completeMultipartUpload({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: part1ETag, PartNumber: 1 },
{ ETag: part2ETag, PartNumber: 2 },
],
},
}).promise();
}).then(res => {
assert.strictEqual(res.Bucket, destBucketName);
assert.strictEqual(res.Key, destObjName);
assert.strictEqual(res.ETag, finalCombinedETag);
}).then(() => {
process.stdout.write('Getting new object');
return s3.getObject({
Bucket: destBucketName,
Key: destObjName,
}).promise();
}).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');
return s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceMpuKey}`,
PartNumber: 1,
UploadId: uploadId,
}).promise().then(res => {
assert.strictEqual(res.ETag, totalMpuObjectHash);
assert(res.LastModified);
}).then(() => {
process.stdout.write('Overwriting first part in MPU test');
return s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId }).promise();
}).then(res => {
assert.strictEqual(res.ETag, etag);
assert(res.LastModified);
}).then(() => {
process.stdout.write('Completing MPU');
return s3.completeMultipartUpload({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: etag, PartNumber: 1 },
],
},
}).promise();
}).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');
return s3.getObject({
Bucket: destBucketName,
Key: destObjName,
}).promise();
}).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', () => {
// AWS response etag for this completed MPU
const finalObjETag = '"db77ebbae9e9f5a244a26b86193ad818-1"';
process.stdout.write('Putting first part in MPU test');
return s3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
}).promise().then(res => {
assert.strictEqual(res.ETag, etag);
assert(res.LastModified);
}).then(() => {
process.stdout.write('Overwriting first part in MPU test and completing MPU ' +
'at the same time');
return Promise.all([
s3.uploadPartCopy({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
}).promise().catch(err => {
// in case the CompleteMPU finished
// earlier, we may get a NoSuchKey error,
// so just ignore it and resolve with a
// special value, otherwise re-throw the
// error
if (err && err.code === 'NoSuchKey') {
return Promise.resolve(null);
}
throw err;
}),
s3.completeMultipartUpload({
Bucket: destBucketName,
Key: destObjName,
UploadId: uploadId,
MultipartUpload: {
Parts: [
{ ETag: etag, PartNumber: 1 },
],
},
}).promise(),
]);
}).then(([uploadRes, completeRes]) => {
// if upload succeeded before CompleteMPU finished
if (uploadRes !== null) {
assert.strictEqual(uploadRes.ETag, etag);
assert(uploadRes.LastModified);
}
assert.strictEqual(completeRes.Bucket, destBucketName);
assert.strictEqual(completeRes.Key, destObjName);
assert.strictEqual(completeRes.ETag, finalObjETag);
}).then(() => {
process.stdout.write('Getting object put by MPU with ' +
'overwrite part');
return s3.getObject({
Bucket: destBucketName,
Key: destObjName,
}).promise();
}).then(res => {
assert.strictEqual(res.ETag, finalObjETag);
});
});
});
it('should return an error if no such upload initiated',
done => {
s3.uploadPartCopy({ Bucket: destBucketName, Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: 'madeupuploadid444233232',
},
err => {
checkError(err, 'NoSuchUpload');
done();
});
});
it('should return an error if attempt to copy from nonexistent bucket',
done => {
s3.uploadPartCopy({ Bucket: destBucketName, Key: destObjName,
CopySource: `nobucket453234/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
},
err => {
checkError(err, 'NoSuchBucket');
done();
});
});
it('should return an error if attempt to copy to nonexistent bucket',
done => {
s3.uploadPartCopy({ Bucket: 'nobucket453234', Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
},
err => {
checkError(err, 'NoSuchBucket');
done();
});
});
it('should return an error if attempt to copy nonexistent object',
done => {
s3.uploadPartCopy({ Bucket: destBucketName, Key: destObjName,
CopySource: `${sourceBucketName}/nokey`,
PartNumber: 1,
UploadId: uploadId,
},
err => {
checkError(err, 'NoSuchKey');
done();
});
});
it('should return an error if use invalid part number',
done => {
s3.uploadPartCopy({ Bucket: destBucketName, Key: destObjName,
CopySource: `${sourceBucketName}/nokey`,
PartNumber: 10001,
UploadId: uploadId,
},
err => {
checkError(err, 'InvalidArgument');
done();
});
});
// S3C doesn't support cold storage and restore
const itSkipS3C = process.env.S3_END_TO_END ? it.skip : it;
itSkipS3C('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.uploadPartCopy({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
}, err => {
assert.strictEqual(err.code, 'InvalidObjectState');
assert.strictEqual(err.statusCode, 403);
done();
});
});
});
itSkipS3C('should copy a part of an object when it\'s transitioning to cold', done => {
fakeMetadataTransition(sourceBucketName, sourceObjName, undefined, err => {
assert.ifError(err);
s3.uploadPartCopy({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
}, (err, res) => {
checkNoError(err);
assert.strictEqual(res.ETag, etag);
assert(res.LastModified);
done();
});
});
});
itSkipS3C('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.uploadPartCopy({
Bucket: destBucketName,
Key: destObjName,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: uploadId,
}, (err, res) => {
checkNoError(err);
assert.strictEqual(res.ETag, etag);
assert(res.LastModified);
done();
});
});
});
describe('copying parts by another account', () => {
const otherAccountBucket = 'otheraccountbucket42342342342';
const otherAccountKey = 'key';
let otherAccountUploadId;
beforeEach(() => {
process.stdout.write('In other account before each');
return otherAccountS3.createBucket({ Bucket:
otherAccountBucket }).promise()
.catch(err => {
process.stdout.write('Error creating other account ' +
`bucket: ${err}\n`);
throw err;
}).then(() => {
process.stdout.write('Initiating other account MPU');
return otherAccountS3.createMultipartUpload({
Bucket: otherAccountBucket,
Key: otherAccountKey,
}).promise();
}).then(iniateRes => {
otherAccountUploadId = iniateRes.UploadId;
}).catch(err => {
process.stdout.write('Error in other account ' +
`beforeEach: ${err}\n`);
throw err;
});
});
afterEach(() => otherAccountBucketUtility.empty(otherAccountBucket)
.then(() => otherAccountS3.abortMultipartUpload({
Bucket: otherAccountBucket,
Key: otherAccountKey,
UploadId: otherAccountUploadId,
}).promise())
.catch(err => {
if (err.code !== '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', done => {
otherAccountS3.uploadPartCopy({ Bucket: otherAccountBucket,
Key: otherAccountKey,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: otherAccountUploadId,
},
err => {
checkError(err, 'AccessDenied');
done();
});
});
it('should not allow an account without write persmission on the ' +
'destination bucket to upload part copy the object', done => {
otherAccountS3.putObject({ Bucket: otherAccountBucket,
Key: otherAccountKey, Body: '' }, () => {
otherAccountS3.uploadPartCopy({ Bucket: destBucketName,
Key: destObjName,
CopySource: `${otherAccountBucket}/${otherAccountKey}`,
PartNumber: 1,
UploadId: uploadId,
},
err => {
checkError(err, 'AccessDenied');
done();
});
});
});
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', done => {
s3.putObjectAcl({ Bucket: sourceBucketName,
Key: sourceObjName, ACL: 'public-read' }, () => {
otherAccountS3.uploadPartCopy({ Bucket: otherAccountBucket,
Key: otherAccountKey,
CopySource: `${sourceBucketName}/${sourceObjName}`,
PartNumber: 1,
UploadId: otherAccountUploadId,
},
err => {
checkNoError(err);
done();
});
});
});
});
});
});