Boto3: copy objects within bucket

Viewed 1501

Is it possible to copy/duplicate objects within one prefix to another prefix in the same s3 bucket?

1 Answers

You can use copy_object() to copy an object in Amazon S3 to another prefix, another bucket and even another Region. The copying takes place entirely within S3, without needing to download/upload the object.

For example, to copy an object in mybucket from folder1/foo.txt to folder2/foo.txt, you could use:

import boto3

s3_client = boto3.client('s3')

response = s3_client.copy_object(
    CopySource='/mybucket/folder1/foo.txt',  # /Bucket-name/path/filename
    Bucket='mybucket',                       # Destination bucket
    Key='folder2/foo.txt'                    # Destination path/filename
)
Related