I need to move a file in AWS s3 bucket to another location, example:
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()
})
})
}
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.
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
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.