Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename objects boto3 S3?

I have about 1000 objects in S3 which named after

abcyearmonthday1
abcyearmonthday2
abcyearmonthday3
...

want to rename them to

abc/year/month/day/1
abc/year/month/day/2
abc/year/month/day/3

how could I do it through boto3. Is there easier way of doing this ?

like image 558
Hello lad Avatar asked Dec 28 '15 11:12

Hello lad


People also ask

Can I rename an s3 folder?

To rename a folder in an S3 bucket, you have to: Open the AWS S3 console and click on your bucket's name. In the Objects tab, optionally use the search input to find your folder. Click on the checkbox next to your folder's name. Click on the Actions button and select Move.

How do I rename a file in s3 Python?

There is no direct method to rename the file in s3. what do you have to do is copy the existing file with new name (Just set the target key) and delete the old one.

What is Boto3 client (' s3 ')?

Boto3 is the name of the Python SDK for AWS. It allows you to directly create, update, and delete AWS resources from your Python scripts.


Video Answer


2 Answers

As explained in Boto3/S3: Renaming an object using copy_object

you can not rename an object in S3 you have to copy object with a new name and then delete the Old object

s3 = boto3.resource('s3')
s3.Object('my_bucket','my_file_new').copy_from(CopySource='my_bucket/my_file_old')
s3.Object('my_bucket','my_file_old').delete()
like image 93
Muhammad Rehman Avatar answered Dec 06 '22 17:12

Muhammad Rehman


There is not direct way to rename S3 object. Below two steps need to perform :

  1. Copy the S3 object at same location with new name.
  2. Then delete the older object.
like image 41
Dinesh Pundkar Avatar answered Dec 06 '22 16:12

Dinesh Pundkar