← All posts
Reading fromAWS from Zero
Volume 2 · Chapter 111. S3 bucket naming rules from the CLI12. S3 create-bucket: us-east-1 vs other regions13. S3 CLI vs S3 API commands14. Upload an S3 object with metadata15. Inspect an S3 object with head-object16. List S3 buckets with s3api17. Copy S3 objects inside one bucket18. Copy S3 objects across buckets19. Use include and exclude rules with S3 sync20. Delete one S3 object safely
ch 2.1 · 0/10 · lesson 7
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.