简体   繁体   中英

copy files from one folder to another on AWS S3 with python

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM