简体   繁体   中英

Copy S3 bucket contents to another bucket using Python boto3

So for someone looking for a fully functional code that you may be looking for. Copy and run the below snippet and change the bucket names [bucketab]&[bucketbc1] to your source and destination bucket respectively. While you may have come across a lot of solution here in StackOverflow but none seems to be giving correct solutions and after a lot of thinking ,I got my own code working with missing attributes that should have been used with other answers provided else wher

import boto3
s3_resource = boto3.client('s3')
keys=[]
for obj in s3_resource.list_objects_v2(Bucket='bucketab')['Contents']:
    keys.append(obj['Key'])

#Using Boto3 Resource to do the copy
s3_resource = boto3.resource('s3')
for key in keys:
    copy_source = {
        'Bucket': 'bucketab',
        'Key': key
    }
    bucket = s3_resource.Bucket('bucketbc1')
    bucket.copy(copy_source, key)    
    ```
import boto3
s3_resource = boto3.client('s3')
paginator = s3_resource.get_paginator('list_objects_v2')
page_iterator = paginator.paginate(Bucket='bucketab')
keys=[]
for page in page_iterator:
    for i in page['Contents']:
        keys.append(i['Key'])
#Using Boto3 Resource to do the copy
s3_resource = boto3.resource('s3')
for key in keys:
    copy_source = {
        'Bucket': 'bucketab',
        'Key': key
    }
    bucket = s3_resource.Bucket('bucketbc1')
    bucket.copy(copy_source, key)   

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