Common aws s3 cp examples
Handy aws s3 cp command examples for copying single files and directories to and from Amazon S3, including recursive copies, content type and cache headers, and streaming with stdin and stdout.
The aws s3 cp command copies individual objects to and from Amazon S3. Where aws s3 sync mirrors whole directories and transfers only what has changed, cp is the tool for copying a specific file, a stream, or a one-off recursive copy.
Copying a file to a bucket
aws s3 cp report.pdf s3://my-bucket/
Copying a file back down
aws s3 cp s3://my-bucket/report.pdf .
Copying a whole directory
Unlike sync, cp needs --recursive to work on a directory, and it copies every file regardless of whether it changed:
aws s3 cp ./exports s3://my-bucket/exports --recursive
Filtering with include and exclude
aws s3 cp ./exports s3://my-bucket/exports --recursive --exclude "*" --include "*.csv"
Setting content type and cache headers
aws s3 cp index.html s3://my-bucket/ --content-type "text/html" --cache-control "no-cache"
Streaming to and from standard input and output
A single dash tells cp to use stdin or stdout, which is useful in scripts and pipelines:
aws s3 cp - s3://my-bucket/log.txt # write stdin to an object
aws s3 cp s3://my-bucket/log.txt - # print an object to stdout
For example, you can compress a database dump and stream it straight to S3 without a temporary file on disk:
mysqldump mydb | gzip | aws s3 cp - s3://my-bucket/backups/mydb.sql.gz
When you are keeping a whole directory in step with a bucket rather than copying one thing, reach for aws s3 sync instead.