1
1
'use strict'
2
2
3
3
const mime = require ( 'mime' )
4
+ const { CopyObjectCommand } = require ( '@aws-sdk/client-s3' )
4
5
5
6
// TODO(mc, 2019-07-16): optimize cache values
6
7
const getCopyParams = obj => ( {
@@ -12,7 +13,7 @@ const getCopyParams = obj => ({
12
13
/**
13
14
* Copy an object to an S3 bucket
14
15
*
15
- * @param {S3 } s3 - AWS.S3 instance
16
+ * @param {S3Client } s3 - AWS SDK v3 S3Client instance
16
17
* @param {S3Object } sourceObj - Object to copy
17
18
* @param {string } destBucket - Destination bucket
18
19
* @param {string } [destPath] - Destination bucket folder (root if unspecified)
@@ -21,10 +22,10 @@ const getCopyParams = obj => ({
21
22
*
22
23
* @typedef S3Object
23
24
* @property {string } Bucket - Object bucket
24
- * @property {String } Prefix - Deploy folder in bucket
25
+ * @property {string } Prefix - Deploy folder in bucket
25
26
* @property {string } Key - Full key to object
26
27
*/
27
- module . exports = function copyObject (
28
+ module . exports = async function copyObject (
28
29
s3 ,
29
30
sourceObj ,
30
31
destBucket ,
@@ -37,18 +38,28 @@ module.exports = function copyObject(
37
38
const copyParams = getCopyParams ( sourceObj )
38
39
39
40
console . log (
40
- `${ dryrun ? 'DRYRUN: ' : '' } Copy
41
- Source: ${ copySource }
42
- Dest: /${ destBucket } /${ destKey }
43
- Params: ${ JSON . stringify ( copyParams ) } \n`
41
+ `${
42
+ dryrun ? 'DRYRUN: ' : ''
43
+ } Copy\nSource: ${ copySource } \nDest: /${ destBucket } /${ destKey } \nParams: ${ JSON . stringify (
44
+ copyParams
45
+ ) } \n`
44
46
)
45
47
46
48
if ( dryrun ) return Promise . resolve ( )
47
49
48
- const copyObjectParams = Object . assign (
49
- { Bucket : destBucket , Key : destKey , CopySource : copySource } ,
50
- copyParams
51
- )
50
+ const copyObjectParams = {
51
+ Bucket : destBucket ,
52
+ Key : destKey ,
53
+ CopySource : copySource ,
54
+ ...copyParams ,
55
+ }
52
56
53
- return s3 . copyObject ( copyObjectParams ) . promise ( )
57
+ try {
58
+ const command = new CopyObjectCommand ( copyObjectParams )
59
+ await s3 . send ( command )
60
+ console . log ( `Successfully copied to /${ destBucket } /${ destKey } ` )
61
+ } catch ( err ) {
62
+ console . error ( `Error copying object: ${ err . message } ` )
63
+ throw err
64
+ }
54
65
}
0 commit comments