0

I am looking for all the methods for copying the data from one folder to another on AWS S3 bucket.

Vinod Kumar
  • 29
  • 1
  • 5
  • 1
    Does this answer your question? [how to copy s3 object from one bucket to another using python boto3](https://stackoverflow.com/questions/47468148/how-to-copy-s3-object-from-one-bucket-to-another-using-python-boto3) – aneroid Mar 15 '21 at 09:20
  • A [loop-version of this answer](https://stackoverflow.com/q/47468148/1431750) in the linked question does it, and in a simpler way. – aneroid Mar 15 '21 at 09:21

1 Answers1

2
import boto3

old_bucket_name = 'BUCKET_NAME'
old_prefix = 'FOLDER_NAME'
new_bucket_name = 'BUCKET_NAME'
new_prefix = 'FOLDER_NAME/'
s3 = boto3.resource('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
old_bucket = s3.Bucket(old_bucket_name)
new_bucket = s3.Bucket(new_bucket_name)

for obj in old_bucket.objects.filter(Prefix=old_prefix):
    old_source = { 'Bucket': old_bucket_name, 'Key': obj.key} # replace the prefix
    new_key = obj.key.replace(old_prefix, new_prefix, 1)
    new_obj = new_bucket.Object(new_key)
    new_obj.copy(old_source)
Vinod Kumar
  • 29
  • 1
  • 5
  • By the way, its not a good practice to hardcode your AWS credentails in your source code. Thus, if you can, please reconsider using other ways. – Marcin Mar 15 '21 at 11:30