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,
AmazonS3FullAccessfor practice, and least privilege in production) - A terminal (macOS, Linux, or Windows PowerShell)
Step 1: Install the AWS CLI
macOS:
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /Linux:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/installWindows: Download and run the MSI installer from the official AWS CLI page, or use winget install Amazon.AWSCLI.
Verify the installation:
aws --versionStep 2: Configure Your Credentials
Run:
aws configureYou’ll be prompted for:
AWS Access Key ID: AKIAXXXXXXXXXXXXXXXX
AWS Secret Access Key: ****************************************
Default region name: us-east-1
Default output format: jsonUsing named profiles
If you work with multiple accounts, create a profile:
aws configure --profile dev
aws s3 ls --profile devTip: For organizations using IAM Identity Center, use
aws configure ssoinstead 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:
| Command | Purpose |
|---|---|
aws s3 | High-level, human-friendly commands (ls, cp, sync, mv, rm) |
aws s3api | Low-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
aws s3 mb s3://my-unique-bucket-name-12345 --region us-east-1Bucket names must be globally unique, 3 to 63 characters, lowercase, and DNS-compliant.
List all buckets
aws s3 lsOutput:
2026-11-20 10:15:32 my-unique-bucket-name-12345List bucket contents
aws s3 ls s3://my-unique-bucket-name-12345List recursively with human-readable sizes and a summary
aws s3 ls s3://my-unique-bucket-name-12345 --recursive --human-readable --summarizeStep 5: Upload Files
Upload a single file
aws s3 cp report.pdf s3://my-unique-bucket-name-12345/Upload to a specific “folder” (prefix)
aws s3 cp report.pdf s3://my-unique-bucket-name-12345/documents/2026/report.pdfS3 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
aws s3 cp ./my-folder s3://my-unique-bucket-name-12345/my-folder --recursiveUpload with a storage class
aws s3 cp backup.zip s3://my-unique-bucket-name-12345/ --storage-class STANDARD_IACommon storage classes: STANDARD, STANDARD_IA, ONEZONE_IA, INTELLIGENT_TIERING, GLACIER, GLACIER_IR, DEEP_ARCHIVE.
Upload with server-side encryption
aws s3 cp secrets.txt s3://my-unique-bucket-name-12345/ --sse AES256Upload with metadata and content type
aws s3 cp index.html s3://my-unique-bucket-name-12345/ \
--content-type "text/html" \
--cache-control "max-age=3600" \
--metadata project=website,owner=aliceStep 6: Download Files
Download a single file
aws s3 cp s3://my-unique-bucket-name-12345/report.pdf ./report.pdfDownload an entire bucket or prefix
aws s3 cp s3://my-unique-bucket-name-12345/documents ./documents --recursiveStream to standard output
aws s3 cp s3://my-unique-bucket-name-12345/log.txt - | head -20Stream from standard input
echo "Hello S3" | aws s3 cp - s3://my-unique-bucket-name-12345/hello.txtStep 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
aws s3 sync ./website s3://my-unique-bucket-name-12345/Sync S3 to a local folder
aws s3 sync s3://my-unique-bucket-name-12345/ ./backupDelete files at the destination that no longer exist at the source
aws s3 sync ./website s3://my-unique-bucket-name-12345/ --deleteWarning:
--deletepermanently removes objects. Always preview first with--dryrun.
Preview changes with a dry run
aws s3 sync ./website s3://my-unique-bucket-name-12345/ --delete --dryrunSync between two buckets
aws s3 sync s3://source-bucket s3://destination-bucketStep 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
aws s3 sync ./photos s3://my-unique-bucket-name-12345/photos \
--exclude "*" --include "*.jpg"Upload everything except logs and temp files
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)
aws s3 mv report.pdf s3://my-unique-bucket-name-12345/archive/Rename an object within S3
aws s3 mv s3://my-unique-bucket-name-12345/old-name.txt s3://my-unique-bucket-name-12345/new-name.txtMove everything between prefixes
aws s3 mv s3://my-unique-bucket-name-12345/temp/ s3://my-unique-bucket-name-12345/archive/ --recursiveStep 10: Delete Objects and Buckets
Delete one object
aws s3 rm s3://my-unique-bucket-name-12345/report.pdfDelete everything under a prefix
aws s3 rm s3://my-unique-bucket-name-12345/temp/ --recursiveDelete only certain files
aws s3 rm s3://my-unique-bucket-name-12345/ --recursive \
--exclude "*" --include "*.tmp"Delete an empty bucket
aws s3 rb s3://my-unique-bucket-name-12345Delete a bucket and all its contents
aws s3 rb s3://my-unique-bucket-name-12345 --forceNote:
--forceempties the bucket first. On versioned buckets it does not remove old versions or delete markers, so you’ll needaws s3apifor those.
Step 11: Share Files with Presigned URLs
Presigned URLs let you share a private object temporarily without changing its permissions.
aws s3 presign s3://my-unique-bucket-name-12345/report.pdf --expires-in 3600This 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
aws s3api get-public-access-block --bucket my-unique-bucket-name-12345Apply a bucket policy from a file
aws s3api put-bucket-policy --bucket my-unique-bucket-name-12345 --policy file://policy.jsonExample policy.json (read-only access for a specific IAM role):
{
"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
aws s3api put-bucket-versioning \
--bucket my-unique-bucket-name-12345 \
--versioning-configuration Status=EnabledList object versions
aws s3api list-object-versions --bucket my-unique-bucket-name-12345Get object metadata without downloading
aws s3api head-object --bucket my-unique-bucket-name-12345 --key report.pdfAdd a lifecycle rule (move to Glacier after 90 days)
aws s3api put-bucket-lifecycle-configuration \
--bucket my-unique-bucket-name-12345 \
--lifecycle-configuration file://lifecycle.jsonlifecycle.json:
{
"Rules": [
{
"ID": "ArchiveOldFiles",
"Status": "Enabled",
"Filter": { "Prefix": "logs/" },
"Transitions": [
{ "Days": 90, "StorageClass": "GLACIER" }
],
"Expiration": { "Days": 365 }
}
]
}Query results with --query (JMESPath)
aws s3api list-objects-v2 \
--bucket my-unique-bucket-name-12345 \
--query "Contents[?Size > \`1000000\`].[Key, Size]" \
--output tableStep 14: Host a Static Website
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
syncinstead ofcp --recursivewhen re-running uploads, since it skips unchanged files. - Tune concurrency for large transfers:
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
--dryrunbefore any destructive command. - Add
--only-show-errorsin scripts to keep logs clean.
Common Errors and Fixes
| Error | Likely Cause | Fix |
|---|---|---|
AccessDenied | Missing IAM permissions or bucket policy | Check IAM policy and bucket policy |
NoSuchBucket | Typo in bucket name or wrong region | Verify the name with aws s3 ls |
BucketAlreadyExists | Name taken globally | Choose a more unique name |
InvalidAccessKeyId | Wrong or deleted credentials | Rerun aws configure |
SignatureDoesNotMatch | Wrong secret key or clock skew | Check credentials and system time |
301 PermanentRedirect | Wrong region | Add --region for the bucket’s region |
Quick Reference Cheat Sheet
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 URLConclusion
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.