AWS

AWS S3 CLI

AWS S3 CLI commands for object storage management.

28 commands
Windows MacOS Linux
#storage #cloud

Bucket Operations

Create a new S3 bucket
aws s3 mb s3://my-bucket
Remove an empty bucket
aws s3 rb s3://my-bucket
Remove bucket and all contents
aws s3 rb s3://my-bucket --force
List all buckets
aws s3 ls
List objects in a bucket
aws s3 ls s3://my-bucket
List all objects recursively
aws s3 ls s3://my-bucket --recursive

Object Operations

Upload a file to S3
aws s3 cp file.txt s3://bucket/
Download a file from S3
aws s3 cp s3://bucket/file.txt .
Rename or move an object
aws s3 mv s3://bucket/old.txt s3://bucket/new.txt
Delete an object
aws s3 rm s3://bucket/file.txt
Delete all objects in a prefix
aws s3 rm s3://bucket/ --recursive

Sync & Copy

Sync local directory to S3
aws s3 sync ./local s3://bucket/path
Sync S3 prefix to local directory
aws s3 sync s3://bucket/path ./local
Sync between two S3 locations
aws s3 sync s3://src s3://dest
Sync and delete removed files
aws s3 sync . s3://bucket --delete
Sync excluding a pattern
aws s3 sync . s3://bucket --exclude "*.log"
Copy all objects to local
aws s3 cp s3://bucket/ . --recursive

Permissions

Set bucket policy
aws s3api put-bucket-policy --bucket b --policy file://policy.json
Get bucket policy
aws s3api get-bucket-policy --bucket my-bucket
Block public access
aws s3api put-public-access-block --bucket b --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
Generate presigned URL
aws s3 presign s3://bucket/file.txt --expires-in 3600

Advanced

Upload to Glacier storage
aws s3 cp large.zip s3://bucket/ --storage-class GLACIER
List object versions
aws s3api list-object-versions --bucket my-bucket
Enable versioning
aws s3api put-bucket-versioning --bucket b --versioning-configuration Status=Enabled
Get object metadata
aws s3api head-object --bucket b --key file.txt

Quick Commands

Upload a file to an S3 bucket
aws s3 cp <file> s3://<bucket>/
Sync files between local and S3 or between buckets
aws s3 sync <source> <destination>
List objects in an S3 bucket
aws s3 ls s3://<bucket>