Reputation: 29
I am looking for all the methods for copying the data from one folder to another on AWS S3 bucket.
Upvotes: 0
Views: 3656
Reputation: 29
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)
Upvotes: 2