I have a javascript lambda function that is triggered by file upload, reads the content, validates, converts data records contained therein into an array and puts data into a DynamoDB table.
When everything is OK, the file (S3 object) is moved to another folder in the same bucket – the function copies the original object to another folder and deletes the original.
This action takes some time and generates "unnecessary" data transfer.
Is there a possibility to "modify" the object key, effectively moving it?
Question posted in Amazon Web Sevices
The official Amazon Web Services documentation can be found here.
The official Amazon Web Services documentation can be found here.
2
Answers
Amazon S3 does not have a ‘rename’ or ‘modify’ function. All objects are immutable and the Key cannot be changed — it is the unique identifier of the object.
To ‘move’ an object, the correct process is to
CopyObject()
and thenDeleteObject()
. This creates a new object with the desired Key.Short Answer
Unfortunately you won’t be able to get a performance boost, but you can abstract away the two operations using the S3 High-Level API
Long Answer
AWS S3 has two APIs.
s3
which is a High-Level APIs3api
which is a Low-Level APIWhen using the
s3 mv
api, you may issue a command (using CLI) to move or rename (if moved to the same destination, with a different name) a file. This API is constructed to mimic the feel and outcome of the Linuxmv
command.Syntax:
This is a high level API, that basically does two operations:
s3:ObjectCreated:Copy
s3:ObjectRemoved:DeleteMarkerCreated
, but abstracts it for you.This can be tested by creating an S3 Event Notification and running a Lambda function against those S3 Bucket events and setting up the event received to be logged in CloudWatch Logs. Then run the
mv
command and you’ll see the above two operations are going to be logged permv
command that is successfully executed.Examples:
Moving a local file to S3
Response:
Renaming a file on S3 (move to the same location)
Response:
Moving a file from S3 to S3
Response:
Moving an S3 object to a local file
Response:
Recursively moving S3 objects to a local directory
Response:
Recursively moving local files to S3
Response: