Amazon S3 is one of the most widely used cloud storage services, and while the AWS Console is handy for quick tasks, the AWS CLI is much faster for anything repetitive, bulk, or automated. In this tutorial you’ll learn the essential aws s3 commands with real examples you can copy and run.

Prerequisites

  • An AWS account
  • An IAM user or role with S3 permissions (for example, AmazonS3FullAccess for practice, and least privilege in production)
  • A terminal (macOS, Linux, or Windows PowerShell)

Step 1: Install the AWS CLI

macOS:

 
bash
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

Linux:

 
bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Windows: Download and run the MSI installer from the official AWS CLI page, or use winget install Amazon.AWSCLI.

Verify the installation:

 
bash
aws --version

Step 2: Configure Your Credentials

Run:

 
bash
aws configure

You’ll be prompted for:

 
AWS Access Key ID: AKIAXXXXXXXXXXXXXXXX
AWS Secret Access Key: ****************************************
Default region name: us-east-1
Default output format: json

Using named profiles

If you work with multiple accounts, create a profile:

 
bash
aws configure --profile dev
aws s3 ls --profile dev

Tip: For organizations using IAM Identity Center, use aws configure sso instead of long-lived access keys. It’s more secure.

Step 3: Understand the Two S3 Command Sets

The CLI offers two ways to work with S3:

CommandPurpose
aws s3High-level, human-friendly commands (ls, cp, sync, mv, rm)
aws s3apiLow-level commands that map directly to S3 API operations

Start with aws s3, and reach for aws s3api when you need finer control.

Step 4: Create and List Buckets

Create a bucket

 
bash
aws s3 mb s3://my-unique-bucket-name-12345 --region us-east-1

Bucket names must be globally unique, 3 to 63 characters, lowercase, and DNS-compliant.

List all buckets

 
bash
aws s3 ls

Output:

 
2026-11-20 10:15:32 my-unique-bucket-name-12345

List bucket contents

 
bash
aws s3 ls s3://my-unique-bucket-name-12345

List recursively with human-readable sizes and a summary

 
bash
aws s3 ls s3://my-unique-bucket-name-12345 --recursive --human-readable --summarize

Step 5: Upload Files

Upload a single file

 
bash
aws s3 cp report.pdf s3://my-unique-bucket-name-12345/

Upload to a specific “folder” (prefix)

 
bash
aws s3 cp report.pdf s3://my-unique-bucket-name-12345/documents/2026/report.pdf

S3 doesn’t have real folders. The slashes are just part of the object key, though the console displays them as folders.

Upload an entire directory

 
bash
aws s3 cp ./my-folder s3://my-unique-bucket-name-12345/my-folder --recursive

Upload with a storage class

 
bash
aws s3 cp backup.zip s3://my-unique-bucket-name-12345/ --storage-class STANDARD_IA

Common storage classes: STANDARD, STANDARD_IA, ONEZONE_IA, INTELLIGENT_TIERING, GLACIER, GLACIER_IR, DEEP_ARCHIVE.

Upload with server-side encryption

 
bash
aws s3 cp secrets.txt s3://my-unique-bucket-name-12345/ --sse AES256

Upload with metadata and content type

 
bash
aws s3 cp index.html s3://my-unique-bucket-name-12345/ \
  --content-type "text/html" \
  --cache-control "max-age=3600" \
  --metadata project=website,owner=alice

Step 6: Download Files

Download a single file

 
bash
aws s3 cp s3://my-unique-bucket-name-12345/report.pdf ./report.pdf

Download an entire bucket or prefix

 
bash
aws s3 cp s3://my-unique-bucket-name-12345/documents ./documents --recursive

Stream to standard output

 
bash
aws s3 cp s3://my-unique-bucket-name-12345/log.txt - | head -20

Stream from standard input

 
bash
echo "Hello S3" | aws s3 cp - s3://my-unique-bucket-name-12345/hello.txt

Step 7: Sync Directories

aws s3 sync copies only new or changed files, which makes it ideal for backups and static site deployments.

Sync local folder to S3

 
bash
aws s3 sync ./website s3://my-unique-bucket-name-12345/

Sync S3 to a local folder

 
bash
aws s3 sync s3://my-unique-bucket-name-12345/ ./backup

Delete files at the destination that no longer exist at the source

 
bash
aws s3 sync ./website s3://my-unique-bucket-name-12345/ --delete

Warning: --delete permanently removes objects. Always preview first with --dryrun.

Preview changes with a dry run

 
bash
aws s3 sync ./website s3://my-unique-bucket-name-12345/ --delete --dryrun

Sync between two buckets

 
bash
aws s3 sync s3://source-bucket s3://destination-bucket

Step 8: Filter with Include and Exclude

Filters work with cp, mv, rm, and sync. Order matters: filters are applied in sequence, and later filters take precedence.

Upload only .jpg files

 
bash
aws s3 sync ./photos s3://my-unique-bucket-name-12345/photos \
  --exclude "*" --include "*.jpg"

Upload everything except logs and temp files

 
bash
aws s3 sync ./app s3://my-unique-bucket-name-12345/app \
  --exclude "*.log" --exclude "tmp/*"

Step 9: Move and Rename Objects

Move a local file to S3 (deletes the local copy)

 
bash
aws s3 mv report.pdf s3://my-unique-bucket-name-12345/archive/

Rename an object within S3

 
bash
aws s3 mv s3://my-unique-bucket-name-12345/old-name.txt s3://my-unique-bucket-name-12345/new-name.txt

Move everything between prefixes

 
bash
aws s3 mv s3://my-unique-bucket-name-12345/temp/ s3://my-unique-bucket-name-12345/archive/ --recursive

Step 10: Delete Objects and Buckets

Delete one object

 
bash
aws s3 rm s3://my-unique-bucket-name-12345/report.pdf

Delete everything under a prefix

 
bash
aws s3 rm s3://my-unique-bucket-name-12345/temp/ --recursive

Delete only certain files

 
bash
aws s3 rm s3://my-unique-bucket-name-12345/ --recursive \
  --exclude "*" --include "*.tmp"

Delete an empty bucket

 
bash
aws s3 rb s3://my-unique-bucket-name-12345

Delete a bucket and all its contents

 
bash
aws s3 rb s3://my-unique-bucket-name-12345 --force

Note: --force empties the bucket first. On versioned buckets it does not remove old versions or delete markers, so you’ll need aws s3api for those.

Step 11: Share Files with Presigned URLs

Presigned URLs let you share a private object temporarily without changing its permissions.

 
bash
aws s3 presign s3://my-unique-bucket-name-12345/report.pdf --expires-in 3600

This returns a URL valid for one hour (3600 seconds). The default is also 3600 seconds.

Step 12: Control Access with ACLs and Policies

Modern S3 buckets have ACLs disabled by default (Object Ownership set to “Bucket owner enforced”) and Block Public Access turned on. Use bucket policies and IAM for access control instead.

Check Block Public Access settings

 
bash
aws s3api get-public-access-block --bucket my-unique-bucket-name-12345

Apply a bucket policy from a file

 
bash
aws s3api put-bucket-policy --bucket my-unique-bucket-name-12345 --policy file://policy.json

Example policy.json (read-only access for a specific IAM role):

 
json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowReadForRole",
      "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::123456789012:role/MyReadRole" },
      "Action": ["s3:GetObject", "s3:ListBucket"],
      "Resource": [
        "arn:aws:s3:::my-unique-bucket-name-12345",
        "arn:aws:s3:::my-unique-bucket-name-12345/*"
      ]
    }
  ]
}

Step 13: Useful s3api Examples

Enable versioning

 
bash
aws s3api put-bucket-versioning \
  --bucket my-unique-bucket-name-12345 \
  --versioning-configuration Status=Enabled

List object versions

 
bash
aws s3api list-object-versions --bucket my-unique-bucket-name-12345

Get object metadata without downloading

 
bash
aws s3api head-object --bucket my-unique-bucket-name-12345 --key report.pdf

Add a lifecycle rule (move to Glacier after 90 days)

 
bash
aws s3api put-bucket-lifecycle-configuration \
  --bucket my-unique-bucket-name-12345 \
  --lifecycle-configuration file://lifecycle.json

lifecycle.json:

 
json
{
  "Rules": [
    {
      "ID": "ArchiveOldFiles",
      "Status": "Enabled",
      "Filter": { "Prefix": "logs/" },
      "Transitions": [
        { "Days": 90, "StorageClass": "GLACIER" }
      ],
      "Expiration": { "Days": 365 }
    }
  ]
}

Query results with --query (JMESPath)

 
bash
aws s3api list-objects-v2 \
  --bucket my-unique-bucket-name-12345 \
  --query "Contents[?Size > \`1000000\`].[Key, Size]" \
  --output table

Step 14: Host a Static Website

 
bash
aws s3 website s3://my-unique-bucket-name-12345/ \
  --index-document index.html --error-document error.html

aws s3 sync ./site s3://my-unique-bucket-name-12345/

For production sites, put CloudFront in front of a private bucket instead of making the bucket public. It’s more secure and faster.

Performance and Cost Tips

  • Use sync instead of cp --recursive when re-running uploads, since it skips unchanged files.
  • Tune concurrency for large transfers:
 
bash
aws configure set default.s3.max_concurrent_requests 20
aws configure set default.s3.multipart_threshold 64MB
  • Use lifecycle rules to move cold data to cheaper storage classes automatically.
  • Use --dryrun before any destructive command.
  • Add --only-show-errors in scripts to keep logs clean.

Common Errors and Fixes

ErrorLikely CauseFix
AccessDeniedMissing IAM permissions or bucket policyCheck IAM policy and bucket policy
NoSuchBucketTypo in bucket name or wrong regionVerify the name with aws s3 ls
BucketAlreadyExistsName taken globallyChoose a more unique name
InvalidAccessKeyIdWrong or deleted credentialsRerun aws configure
SignatureDoesNotMatchWrong secret key or clock skewCheck credentials and system time
301 PermanentRedirectWrong regionAdd --region for the bucket’s region

Quick Reference Cheat Sheet

 
bash
aws s3 mb s3://bucket                       # create bucket
aws s3 ls                                   # list buckets
aws s3 ls s3://bucket --recursive           # list objects
aws s3 cp file s3://bucket/                 # upload
aws s3 cp s3://bucket/file .                # download
aws s3 sync . s3://bucket                   # sync up
aws s3 mv s3://bucket/a s3://bucket/b       # move/rename
aws s3 rm s3://bucket/file                  # delete object
aws s3 rb s3://bucket --force               # delete bucket
aws s3 presign s3://bucket/file             # temporary URL

Conclusion

The AWS S3 CLI turns tedious console work into quick, scriptable commands. Start with cp, ls, and sync, then add s3api for versioning, lifecycle rules, and policies as your needs grow. Once you’re comfortable, drop these commands into shell scripts or CI/CD pipelines to automate backups, deployments, and data transfers.