DEV Community

scott hutchinson
scott hutchinson

Posted on

Python code to copy all objects from one S3 bucket to another

import boto3

s3_resource = boto3.resource('s3')

new_bucket_name = "targetBucketName"
bucket_to_copy = "sourceBucketName"

for key in s3.list_objects(Bucket=bucket_to_copy)['Contents']:
    files = key['Key']
    copy_source = {'Bucket': "bucket_to_copy",'Key': files}
    s3_resource.meta.client.copy(copy_source, new_bucket_name, files)
    print(files)

Discussion (5)

Collapse
ncksecuritydude profile image
NickTheSecurityDude

Its missing:
s3 = boto3.client('s3')
under import boto3

Collapse
vssharma79 profile image
Venkatesh Sharma

I've copied above code and getting error for "s3.list_objects" in for loop, below error :
NameError: name 's3' is not defined

Collapse
kumundzhievmaxim profile image
Maksim Kumundzhiev

Probably the author forget to change:

s3 on s3_resource

E.g.:

import boto3

s3_resource = boto3.resource('s3')

new_bucket_name = "targetBucketName"
bucket_to_copy = "sourceBucketName"

for key in s3_resource.list_objects(Bucket=bucket_to_copy)['Contents']:
    files = key['Key']
    copy_source = {'Bucket': "bucket_to_copy",'Key': files}
    s3_resource.meta.client.copy(copy_source, new_bucket_name, files)
    print(files)
Collapse
dhanashekaran profile image
dhanashekaran

Thanks for the code, it helped me

Collapse
aswanikumardara2 profile image
aswanikumardara2

Cloud you please let me know, which account need to run the lambda, source account or destination account