Replace S3 metadata while copying an object
📑 On this page
Part 36 of AWS from Zero. This lesson keeps the scope to one S3 behavior you can verify from the terminal.
What we are learning
Copying normally preserves metadata. MetadataDirective=REPLACE lets the destination object receive a deliberately new content type or custom metadata set.
Before you run it
aws sts get-caller-identity
REGION="ap-south-1"
BUCKET="replace-with-your-private-demo-bucket"Use a private general purpose bucket that you own. Replace every placeholder before running a write or delete command.
The command
aws s3api copy-object \
--bucket "$BUCKET" \
--copy-source "$BUCKET/source.txt" \
--key revised.txt \
--metadata-directive REPLACE \
--content-type "text/plain; charset=utf-8" \
--metadata "stage=revised,series=aws-from-zero"A successful configuration command may return no output. Treat inspection as a separate required step.
Inspect the result
aws s3api head-object \
--bucket "$BUCKET" \
--key revised.txt \
--query "{Type:ContentType,Metadata:Metadata}"Read the returned fields rather than assuming the write succeeded exactly as intended.
One tiny variation
aws s3api copy-object \
--bucket "$BUCKET" \
--copy-source "$BUCKET/source.txt" \
--key preserved.txt \
--metadata-directive COPYCOPY preserves source metadata. Compare both destination objects with head-object.
Common mistake
With REPLACE, omitted metadata values are not silently retained. Supply the complete metadata and content headers the destination should have.
Cleanup
aws s3 rm "s3://$BUCKET/revised.txt"
aws s3 rm "s3://$BUCKET/preserved.txt"Only the two destination copies are removed; the source remains.
Next, we will learn Choose an S3 storage class during upload.