How to copy the object from s3 to s3 using node.js

后端 未结 4 889
小蘑菇
小蘑菇 2021-02-19 13:04

I would like to know how to copy the object from s3 to s3 using node.js With the aws s3 command, It could be executed as follows.

s3 cp --recursive s3://xx/yy  s         


        
4条回答
  •  迷失自我
    2021-02-19 13:34

    If you just want to copy one object copyObject API.

     var params = {
      Bucket: "destinationbucket", 
      CopySource: "/sourcebucket/sourceKeyName", 
      Key: "targetKeyName"
     };
     s3.copyObject(params, function(err, data) {
       if (err) console.log(err, err.stack); // an error occurred
       else     console.log(data);           // successful response
     });
    

    If you want to perform recursively for all objects in a bucket, then

    1. List all the object keys in a bucket using the listObjectsV2 API.

    2. If versioning is enabled in the source-bucket and you want to copy a specific version of the key, Invoke the listObjectVersions API as well and get the Version-Id for each S3 Key.

      NOTE: If versioning is not enabled, then you can ignore STEP-2.

    3. Call copyObject for each S3 Key and the Version-Id obtained in Step-1 and Step-2 respectively. Version-id is optional.

提交回复
热议问题