Table of contents
Copy object within a bucket
import boto3
# Set your AWS credentials and region
aws_access_key_id = 'YOUR_ACCESS_KEY'
aws_secret_access_key = 'YOUR_SECRET_KEY'
region_name = 'YOUR_REGION'
# Set your bucket name and object key (path/filename)
bucket_name = 'your-bucket-name'
source_object_key = 'folder1/source_object.txt'
destination_folder_key = 'folder2/destination_object.txt'
# Create an S3 client
s3 = boto3.client(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region_name
)
# Copy the object to the new location
s3.copy_object(
Bucket=bucket_name,
CopySource={
'Bucket': bucket_name,
'Key': source_object_key
},
Key=destination_folder_key
)
# Delete the original object
s3.delete_object(
Bucket=bucket_name,
Key=source_object_key
)
print(f"Object moved from {source_object_key} to {destination_folder_key}")
Reference: ChatGPT