-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathobject.js
805 lines (754 loc) · 23.1 KB
/
object.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
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
'use strict';
const Busboy = require('busboy');
const crypto = require('crypto');
const { once } = require('events');
const { URL } = require('url');
const { DUMMY_ACCOUNT } = require('../models/account');
const S3Error = require('../models/error');
const S3Event = require('../models/event');
const S3Object = require('../models/object');
const { TaggingConfiguration } = require('../models/config');
const { capitalizeHeader, utf8BodyParser, xmlBodyParser } = require('../utils');
const { aws4SignatureBodyParser } = require('../signature/v4');
function triggerS3Event(ctx, eventData) {
ctx.app.emit(
'event',
new S3Event(eventData, {
reqHeaders: ctx.headers,
sourceIp: ctx.ip,
}),
);
}
/*
* Operations on Objects
* The following methods correspond to operations you can perform on Amazon S3 objects.
* https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectOps.html
*/
exports.METHODS = [
'acl',
'legal-hold',
'retention',
'select',
'tagging',
'torrent',
'uploadId',
'uploads',
];
/**
* Delete Multiple Objects
* The Multi-Object Delete request contains a list of up to 1000 keys that you want to delete. In
* the XML, you provide the object key names, and optionally, version IDs if you want to delete a
* specific version of the object from a versioning-enabled bucket.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html}
*/
exports.deleteMultipleObjects = async function deleteMultipleObjects(ctx) {
await xmlBodyParser(ctx);
if (!ctx.request.body.Delete || !ctx.request.body.Delete.Object) {
throw new S3Error(
'MalformedXML',
'The XML you provided was not well-formed or did not validate against ' +
'our published schema.',
);
}
const keys = [].concat(ctx.request.body.Delete.Object).map((o) => o.Key);
await Promise.all(
keys.map(async (key) => {
const objectExists = await ctx.store.existsObject(ctx.params.bucket, key);
if (!objectExists) return;
await ctx.store.deleteObject(ctx.params.bucket, key);
ctx.logger.info(
'Deleted object "%s" in bucket "%s"',
key,
ctx.params.bucket,
);
}),
);
ctx.body = {
DeleteResult: {
'@': { xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/' },
Deleted: keys.map((k) => ({ Key: k })),
},
};
};
/**
* DELETE Object
* The DELETE operation removes the null version (if there is one) of an object and inserts a
* delete marker, which becomes the current version of the object. If there isn't a null version,
* Amazon S3 does not remove any objects.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectDELETE.html}
*/
exports.deleteObject = async function deleteObject(ctx) {
try {
if (await ctx.store.existsObject(ctx.params.bucket, ctx.params.key)) {
await ctx.store.deleteObject(ctx.params.bucket, ctx.params.key);
triggerS3Event(ctx, {
bucket: ctx.params.bucket,
eventType: 'Delete',
S3Item: { key: ctx.params.key },
});
}
ctx.status = 204;
} catch (err) {
ctx.logger.error(
'Error deleting object "%s" from bucket "%s"',
ctx.params.key,
ctx.params.bucket,
err,
);
throw err;
}
};
/**
* GET Object
* This implementation of the GET operation retrieves objects from Amazon S3. To use GET, you must
* have READ access to the object. If you grant READ access to the anonymous user, you can return
* the object without using an authorization header.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html}
*/
exports.getObject = async function getObject(ctx) {
const options = {};
if (/^bytes=/.test(ctx.headers.range)) {
const [start, end] = ctx.headers.range.replace('bytes=', '').split('-');
options.start = Number(start);
if (end) options.end = Number(end);
}
const key = ctx.params.key;
const object = await ctx.store.getObject(ctx.params.bucket, key, options);
if (!object) {
throw new S3Error('NoSuchKey', 'The specified key does not exist.', {
Key: key,
});
}
// Range request was out of range
if (object.range && !object.content) {
throw new S3Error(
'InvalidRange',
'The requested range is not satisfiable',
{
RangeRequested: ctx.get('range'),
ActualObjectSize: object.size,
},
);
}
const noneMatch = ctx.headers['if-none-match'];
if (noneMatch && (noneMatch === object.metadata.etag || noneMatch === '*')) {
ctx.status = 304;
return;
}
const modifiedSince = ctx.headers['if-modified-since'];
if (new Date(modifiedSince) >= object.lastModifiedDate) {
ctx.status = 304;
return;
}
ctx.set('Accept-Ranges', 'bytes');
for (const header of Object.keys(object.metadata)) {
ctx.set(capitalizeHeader(header), object.metadata[header]);
}
if (object.range) {
ctx.set(
'Content-Range',
`bytes ${object.range.start}-${object.range.end}/${object.size}`,
);
ctx.length = object.range.end + 1 - object.range.start;
ctx.status = 206;
} else {
ctx.length = object.size;
ctx.status = 200;
}
if (ctx.method === 'HEAD') {
object.content.destroy();
} else {
ctx.body = object.content;
}
};
/**
* GET Object ACL
* This implementation of the GET operation uses the acl subresource to return the access control
* list (ACL) of an object. To use this operation, you must have READ_ACP access to the object.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGETacl.html}
*/
exports.getObjectAcl = async function getObjectAcl(ctx) {
// NOTE: This is a stub implemenatation
ctx.body = {
AccessControlPolicy: {
'@': { xmlns: 'http://doc.s3.amazonaws.com/2006-03-01/' },
Owner: {
ID: DUMMY_ACCOUNT.id,
DisplayName: DUMMY_ACCOUNT.displayName,
},
AccessControlList: {
Grant: {
Grantee: {
'@': {
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:type': 'CanonicalUser',
},
ID: 'abc',
DisplayName: 'You',
},
},
Permission: 'FULL_CONTROL',
},
},
};
};
/**
* GET Object tagging
* This implementation of the GET operation returns the tags associated with an object. You
* send the GET request against the tagging subresource associated with the object.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGETtagging.html}
*
* @param {Koa.Context} ctx
* @returns {Promise<void>}
*/
exports.getObjectTagging = async function getObjectTagging(ctx) {
const key = ctx.params.key;
const exists = await ctx.store.existsObject(ctx.params.bucket, key);
if (!exists) {
throw new S3Error('NoSuchKey', 'The specified key does not exist.', {
Key: key,
});
}
const config = await ctx.store.getSubresource(
ctx.params.bucket,
key,
'tagging',
);
ctx.type = 'application/xml';
ctx.body = (config || TaggingConfiguration.EMPTY).toXML();
};
/**
* POST Object
* The POST operation adds an object to a specified bucket using HTML forms.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html}
*/
exports.postObject = async function postObject(ctx) {
if (!ctx.is('multipart/form-data')) {
throw new S3Error(
'PreconditionFailed',
'At least one of the pre-conditions you specified did not hold',
{
Condition:
'Bucket POST must be of the enclosure-type multipart/form-data',
},
);
}
try {
let successRedirect;
let successStatus = 204;
let fileCount = 0;
const busboy = new Busboy({ headers: ctx.headers });
const finishedPromise = once(busboy, 'finish');
const object = await Promise.race([
finishedPromise.then(() => null),
new Promise((resolve, reject) => {
let key;
const metadata = {};
const errorHandler = (err) => {
busboy.off('field', fieldHandler);
busboy.off('file', fileHandler);
reject(err);
};
const fieldHandler = (fieldname, value) => {
if (fieldname === 'file') {
// busboy will identify parts as fields if they both lack a filename
// and have a content-type other than application/octet-stream
const file = Buffer.from(value);
fileCount++;
resolve(new S3Object(ctx.params.bucket, key, file, metadata));
}
if (fileCount) {
// ignore everything submitted after the first 'file' field
return;
}
switch (fieldname) {
case 'key':
key = value;
break;
case 'success_action_redirect':
successRedirect = value;
break;
case 'redirect':
// redirect field name is deprecated and is always overridden by
// success_action_redirect if present
if (successRedirect === undefined) {
successRedirect = value;
}
break;
case 'success_action_status':
if (value === '200' || value === '201') {
successStatus = Number(value);
}
break;
case 'AWSAccessKeyId':
case 'acl':
case 'policy':
case 'tagging':
// unimplemented
break;
default:
metadata[fieldname.toLowerCase()] = value;
break;
}
};
const fileHandler = (fieldname, file) => {
if (fieldname !== 'file' || fileCount++) {
return file.resume();
}
resolve(new S3Object(ctx.params.bucket, key, file, metadata));
};
busboy.once('error', errorHandler);
busboy.on('field', fieldHandler);
busboy.on('file', fileHandler);
ctx.req.pipe(busboy);
}),
]);
if (!object) {
throw new S3Error(
'InvalidArgument',
'POST requires exactly one file upload per request.',
{
ArgumentName: 'file',
ArgumentValue: fileCount,
},
);
}
if (object.key === undefined) {
throw new S3Error(
'InvalidArgument',
"Bucket POST must contain a field named 'key'. If it is specified, " +
'please check the order of the fields',
{
ArgumentName: 'key',
ArgumentValue: '',
},
);
}
if (!object.key.length) {
throw new S3Error(
'InvalidArgument',
'User key must have a length greater than 0.',
{
ArgumentName: 'key',
ArgumentValue: '',
},
);
}
const { md5, size } = await ctx.store.putObject(object);
ctx.logger.info(
'Stored object "%s" in bucket "%s" successfully',
object.key,
object.bucket,
);
triggerS3Event(ctx, {
bucket: ctx.params.bucket,
eventType: 'Post',
S3Item: new S3Object(object.bucket, object.key, null, {
'content-length': size,
etag: JSON.stringify(md5),
}),
});
if (!busboy._finished) {
await finishedPromise;
}
if (fileCount > 1) {
ctx.logger.warn(
'A POST request for object %s in bucket %s contained %d files in the ' +
'multipart body. This request would be rejected by real S3 servers.',
object.key,
object.bucket,
fileCount,
);
}
ctx.etag = md5;
if (successRedirect) {
ctx.status = 303;
const location = new URL(successRedirect);
location.searchParams.append('bucket', object.bucket);
location.searchParams.append('key', object.key);
location.searchParams.append('etag', ctx.response.get('ETag'));
ctx.set('Location', location.href);
} else {
ctx.status = successStatus;
const location = new URL(ctx.originalUrl, ctx.origin);
if (!location.pathname.endsWith('/')) {
location.pathname += '/';
}
location.pathname += object.key;
ctx.set('Location', location.href);
}
if (ctx.status === 201) {
ctx.body = {
PostResponse: {
Location: ctx.response.get('Location'),
Bucket: object.bucket,
Key: object.key,
Etag: JSON.stringify(md5),
},
};
} else {
ctx.body = '';
ctx.type = '';
}
} catch (err) {
ctx.logger.error(
'Error uploading object to bucket "%s"',
ctx.params.bucket,
err,
);
throw err;
}
};
/**
* PUT Object
* This implementation of the PUT operation adds an object to a bucket.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html}
*/
exports.putObject = async function putObject(ctx) {
aws4SignatureBodyParser(ctx);
const object = new S3Object(
ctx.params.bucket,
ctx.params.key,
ctx.request.body,
ctx.headers,
);
try {
const { md5, size } = await ctx.store.putObject(object);
ctx.logger.info(
'Stored object "%s" in bucket "%s" successfully',
object.key,
object.bucket,
);
triggerS3Event(ctx, {
bucket: ctx.params.bucket,
eventType: 'Put',
S3Item: new S3Object(object.bucket, object.key, null, {
'content-length': size,
etag: JSON.stringify(md5),
}),
});
ctx.etag = md5;
ctx.body = '';
} catch (err) {
ctx.logger.error(
'Error uploading object "%s" to bucket "%s"',
object.key,
object.bucket,
err,
);
throw err;
}
};
/**
* PUT Object - Copy
* This implementation of the PUT operation creates a copy of an object that is already stored in
* Amazon S3. A PUT copy operation is the same as performing a GET and then a PUT. Adding the
* request header, x-amz-copy-source, makes the PUT operation copy the source object into the
* destination bucket.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html}
*/
exports.putObjectCopy = async function putObjectCopy(ctx) {
let copySource = decodeURI(ctx.headers['x-amz-copy-source']);
copySource = copySource.startsWith('/') ? copySource.slice(1) : copySource;
let [srcBucket, ...srcKey] = copySource.split('/');
srcKey = srcKey.join('/');
const destBucket = ctx.params.bucket;
const destKey = ctx.params.key;
try {
const bucket = await ctx.store.getBucket(srcBucket);
if (!bucket) {
ctx.logger.error('No bucket found for "%s"', srcBucket);
throw new S3Error('NoSuchBucket', 'The specified bucket does not exist', {
BucketName: srcBucket,
});
}
if (!(await ctx.store.existsObject(srcBucket, srcKey))) {
ctx.logger.error(
'Object "%s" in bucket "%s" does not exist',
srcKey,
srcBucket,
);
throw new S3Error('NoSuchKey', 'The specified key does not exist.', {
Key: srcKey,
});
}
const replaceMetadata =
ctx.headers['x-amz-metadata-directive'] === 'REPLACE';
if (srcBucket === destBucket && srcKey === destKey && !replaceMetadata) {
throw new S3Error(
'InvalidRequest',
'This copy request is illegal because it is trying to copy an object ' +
"to itself without changing the object's metadata, storage class, " +
'website redirect location or encryption attributes.',
);
}
const metadata = await ctx.store.copyObject(
srcBucket,
srcKey,
destBucket,
destKey,
replaceMetadata ? ctx.headers : null,
);
ctx.logger.info(
'Copied object "%s" from bucket "%s" into bucket "%s" with key of "%s"',
srcKey,
srcBucket,
destBucket,
destKey,
);
triggerS3Event(ctx, {
bucket: ctx.params.bucket,
eventType: 'Copy',
S3Item: new S3Object(destBucket, destKey, null, metadata),
});
ctx.body = {
CopyObjectResult: {
LastModified: new Date(metadata['last-modified']).toISOString(),
ETag: metadata.etag,
},
};
} catch (err) {
ctx.logger.error(
'Error copying object "%s" from bucket "%s" into bucket "%s" with key of "%s"',
srcKey,
srcBucket,
destBucket,
destKey,
);
throw err;
}
};
/**
* PUT Object tagging
* This implementation of the PUT operation uses the tagging subresource to add a set of tags
* to an existing object.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUTtagging.html}
*
* @param {Koa.Context} ctx
* @returns {Promise<void>}
*/
exports.putObjectTagging = async function putObject(ctx) {
const key = ctx.params.key;
const bucket = ctx.params.bucket;
const exists = await ctx.store.existsObject(bucket, key);
if (!exists) {
throw new S3Error('NoSuchKey', 'The specified key does not exist.', {
Key: key,
});
}
try {
await utf8BodyParser(ctx);
const config = TaggingConfiguration.validate(ctx.request.body);
await ctx.store.putSubresource(bucket, key, config);
ctx.body = '';
} catch (err) {
ctx.logger.error(
'Error tagging object "%s" in bucket "%s"',
key,
bucket,
err,
);
throw err;
}
};
/**
* Complete Multipart Upload
* This operation completes a multipart upload by assembling previously uploaded parts.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadComplete.html}
*/
exports.completeMultipartUpload = async function completeMultipartUpload(ctx) {
await xmlBodyParser(ctx);
if (
!ctx.request.body.CompleteMultipartUpload ||
!ctx.request.body.CompleteMultipartUpload.Part
) {
throw new S3Error(
'MalformedXML',
'The XML you provided was not well-formed or did not validate against ' +
'our published schema.',
);
}
const parts = []
.concat(ctx.request.body.CompleteMultipartUpload.Part)
.map((part) => ({
number: Number(part.PartNumber),
// S3 removes all double quotes when comparing etags
etag: part.ETag.replace(/"/g, ''),
}));
try {
const { md5, size } = await ctx.store.putObjectMultipart(
ctx.params.bucket,
ctx.query.uploadId,
parts,
);
ctx.logger.info(
'Stored object "%s" in bucket "%s" successfully',
ctx.params.key,
ctx.params.bucket,
);
triggerS3Event(ctx, {
bucket: ctx.params.bucket,
eventType: 'Post',
S3Item: new S3Object(ctx.params.bucket, ctx.params.key, null, {
etag: JSON.stringify(md5),
'content-length': size,
}),
});
ctx.body = {
CompleteMultipartUploadResult: {
Location: ctx.href.split('?')[0],
Bucket: ctx.params.bucket,
Key: ctx.params.key,
ETag: JSON.stringify(md5),
},
};
} catch (err) {
ctx.logger.error(
'Error uploading object "%s" to bucket "%s"',
ctx.params.key,
ctx.params.bucket,
err,
);
throw err;
}
};
/**
* Initiate Multipart Upload
* This operation initiates a multipart upload and returns an upload ID. This upload ID is used to
* associate all of the parts in the specific multipart upload. You specify this upload ID in each
* of your subsequent upload part requests. You also include this upload ID in the final request to
* either complete or abort the multipart upload request.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadInitiate.html}
*/
exports.initiateMultipartUpload = async function initiateMultipartUpload(ctx) {
const uploadId = crypto.randomBytes(16).toString('hex');
await ctx.store.initiateUpload(
ctx.params.bucket,
ctx.params.key,
uploadId,
ctx.headers,
);
ctx.body = {
InitiateMultipartUploadResult: {
Bucket: ctx.params.bucket,
Key: ctx.params.key,
UploadId: uploadId,
},
};
};
/**
* Upload Part
* This operation uploads a part in a multipart upload. Part numbers can be any number from 1 to
* 10,000, inclusive. A part number uniquely identifies a part and also defines its position within
* the object being created. If you upload a new part using the same part number that was used with
* a previous part, the previously uploaded part is overwritten. Each part must be at least 5 MB in
* size, except the last part. There is no size limit on the last part of your multipart upload.
* {@link https://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadUploadPart.html}
*/
exports.uploadPart = async function uploadPart(ctx) {
aws4SignatureBodyParser(ctx);
try {
const { md5 } = await ctx.store.putPart(
ctx.params.bucket,
ctx.query.uploadId,
ctx.query.partNumber,
ctx.request.body,
);
ctx.logger.info(
'Stored part %s of %s in bucket "%s" successfully',
ctx.query.partNumber,
ctx.query.uploadId,
ctx.params.bucket,
);
ctx.etag = md5;
ctx.body = '';
} catch (err) {
ctx.logger.error(
'Error uploading part %s of %s to bucket "%s"',
ctx.query.partNumber,
ctx.query.uploadId,
ctx.params.bucket,
err,
);
throw err;
}
};
exports.uploadPartCopy = async function uploadPartCopy(ctx) {
let copySource = decodeURI(ctx.headers['x-amz-copy-source']);
copySource = copySource.startsWith('/') ? copySource.slice(1) : copySource;
let [srcBucket, ...srcKey] = copySource.split('/');
srcKey = srcKey.join('/');
const bucket = await ctx.store.getBucket(srcBucket);
if (!bucket) {
ctx.logger.error('No bucket found for "%s"', srcBucket);
throw new S3Error('NoSuchBucket', 'The specified bucket does not exist', {
BucketName: srcBucket,
});
}
const options = {};
if ('x-amz-copy-source-range' in ctx.headers) {
const match = /^bytes=(\d+)-(\d+)$/.exec(
ctx.headers['x-amz-copy-source-range'],
);
if (!match) {
throw new S3Error(
'InvalidArgument',
'The x-amz-copy-source-range value must be of the form bytes=first-last where first and last are the zero-based offsets of the first and last bytes to copy',
{
ArgumentName: 'x-amz-copy-source-range',
ArgumentValue: ctx.get('x-amz-copy-source-range'),
},
);
}
options.start = Number(match[1]);
options.end = Number(match[2]);
}
const object = await ctx.store.getObject(srcBucket, srcKey, options);
if (!object) {
throw new S3Error('NoSuchKey', 'The specified key does not exist.', {
Key: srcKey,
});
}
// Range request was out of range
if (object.range && (!object.content || object.range.end < options.end)) {
if (object.content) object.content.destroy();
throw new S3Error(
'InvalidArgument',
`Range specified is not valid for source object of size: ${object.size}`,
{
ArgumentName: 'x-amz-copy-source-range',
ArgumentValue: ctx.get('x-amz-copy-source-range'),
},
);
}
try {
const { md5 } = await ctx.store.putPart(
ctx.params.bucket,
ctx.query.uploadId,
ctx.query.partNumber,
object.content,
);
ctx.logger.info(
'Copied part %s of %s in bucket "%s" successfully',
ctx.query.partNumber,
ctx.query.uploadId,
ctx.params.bucket,
);
ctx.etag = md5;
ctx.body = {
CopyPartResult: {
LastModified: new Date().toISOString(),
ETag: JSON.stringify(md5),
},
};
} catch (err) {
ctx.logger.error(
'Error copying part %s of %s to bucket "%s"',
ctx.query.partNumber,
ctx.query.uploadId,
ctx.params.bucket,
err,
);
throw err;
}
};