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

  • Last updated Apr 25, 2024

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

  1. First, you must install the latest version of Boto3 Python library:
  2. pip install boto3
  3. Here's an example code to copy files from one S3 bucket to another S3 bucket in Python:
  4. 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)