Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How do I copy all objects from one prefix to other? I have tried all possible ways to copy all objects in one shot from one prefix to other, but the only way that seems to work is by looping over a list of objects and copying them one by one. This is really inefficient. If I have hundreds of files in a folder, will I have to make 100 calls?

var params = {          Bucket: bucket,          CopySource: bucket+'/'+oldDirName+'/filename.txt',          Key: newDirName+'/filename.txt',  }; s3.copyObject(params, function(err, data) {   if (err) {       callback.apply(this, [{           type: "error",           message: "Error while renaming Directory",           data: err       }]);   } else {       callback.apply(this, [{           type: "success",           message: "Directory renamed successfully",           data: data       }]);   } }); 
like image 961
Yousaf Avatar asked Jun 20 '15 21:06

Yousaf


People also ask

How do I move files from one S3 bucket to another in Java?

You can copy an object from one bucket to another by using the AmazonS3 client's copyObject method. It takes the name of the bucket to copy from, the object to copy, and the destination bucket name. s3. copyObject(from_bucket, object_key, to_bucket, object_key); } catch (AmazonServiceException e) { System.

Can we move S3 bucket from one account to another?

Resolution. You can't transfer Amazon S3 bucket ownership between AWS accounts because the bucket is always owned by the account that created it. Instead, you can copy Amazon S3 objects from one bucket to another so that you give ownership of the copied objects to the destination account.

How do you copy an object between S3 buckets using AWS lambda function?

Add below Bucket Access policy to the IAM Role created in Destination account. Lambda function will assume the Role of Destination IAM Role and copy the S3 object from Source bucket to Destination. In the Lambda console, choose Create a Lambda function. Directly move to configure function.


1 Answers

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!

like image 115
Aditya Manohar Avatar answered Oct 02 '22 07:10

Aditya Manohar