How to copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js How to copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js node.js node.js

How to copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js


You will need to make one AWS.S3.listObjects() to list your objects with a specific prefix. But you are correct in that you will need to make one call for every object that you want to copy from one bucket/prefix to the same or another bucket/prefix.

You can also use a utility library like async to manage your requests.

var AWS = require('aws-sdk');var async = require('async');var bucketName = 'foo';var oldPrefix = 'abc/';var newPrefix = 'xyz/';var s3 = new AWS.S3({params: {Bucket: bucketName}, region: 'us-west-2'});var done = function(err, data) {  if (err) console.log(err);  else console.log(data);};s3.listObjects({Prefix: oldPrefix}, function(err, data) {  if (data.Contents.length) {    async.each(data.Contents, function(file, cb) {      var params = {        Bucket: bucketName,        CopySource: bucketName + '/' + file.Key,        Key: file.Key.replace(oldPrefix, newPrefix)      };      s3.copyObject(params, function(copyErr, copyData){        if (copyErr) {          console.log(copyErr);        }        else {          console.log('Copied: ', params.Key);          cb();        }      });    }, done);  }});

Hope this helps!


Here is a code snippet that do it in the "async await" way:

const AWS = require('aws-sdk');AWS.config.update({  credentials: new AWS.Credentials(....), // credential parameters});AWS.config.setPromisesDependency(require('bluebird'));const s3 = new AWS.S3();... ...const bucketName = 'bucketName';        // example bucketconst folderToMove = 'folderToMove/';   // old folder nameconst destinationFolder = 'destinationFolder/'; // new destination folder try {    const listObjectsResponse = await s3.listObjects({        Bucket: bucketName,        Prefix: folderToMove,        Delimiter: '/',    }).promise();    const folderContentInfo = listObjectsResponse.Contents;    const folderPrefix = listObjectsResponse.Prefix;    await Promise.all(      folderContentInfo.map(async (fileInfo) => {        await s3.copyObject({          Bucket: bucketName,          CopySource: `${bucketName}/${fileInfo.Key}`,  // old file Key          Key: `${destinationFolder}/${fileInfo.Key.replace(folderPrefix, '')}`, // new file Key        }).promise();            await s3.deleteObject({          Bucket: bucketName,          Key: fileInfo.Key,        }).promise();      })    );} catch (err) {  console.error(err); // error handling}