How can I make a copy of a dataset from one S3 bucket to another in the same AWS account?

23    Asked by DavidPiper in AWS , Asked on May 7, 2024

I have a large dataset stored in an Amazon S3 bucket named source bucket and I need to make a copy of this dataset into another S3 bucket named destination Bucket within the same AWS account. Explain to me how can I achieve this task programmatically by using AWS SDK or CLI command. 

Answered by Donna Chapman

In the context of AWS, here are the steps given:-

Using AWS CLI

You can use the “aws S3 cp” command to copy objects between S3 buckets. Here is an example given:-

#!/bin/bash

# Define source and destination bucket names

Source_bucket=”source-bucket”
Destination_bucket=”destination-bucket”# Function to copy objects from source bucket to destination bucket
Function copy_objects {

    # List objects in source bucket

    Objects=$(aws s3 ls “s3://${source_bucket}/” –recursive | awk ‘{print $4}’)
    If [ -z “$objects” ]; then
        Echo “No objects found in ${source_bucket}”
        Exit 1
    Fi
    # Loop through each object and copy to destination bucket
    For object in $objects; do
        # Replace spaces with in object names for correct URL encoding
        Encoded_object=$(echo “$object” | sed ‘s/ / /g’)
        # Copy object to destination bucket
        Aws s3 cp “s3://${source_bucket}/${encoded_object}” “s3://${destination_bucket}/${encoded_object}” –quiet
        If [ $? -eq 0 ]; then
            Echo “Copied: ${object}”
        Else
            Echo “Error copying: ${object}”
        Fi
    Done
}

# Call copy_objects function

Copy_objects
This command would copy all the objects from the source bucket to the destination Bucket recursively.
Using AWS SDK
Here is a Python coding snippet given by using Boto 3 to copy objects between S3 buckets:-
Import boto3
# Initialize S3 client
S3_client = boto3.client(‘s3’)
Def copy_objects(source_bucket, destination_bucket):
    Try:
        Response = s3_client.list_objects_v2(Bucket=source_bucket)
        If ‘Contents’ in response:
            For obj in response[‘Contents’]:
                Source_key = obj[‘Key’]
                Destination_key = source_key # You can modify the destination key if needed
                Copy_source = {‘Bucket’: source_bucket, ‘Key’: source_key}
                S3_client.copy_object(CopySource=copy_source, Bucket=destination_bucket, Key=destination_key)
                Print(f”Successfully copied {source_key} to {destination_key}”)
        Else:
            Print(f”No objects found in {source_bucket}”)
    Except Exception as e:
        Print(f”Error copying objects: {e}”)
# Example usage
If __name__ == “__main__”:
    Source_bucket = ‘source-bucket’
    Destination_bucket = ‘destination-bucket’
    Copy_objects(source_bucket, destination_bucket)

Here is the example given in HTML of how you can copy an object from one Amazon S3 bucket to another:-




    <meta</span> charset=”UTF-8”>

    <meta</span> name=”viewport” content=”width=device-width, initial-scale=1.0”>

    Copy S3 Objects



    Copy S3 Objects

   


        Source Bucket:

       

        Destination Bucket:

       

       

   




Here is also the Python script given:-

#!/usr/bin/env python3

Import cgi
Import subprocess
# Get form data
Form = cgi.FieldStorage()
Source_bucket = form.getvalue(‘source_bucket’)
Destination_bucket = form.getvalue(‘destination_bucket’)
# Function to copy S3 objects using AWS CLI
Def copy_s3_objects(source_bucket, destination_bucket):

    Try:

        # Run AWS CLI command to copy objects        Command = f”aws s3 cp s3://{source_bucket}/ s3://{destination_bucket}/ --recursive”
        Process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        Stdout, stderr = process.communicate()
        # Check if the copy was successful
        If process.returncode == 0:
            Return f”Objects copied successfully from {source_bucket} to {destination_bucket}”

        Else:

            Return f”Error copying objects: {stderr.decode(‘utf-8’)}”
    Except Exception as e:
        Return f”Error: {e}”

# Print HTML content

Print(“Content-Type: text/html
”)
Print(“”)
Print(“Copy S3 Objects”)
Print(“”)
Print(“Copy S3 Objects”)
If source_bucket and destination_bucket:
    Result = copy_s3_objects(source_bucket, destination_bucket)
    Print(f”{result}”)
Print(“”)
Print(“”)


Your Answer

Interviews

Parent Categories