Let’s suppose you are building an app that manages the files that you have on an AWS bucket. You decided to go with Python 3 and use the popular Boto 3 library, which in fact is the library used by AWS CLI.
AWS CLI provides a command to move objects, so you hoped you could use this feature as well. But, to your surprise, you did not find any reference to any method which can do this operation using Boto 3. So, how does AWS CLI move and rename objects using this module?
First of all, you have to remember that S3 buckets do NOT have any “move” or “rename” operation. All you can do is create, copy and delete.
Under the hood, AWS CLI copies the objects to the target folder and then removes the original file. The same applies to the rename operation. So, simple enough, you can do the same in your own implementation using Boto 3.
If you want to move a file — or rename it — with Boto, you have to:
- Copy the object A to a new location within the same bucket. Thus, creating the object B.
- Delete the former object A.
Because there is no “move” o “rename” action in S3, we can simulate those actions by using the above steps. Pretty simple, eh?
So, if you wish to move an object, you can use this as an example (in Python 3):
import boto3s3_resource = boto3.resource(‘s3’)# Copy object A as object B
CopySource=”path/to/your/object_A.txt”)# Delete the former object A
This process works to rename objects as well.
Moving files and grant public read access
You can move — or rename — an object granting public read access through the ACL (Access Control List) of the new object. To do this, you have to pass the ACL to the copy_from method
This way, you will move or rename the object and every user will get access to the object and read it.