← All posts
2 min read
Copy S3 objects inside one bucket
#aws#cli#s3#storage
📑 On this page
Part 17 of AWS from Zero. Today we copy one object without downloading it first.
What we are learning
An S3 copy has a source bucket and key, plus a destination bucket and key. Both can be in the same bucket.
Before you run it
REGION="ap-south-1"
BUCKET="aws-zero-copy-inside-12345"
aws s3api create-bucket \
--bucket "$BUCKET" \
--region "$REGION" \
--create-bucket-configuration LocationConstraint="$REGION"
echo "copy me" > source.txt
aws s3 cp source.txt "s3://$BUCKET/inbox/source.txt"The command
aws s3api copy-object \
--bucket "$BUCKET" \
--copy-source "$BUCKET/inbox/source.txt" \
--key "archive/source.txt"S3 performs the copy on the service side. The destination key is a separate object.
Inspect the result
aws s3api list-objects-v2 \
--bucket "$BUCKET" \
--query "Contents[].{Key:Key,Size:Size,ETag:ETag}" \
--output tableYou should see both inbox/source.txt and archive/source.txt.
One tiny variation
Copy the object to a new name:
aws s3api copy-object \
--bucket "$BUCKET" \
--copy-source "$BUCKET/inbox/source.txt" \
--key "archive/renamed.txt"S3 has no standalone rename operation. A rename is normally a copy followed by deletion of the old key.
Common mistake
--copy-source is not an s3:// URI. It uses bucket/key:
bucket-name/path/to/object.txtKeys containing spaces or special characters must be URL encoded in the copy source.
Cleanup
aws s3 rm "s3://$BUCKET/" --recursive
aws s3api delete-bucket --bucket "$BUCKET" --region "$REGION"
rm source.txtNext, we will copy an object between two buckets.