Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

804
Views
How can I move (not copy) a file in AWS S3 using their sdk?

I need to move a file in AWS s3 bucket to another location, example:

  • From: http://aws.xxxxx/xxxx/locationA/file.png
  • To: http://aws.xxxxx/xxxx/locationB/file.png

I've looked over the documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html, but haven't found any mention of either moving nor updating the file (I'm thinking I could update the file Key path...).

So far, it seems I need to copy the file then remove the old one? Is there a more straightforward way of doing it?

My current code which copies then removes old file:

function moveFileInAws(fromLocation, toLocation, callback) {
  awsSdk.copyObject({
    Bucket: BUCKET_NAME,
    ACL: 'public-read',
    CopySource: fromLocation,
    Key: toLocation
  }, (err, data) => {
    if (err) {
        console.log(err)
        return callback("Couldn't copy files in directory")
    }
    // callback()
    awsSdk.deleteObject({ Key: fromLocation }, (err, data) => {
      if (err) {
        console.log("Couldn't delete files in directory")
        console.log(err)
        return callback("Couldn't delete files in directory")
      }
      callback()
    })
  })
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Based on the answer here: AWS S3 - Move an object to a different folder in which user @Michael-sqlbot comments:

That's because S3, itself, doesn't have an atomic move or rename operation... so there isn't really an alternative

And the docs here: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-s3-objects.html#copy-object (for the Java SDK, but seems to be useful here) which notes:

You can use copyObject with deleteObject to move or rename an object, by first copying the object to a new name (you can use the same bucket as both the source and destination) and then deleting the object from its old location.

It sounds like S3's infrastructure simply doesn't support moving or renaming in a single operation. You must copy, then delete.

over 4 years ago · Santiago Trujillo Report

0

Amazon S3 doesn’t provide an API to move or rename an object from one bucket to another in a single step.

As in your example, you can use copyObject with deleteObject to move or rename an object, by first copying the object to a new name (you can use the same bucket as both the source and destination) and then deleting the object from its old location.

For more information see Performing Operations on Amazon S3 Objects

over 4 years ago · Santiago Trujillo Report

0

I'm not at all familiar w/ the JavaScript SDK that you're using, but using aws cli, there is:

aws s3 mv s3://bucket/folder/file s3://bucket/folder2/file

that seems it would do what you want. Not sure about JavaScript SDK.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!