Lesson 3 of 4

S3: Simple Storage Service

Learn to store and retrieve any amount of data with Amazon S3, including buckets, objects, versioning, lifecycle policies, and static website hosting.

32 minutes

S3: Simple Storage Service

Amazon S3 is object storage built to store and retrieve any amount of data from anywhere. It's one of AWS's most popular and versatile services.

What is S3?

  • Object Storage: Store files of any type and size
  • Durability: 99.999999999% (11 nines) durability
  • Scalability: Store unlimited amounts of data
  • Accessibility: Access data from anywhere via HTTP/HTTPS

Core Concepts

Buckets

  • Containers for storing objects
  • Must have globally unique names
  • Region-specific but accessible globally
  • Can host static websites

Objects

  • Files stored in S3 (up to 5TB per object)
  • Consists of data and metadata
  • Identified by a unique key (filename)
  • Can be public or private

Storage Classes

S3 Standard

  • Frequently accessed data
  • Low latency, high throughput
  • Most expensive but fastest

S3 Intelligent-Tiering

  • Automatic cost optimization
  • Moves data between tiers based on access patterns

S3 Standard-IA (Infrequent Access)

  • Less frequently accessed data
  • Lower storage cost, retrieval fee
  • Perfect for backups

S3 Glacier

  • Long-term archival
  • Very low cost
  • Retrieval times from minutes to hours

S3 Glacier Deep Archive

  • Lowest cost storage
  • Retrieval time 12+ hours
  • For compliance and archival

Key Features

Versioning

  • Keep multiple versions of objects
  • Protect against accidental deletion
  • Easy rollback to previous versions

Lifecycle Policies

  • Automate transitions between storage classes
  • Automatically delete old versions
  • Reduce storage costs

Access Control

  • Bucket policies for bucket-level permissions
  • ACLs for object-level permissions
  • IAM policies for user-based access

Static Website Hosting

  • Host static websites directly from S3
  • Serve HTML, CSS, JavaScript, images
  • Integrate with CloudFront for CDN

Use Cases

  • Backup and Restore: Store critical backups
  • Data Lakes: Centralized repository for analytics
  • Static Websites: Host websites without servers
  • Media Storage: Store and serve images, videos
  • Application Data: Store application files and logs

Best Practices

  • Enable versioning for important data
  • Use lifecycle policies to reduce costs
  • Implement encryption at rest
  • Use CloudFront for frequently accessed content
  • Monitor usage with S3 analytics
  • Use bucket policies for security

Code Example

# S3 Operations with AWS CLI

# Create a new S3 bucket
aws s3 mb s3://my-unique-bucket-name-12345

# List all buckets
aws s3 ls

# Upload a file to S3
aws s3 cp myfile.txt s3://my-unique-bucket-name-12345/

# Upload directory recursively
aws s3 cp ./my-folder s3://my-unique-bucket-name-12345/my-folder/ --recursive

# Download a file from S3
aws s3 cp s3://my-unique-bucket-name-12345/myfile.txt ./

# Sync local directory with S3 bucket
aws s3 sync ./local-folder s3://my-unique-bucket-name-12345/remote-folder/

# List bucket contents
aws s3 ls s3://my-unique-bucket-name-12345/

# Delete a file
aws s3 rm s3://my-unique-bucket-name-12345/myfile.txt

# Delete bucket (must be empty)
aws s3 rb s3://my-unique-bucket-name-12345

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

# Configure static website hosting
aws s3 website s3://my-unique-bucket-name-12345/ \
  --index-document index.html \
  --error-document error.html

# Set bucket policy for public read access
aws s3api put-bucket-policy \
  --bucket my-unique-bucket-name-12345 \
  --policy '{
    "Version": "2012-10-17",
    "Statement": [{
      "Sid": "PublicReadGetObject",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::my-unique-bucket-name-12345/*"
    }]
  }'

# Create lifecycle policy (transition to Glacier after 30 days)
aws s3api put-bucket-lifecycle-configuration \
  --bucket my-unique-bucket-name-12345 \
  --lifecycle-configuration '{
    "Rules": [{
      "Id": "ArchiveOldFiles",
      "Status": "Enabled",
      "Transitions": [{
        "Days": 30,
        "StorageClass": "GLACIER"
      }]
    }]
  }'

# Generate pre-signed URL (valid for 1 hour)
aws s3 presign s3://my-unique-bucket-name-12345/myfile.txt --expires-in 3600