How to Use AWS CLI with S3: Commands and Examples
Use the
aws s3 command in AWS CLI to interact with Amazon S3. Common commands include aws s3 ls to list buckets, aws s3 cp to copy files, and aws s3 mb to create buckets.Syntax
The basic syntax for AWS CLI with S3 commands is aws s3 <command> <parameters>. Here are common commands:
ls: List buckets or objectsmb: Make a new bucketrb: Remove a bucketcp: Copy files to/from S3sync: Synchronize directories
Parameters include bucket names (like s3://my-bucket) and file paths.
bash
aws s3 ls aws s3 mb s3://my-bucket aws s3 cp localfile.txt s3://my-bucket/ aws s3 sync localdir/ s3://my-bucket/ --delete
Example
This example shows how to create a bucket, upload a file, list the bucket contents, and then download the file back.
bash
aws s3 mb s3://example-bucket-12345 aws s3 cp hello.txt s3://example-bucket-12345/ aws s3 ls s3://example-bucket-12345/ aws s3 cp s3://example-bucket-12345/hello.txt downloaded-hello.txt
Output
make_bucket: example-bucket-12345
upload: ./hello.txt to s3://example-bucket-12345/hello.txt
2024-06-01 12:00:00 12 hello.txt
download: s3://example-bucket-12345/hello.txt to ./downloaded-hello.txt
Common Pitfalls
Common mistakes include:
- Not configuring AWS credentials before using CLI (
aws configureis required). - Using incorrect bucket names or missing the
s3://prefix. - Trying to copy directories with
cpinstead ofsync. - Not having proper permissions to access the bucket.
bash
aws s3 cp myfolder s3://my-bucket/ # Wrong: cp does not copy folders aws s3 sync myfolder/ s3://my-bucket/ # Right: sync copies folders recursively
Quick Reference
| Command | Description | Example |
|---|---|---|
| aws s3 ls | List buckets or objects | aws s3 ls s3://my-bucket/ |
| aws s3 mb | Create a new bucket | aws s3 mb s3://my-bucket |
| aws s3 rb | Remove an empty bucket | aws s3 rb s3://my-bucket |
| aws s3 cp | Copy files to/from S3 | aws s3 cp file.txt s3://my-bucket/ |
| aws s3 sync | Sync directories recursively | aws s3 sync localdir/ s3://my-bucket/ |
Key Takeaways
Always configure AWS credentials with
aws configure before using AWS CLI.Use
aws s3 ls to list buckets or objects easily.Use
aws s3 cp for single file transfers and aws s3 sync for folders.Bucket names must be prefixed with
s3:// in commands.Check permissions to avoid access errors when working with S3.