Copy Files from one S3 Bucket to Another S3 Bucket in Python

In this tutorial, we will show you how to copy files from one S3 bucket to another S3 bucket using the AWS Boto3 SDK in Python.

First, you must install the latest version of Boto3 Python library using the following command:


pip install boto3

Following is the code to copy files from one S3 bucket to another S3 bucket in Python:

Example

import boto3

ACCESS_SECRET = "your aws access secret"
ACCESS_KEY = "your aws access key"

s3_client = boto3.client('s3', region_name='us-east-1', aws_access_key_id=ACCESS_KEY,
                         aws_secret_access_key=ACCESS_SECRET)

source_bucket_name = "bucket-one"
source_key = "folder/example-file.txt"

copy_source = {
    'Bucket': source_bucket_name,
    'Key': source_key
}

destination_bucket_name = "bucket-two"
destination_file_key = "folder/new-copied-example-file.txt"

s3_client.copy(copy_source, destination_bucket_name, destination_file_key)