0
0
AwsHow-ToBeginner · 4 min read

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 objects
  • mb: Make a new bucket
  • rb: Remove a bucket
  • cp: Copy files to/from S3
  • sync: 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 configure is required).
  • Using incorrect bucket names or missing the s3:// prefix.
  • Trying to copy directories with cp instead of sync.
  • 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

CommandDescriptionExample
aws s3 lsList buckets or objectsaws s3 ls s3://my-bucket/
aws s3 mbCreate a new bucketaws s3 mb s3://my-bucket
aws s3 rbRemove an empty bucketaws s3 rb s3://my-bucket
aws s3 cpCopy files to/from S3aws s3 cp file.txt s3://my-bucket/
aws s3 syncSync directories recursivelyaws 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.