devops
S3 Basics
Buckets, objects, storage classes, and essential S3 CLI commands
What is S3?
Amazon Simple Storage Service (S3) is object storage β think of it as an infinitely scalable hard drive in the cloud. You store objects (files) inside buckets (containers).
- Objects can be anything: images, videos, logs, backups, static websites, zip archives
- Maximum object size: 5 TB
- Bucket names are globally unique across all of AWS
Core Concepts
| Term | Meaning |
|---|---|
| Bucket | Top-level container (like a folder root). One per name globally. |
| Object | A file + its metadata stored in a bucket |
| Key | The objectβs βpathβ within a bucket, e.g. images/logo.png |
| Prefix | A key segment used like a folder, e.g. images/ |
| Region | Where the bucket physically lives. Choose close to your users. |
Storage Classes
| Class | Use case | Retrieval | Cost |
|---|---|---|---|
| S3 Standard | Frequently accessed data | Instant | $$ |
| S3 Standard-IA | Infrequently accessed, but needs fast retrieval | Instant | $ |
| S3 One Zone-IA | Infrequently accessed, single AZ (cheaper, less durable) | Instant | $ |
| S3 Glacier Instant | Archive with instant retrieval | Instant | Β’ |
| S3 Glacier Flexible | Archive, retrieved in minutes to hours | Minutes/Hours | Β’ |
| S3 Glacier Deep Archive | Long-term archive (7-10 years), rare access | Hours | Β’Β’ |
Use S3 Intelligent-Tiering if access patterns are unpredictable β it moves objects between tiers automatically.
Creating a Bucket (CLI)
# Create a bucket (replace region and name)aws s3api create-bucket \ --bucket my-devops-notes-bucket \ --region ap-south-1 \ --create-bucket-configuration LocationConstraint=ap-south-1
# List all your bucketsaws s3 lsCommon S3 CLI Commands
# Upload a fileaws s3 cp myfile.txt s3://my-bucket/myfile.txt
# Upload entire folderaws s3 cp ./dist/ s3://my-bucket/dist/ --recursive
# Download a fileaws s3 cp s3://my-bucket/myfile.txt ./myfile.txt
# List objects in a bucketaws s3 ls s3://my-bucket/
# List with human-readable sizesaws s3 ls s3://my-bucket/ --human-readable --summarize
# Delete a fileaws s3 rm s3://my-bucket/myfile.txt
# Sync a folder (only upload changed/new files)aws s3 sync ./dist/ s3://my-bucket/ --deleteBucket Policy Example
Make all objects in a bucket publicly readable (for a static website):
{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::my-bucket/*" } ]}# Apply the policyaws s3api put-bucket-policy \ --bucket my-bucket \ --policy file://bucket-policy.jsonStatic Website Hosting
S3 can serve static HTML/CSS/JS sites (like this blog!) with no server needed:
# Enable static website hostingaws s3 website s3://my-bucket/ \ --index-document index.html \ --error-document 404.html
# Upload site filesaws s3 sync ./dist/ s3://my-bucket/ --deleteThe site will be available at:
http://my-bucket.s3-website.<region>.amazonaws.com
For a custom domain + HTTPS, put CloudFront in front of the S3 bucket.
Versioning
Keep every version of an object β great for backups:
# Enable versioningaws s3api put-bucket-versioning \ --bucket my-bucket \ --versioning-configuration Status=Enabled
# List versions of a specific objectaws s3api list-object-versions \ --bucket my-bucket \ --prefix myfile.txtKey Security Tips
- Block all public access by default β turn it on unless you explicitly need public objects
- Enable versioning on important buckets to protect against accidental deletes
- Enable server-side encryption β SSE-S3 is free and on by default for new buckets
- Use IAM roles, not access keys, when accessing S3 from EC2 or Lambda
- Enable S3 Access Logs to audit who accessed what