1- const { promisify } = require ( 'util' ) ;
21const assert = require ( 'assert' ) ;
3- const async = require ( 'async' ) ;
4- const AWS = require ( 'aws-sdk' ) ;
2+ const {
3+ S3Client,
4+ PutObjectCommand,
5+ GetObjectCommand,
6+ CopyObjectCommand,
7+ PutObjectAclCommand,
8+ CreateBucketCommand,
9+ } = require ( '@aws-sdk/client-s3' ) ;
510const withV4 = require ( '../../support/withV4' ) ;
611const BucketUtility = require ( '../../../lib/utility/bucket-util' ) ;
712const constants = require ( '../../../../../../constants' ) ;
@@ -24,7 +29,7 @@ const locMetaHeader = constants.objectLocationConstraintHeader.substring(11);
2429let bucketUtil ;
2530let s3 ;
2631
27- function putSourceObj ( location , isEmptyObj , bucket , cb ) {
32+ async function putSourceObj ( location , isEmptyObj , bucket ) {
2833 const key = `somekey-${ genUniqID ( ) } ` ;
2934 const sourceParams = { Bucket : bucket , Key : key ,
3035 Metadata : {
@@ -38,32 +43,28 @@ function putSourceObj(location, isEmptyObj, bucket, cb) {
3843 sourceParams . Body = body ;
3944 }
4045 process . stdout . write ( 'Putting source object\n' ) ;
41- s3 . putObject ( sourceParams , ( err , result ) => {
42- assert . equal ( err , null , `Error putting source object: ${ err } ` ) ;
43- if ( isEmptyObj ) {
44- assert . strictEqual ( result . ETag , `"${ emptyMD5 } "` ) ;
45- } else {
46- assert . strictEqual ( result . ETag , `"${ correctMD5 } "` ) ;
47- }
48- cb ( key ) ;
49- } ) ;
46+ const result = await s3 . send ( new PutObjectCommand ( sourceParams ) ) ;
47+ if ( isEmptyObj ) {
48+ assert . strictEqual ( result . ETag , `"${ emptyMD5 } "` ) ;
49+ } else {
50+ assert . strictEqual ( result . ETag , `"${ correctMD5 } "` ) ;
51+ }
52+ return key ;
5053}
5154
52- function assertGetObjects ( sourceKey , sourceBucket , sourceLoc , destKey ,
53- destBucket , destLoc , awsKey , mdDirective , isEmptyObj , awsS3 , awsLocation ,
54- callback ) {
55+ async function assertGetObjects ( sourceKey , sourceBucket , sourceLoc , destKey ,
56+ destBucket , destLoc , awsKey , mdDirective , isEmptyObj , awsS3 , awsLocation ) {
5557 const awsBucket =
5658 config . locationConstraints [ awsLocation ] . details . bucketName ;
5759 const sourceGetParams = { Bucket : sourceBucket , Key : sourceKey } ;
5860 const destGetParams = { Bucket : destBucket , Key : destKey } ;
5961 const awsParams = { Bucket : awsBucket , Key : awsKey } ;
60- async . series ( [
61- cb => s3 . getObject ( sourceGetParams , cb ) ,
62- cb => s3 . getObject ( destGetParams , cb ) ,
63- cb => awsS3 . getObject ( awsParams , cb ) ,
64- ] , ( err , results ) => {
65- assert . equal ( err , null , `Error in assertGetObjects: ${ err } ` ) ;
66- const [ sourceRes , destRes , awsRes ] = results ;
62+
63+ const [ sourceRes , destRes , awsRes ] = await Promise . all ( [
64+ s3 . send ( new GetObjectCommand ( sourceGetParams ) ) ,
65+ s3 . send ( new GetObjectCommand ( destGetParams ) ) ,
66+ awsS3 . send ( new GetObjectCommand ( awsParams ) ) ,
67+ ] ) ;
6768 if ( isEmptyObj ) {
6869 assert . strictEqual ( sourceRes . ETag , `"${ emptyMD5 } "` ) ;
6970 assert . strictEqual ( destRes . ETag , `"${ emptyMD5 } "` ) ;
@@ -100,69 +101,63 @@ callback) {
100101 undefined ) ;
101102 }
102103 }
103- assert . strictEqual ( sourceRes . ContentLength , destRes . ContentLength ) ;
104- assert . strictEqual ( sourceRes . Metadata [ locMetaHeader ] , sourceLoc ) ;
105- assert . strictEqual ( destRes . Metadata [ locMetaHeader ] , destLoc ) ;
106- callback ( ) ;
107- } ) ;
104+ assert . strictEqual ( sourceRes . ContentLength , destRes . ContentLength ) ;
105+ assert . strictEqual ( sourceRes . Metadata [ locMetaHeader ] , sourceLoc ) ;
106+ assert . strictEqual ( destRes . Metadata [ locMetaHeader ] , destLoc ) ;
108107}
109108
110109describeSkipIfNotMultiple ( 'MultipleBackend object copy: AWS' ,
111110function testSuite ( ) {
112111 this . timeout ( 250000 ) ;
113112 withV4 ( sigCfg => {
114- beforeEach ( ( ) => {
113+ beforeEach ( async ( ) => {
115114 bucketUtil = new BucketUtility ( 'default' , sigCfg ) ;
116115 s3 = bucketUtil . s3 ;
117116 process . stdout . write ( 'Creating bucket\n' ) ;
118- s3 . createBucketPromise = promisify ( s3 . createBucket ) ;
117+
119118 if ( process . env . ENABLE_KMS_ENCRYPTION === 'true' ) {
120- s3 . createBucketPromise = createEncryptedBucketPromise ;
119+ await createEncryptedBucketPromise ( { Bucket : bucket } ) ;
120+ await createEncryptedBucketPromise ( { Bucket : awsServerSideEncryptionbucket } ) ;
121+ await createEncryptedBucketPromise ( { Bucket : bucketAws } ) ;
122+ } else {
123+ await s3 . send ( new CreateBucketCommand ( {
124+ Bucket : bucket ,
125+ CreateBucketConfiguration : {
126+ LocationConstraint : memLocation ,
127+ } ,
128+ } ) ) ;
129+
130+ await s3 . send ( new CreateBucketCommand ( {
131+ Bucket : awsServerSideEncryptionbucket ,
132+ CreateBucketConfiguration : {
133+ LocationConstraint : awsLocationEncryption ,
134+ } ,
135+ } ) ) ;
136+
137+ await s3 . send ( new CreateBucketCommand ( {
138+ Bucket : bucketAws ,
139+ CreateBucketConfiguration : {
140+ LocationConstraint : awsLocation ,
141+ } ,
142+ } ) ) ;
121143 }
122- return s3 . createBucketPromise ( { Bucket : bucket ,
123- CreateBucketConfiguration : {
124- LocationConstraint : memLocation ,
125- } ,
126- } )
127- . then ( ( ) => s3 . createBucketPromise ( {
128- Bucket : awsServerSideEncryptionbucket ,
129- CreateBucketConfiguration : {
130- LocationConstraint : awsLocationEncryption ,
131- } ,
132- } ) )
133- . then ( ( ) => s3 . createBucketPromise ( { Bucket : bucketAws ,
134- CreateBucketConfiguration : {
135- LocationConstraint : awsLocation ,
136- } ,
137- } ) )
138- . catch ( err => {
139- process . stdout . write ( `Error creating bucket: ${ err } \n` ) ;
140- throw err ;
141- } ) ;
142144 } ) ;
143145
144- afterEach ( ( ) => {
146+ afterEach ( async ( ) => {
145147 process . stdout . write ( 'Emptying bucket\n' ) ;
146- return bucketUtil . empty ( bucket )
147- . then ( ( ) => bucketUtil . empty ( bucketAws ) )
148- . then ( ( ) => bucketUtil . empty ( awsServerSideEncryptionbucket ) )
149- . then ( ( ) => {
150- process . stdout . write ( `Deleting bucket ${ bucket } \n` ) ;
151- return bucketUtil . deleteOne ( bucket ) ;
152- } )
153- . then ( ( ) => {
154- process . stdout . write ( 'Deleting bucket ' +
155- `${ awsServerSideEncryptionbucket } \n` ) ;
156- return bucketUtil . deleteOne ( awsServerSideEncryptionbucket ) ;
157- } )
158- . then ( ( ) => {
159- process . stdout . write ( `Deleting bucket ${ bucketAws } \n` ) ;
160- return bucketUtil . deleteOne ( bucketAws ) ;
161- } )
162- . catch ( err => {
163- process . stdout . write ( `Error in afterEach: ${ err } \n` ) ;
164- throw err ;
165- } ) ;
148+ await bucketUtil . empty ( bucket ) ;
149+ await bucketUtil . empty ( bucketAws ) ;
150+ await bucketUtil . empty ( awsServerSideEncryptionbucket ) ;
151+
152+ process . stdout . write ( `Deleting bucket ${ bucket } \n` ) ;
153+ await bucketUtil . deleteOne ( bucket ) ;
154+
155+ process . stdout . write ( 'Deleting bucket ' +
156+ `${ awsServerSideEncryptionbucket } \n` ) ;
157+ await bucketUtil . deleteOne ( awsServerSideEncryptionbucket ) ;
158+
159+ process . stdout . write ( `Deleting bucket ${ bucketAws } \n` ) ;
160+ await bucketUtil . deleteOne ( bucketAws ) ;
166161 } ) ;
167162
168163 it ( 'should copy an object from mem to AWS relying on ' +
@@ -484,42 +479,36 @@ function testSuite() {
484479
485480 it ( 'should copy an object on AWS to a different AWS location ' +
486481 'with source object READ access' ,
487- done => {
482+ async ( ) => {
488483 const awsConfig2 = getRealAwsConfig ( awsLocation2 ) ;
489- const awsS3Two = new AWS . S3 ( awsConfig2 ) ;
484+ const awsS3Two = new S3Client ( awsConfig2 ) ;
490485 const copyKey = `copyKey-${ genUniqID ( ) } ` ;
491486 const awsBucket =
492487 config . locationConstraints [ awsLocation ] . details . bucketName ;
493- async . waterfall ( [
494- // giving access to the object on the AWS side
495- next => putSourceObj ( awsLocation , false , bucket , key =>
496- next ( null , key ) ) ,
497- ( key , next ) => awsS3 . putObjectAcl (
498- { Bucket : awsBucket , Key : key ,
499- ACL : 'public-read' } , err => next ( err , key ) ) ,
500- ( key , next ) => {
501- const copyParams = {
502- Bucket : bucket ,
503- Key : copyKey ,
504- CopySource : `/${ bucket } /${ key } ` ,
505- MetadataDirective : 'REPLACE' ,
506- Metadata : {
507- 'scal-location-constraint' : awsLocation2 } ,
508- } ;
509- process . stdout . write ( 'Copying object\n' ) ;
510- s3 . copyObject ( copyParams , ( err , result ) => {
511- assert . equal ( err , null , 'Expected success ' +
512- `but got error: ${ err } ` ) ;
513- assert . strictEqual ( result . CopyObjectResult . ETag ,
514- `"${ correctMD5 } "` ) ;
515- next ( err , key ) ;
516- } ) ;
517- } ,
518- ( key , next ) =>
519- assertGetObjects ( key , bucket , awsLocation , copyKey ,
520- bucket , awsLocation2 , copyKey , 'REPLACE' , false ,
521- awsS3Two , awsLocation2 , next ) ,
522- ] , done ) ;
488+
489+ // giving access to the object on the AWS side
490+ const key = await putSourceObj ( awsLocation , false , bucket ) ;
491+ await awsS3 . send ( new PutObjectAclCommand ( {
492+ Bucket : awsBucket ,
493+ Key : key ,
494+ ACL : 'public-read'
495+ } ) ) ;
496+
497+ const copyParams = {
498+ Bucket : bucket ,
499+ Key : copyKey ,
500+ CopySource : `/${ bucket } /${ key } ` ,
501+ MetadataDirective : 'REPLACE' ,
502+ Metadata : {
503+ 'scal-location-constraint' : awsLocation2 } ,
504+ } ;
505+ process . stdout . write ( 'Copying object\n' ) ;
506+ const result = await s3 . send ( new CopyObjectCommand ( copyParams ) ) ;
507+ assert . strictEqual ( result . CopyObjectResult . ETag , `"${ correctMD5 } "` ) ;
508+
509+ await assertGetObjects ( key , bucket , awsLocation , copyKey ,
510+ bucket , awsLocation2 , copyKey , 'REPLACE' , false ,
511+ awsS3Two , awsLocation2 ) ;
523512 } ) ;
524513
525514 itSkipCeph ( 'should return error AccessDenied copying an object on ' +
0 commit comments