@@ -580,69 +580,86 @@ describe('Object Part Copy', () => {
580580 } ) ;
581581
582582 it ( 'should not corrupt object if overwriting an existing part by copying a part ' +
583- 'while the MPU is being completed' , ( ) => {
584- // AWS response etag for this completed MPU
585- const finalObjETag = '"db77ebbae9e9f5a244a26b86193ad818-1"' ;
586- process . stdout . write ( 'Putting first part in MPU test' ) ;
587- return s3 . uploadPartCopy ( { Bucket : destBucketName ,
588- Key : destObjName ,
589- CopySource : `${ sourceBucketName } /${ sourceObjName } ` ,
590- PartNumber : 1 ,
591- UploadId : uploadId ,
592- } ) . promise ( ) . then ( res => {
583+ 'while the MPU is being completed' , async ( ) => {
584+ const finalObjETag = '"db77ebbae9e9f5a244a26b86193ad818-1"' ;
585+ process . stdout . write ( 'Putting first part in MPU test"' ) ;
586+ const randomDestObjName = `copycatobject${ Math . floor ( Math . random ( ) * 100000 ) } ` ;
587+
588+ const initiateRes = await s3
589+ . createMultipartUpload ( {
590+ Bucket : destBucketName ,
591+ Key : randomDestObjName ,
592+ } )
593+ . promise ( ) ;
594+ const uploadId = initiateRes . UploadId ;
595+
596+ const res = await s3
597+ . uploadPartCopy ( {
598+ Bucket : destBucketName ,
599+ Key : randomDestObjName ,
600+ CopySource : `${ sourceBucketName } /${ sourceObjName } ` ,
601+ PartNumber : 1 ,
602+ UploadId : uploadId ,
603+ } )
604+ . promise ( ) ;
593605 assert . strictEqual ( res . ETag , etag ) ;
594606 assert ( res . LastModified ) ;
595- } ) . then ( ( ) => {
596- process . stdout . write ( 'Overwriting first part in MPU test and completing MPU ' +
597- 'at the same time' ) ;
598- return Promise . all ( [
599- s3 . uploadPartCopy ( {
600- Bucket : destBucketName ,
601- Key : destObjName ,
602- CopySource : `${ sourceBucketName } /${ sourceObjName } ` ,
603- PartNumber : 1 ,
604- UploadId : uploadId ,
605- } ) . promise ( ) . catch ( err => {
606- // in case the CompleteMPU finished
607- // earlier, we may get a NoSuchKey error,
608- // so just ignore it and resolve with a
609- // special value, otherwise re-throw the
610- // error
611- if ( err && err . code === 'NoSuchKey' ) {
612- return Promise . resolve ( null ) ;
613- }
614- throw err ;
607+
608+ process . stdout . write (
609+ 'Overwriting first part in MPU test and completing MPU at the same time' ,
610+ ) ;
611+ const [ completeRes , uploadRes ] = await Promise . all ( [
612+ s3
613+ . completeMultipartUpload ( {
614+ Bucket : destBucketName ,
615+ Key : randomDestObjName ,
616+ UploadId : uploadId ,
617+ MultipartUpload : {
618+ Parts : [ { ETag : etag , PartNumber : 1 } ] ,
619+ } ,
620+ } )
621+ . promise ( )
622+ . catch ( err => {
623+ throw err ;
624+ } ) ,
625+ s3
626+ . uploadPartCopy ( {
627+ Bucket : destBucketName ,
628+ Key : randomDestObjName ,
629+ CopySource : `${ sourceBucketName } /${ sourceObjName } ` ,
630+ PartNumber : 1 ,
631+ UploadId : uploadId ,
632+ } )
633+ . promise ( )
634+ . catch ( err => {
635+ const completeMPUFinishedEarlier =
636+ err && err . code === 'NoSuchKey' ;
637+ if ( completeMPUFinishedEarlier ) {
638+ return Promise . resolve ( null ) ;
639+ }
640+
641+ throw err ;
615642 } ) ,
616- s3 . completeMultipartUpload ( {
617- Bucket : destBucketName ,
618- Key : destObjName ,
619- UploadId : uploadId ,
620- MultipartUpload : {
621- Parts : [
622- { ETag : etag , PartNumber : 1 } ,
623- ] ,
624- } ,
625- } ) . promise ( ) ,
626643 ] ) ;
627- } ) . then ( ( [ uploadRes , completeRes ] ) => {
628- // if upload succeeded before CompleteMPU finished
644+
629645 if ( uploadRes !== null ) {
630- assert . strictEqual ( uploadRes . ETag , etag ) ;
631- assert ( uploadRes . LastModified ) ;
646+ assert . strictEqual ( uploadRes . ETag , etag ) ;
647+ assert ( uploadRes . LastModified ) ;
632648 }
649+
633650 assert . strictEqual ( completeRes . Bucket , destBucketName ) ;
634- assert . strictEqual ( completeRes . Key , destObjName ) ;
651+ assert . strictEqual ( completeRes . Key , randomDestObjName ) ;
635652 assert . strictEqual ( completeRes . ETag , finalObjETag ) ;
636- } ) . then ( ( ) => {
637- process . stdout . write ( 'Getting object put by MPU with ' +
638- 'overwrite part' ) ;
639- return s3 . getObject ( {
653+ process . stdout . write (
654+ 'Getting object put by MPU with ' + 'overwrite part' ,
655+ ) ;
656+ const resGet = await s3
657+ . getObject ( {
640658 Bucket : destBucketName ,
641- Key : destObjName ,
642- } ) . promise ( ) ;
643- } ) . then ( res => {
644- assert . strictEqual ( res . ETag , finalObjETag ) ;
645- } ) ;
659+ Key : randomDestObjName ,
660+ } )
661+ . promise ( ) ;
662+ assert . strictEqual ( resGet . ETag , finalObjETag ) ;
646663 } ) ;
647664 } ) ;
648665
0 commit comments