Amazon S3 is a scalable and durable storage service provided by AWS, allowing developers and businesses to store and retrieve any amount of data from anywhere on the web. It offers a variety of features for hosting static websites, managing files, and enabling replication across regions, making it highly versatile for different use cases.


Use Cases of Amazon S3

For Developers

  • Hosting Static Websites: Deploy React, Angular, or other static websites by uploading the build files to an S3 bucket.
  • Video Streaming: Serve video files efficiently.
  • Stateless Applications: Store user files and application data in S3 for scalable and stateless architecture.

For DevOps Engineers

  • Backup and Recovery: Use versioning and lifecycle policies for automatic backups and archival of files.
  • Log Storage: Store server or application logs for monitoring and analytics.

For Big Data Analytics

  • Data Lake: Store massive amounts of data in S3 for analytics using AWS tools like Athena, Redshift, or EMR.


Pricing Model

S3 pricing includes the following:

  • Storage Charges: Based on the size of data stored.
  • Data Transfer Charges: For data moving in and out of S3.
  • PUT/GET Requests: Based on the number of operations performed.
  • Retrieval and Bandwidth: Costs for downloading and retrieving data.


Getting Started with S3

Create a Bucket

  1. Navigate to the S3 console.
  2. Click on Create Bucket.
  3. Specify a meaningful name (e.g., shamim-portfolio).
  4. Choose a region to host the bucket.
  5. Configure settings like Versioning, Bucket Policy, and Public Access.

Naming Conventions

  • Bucket names must be globally unique.
  • Use only lowercase letters, numbers, hyphens, and periods.

Uploading Files

  • Uploaded files in S3 are referred to as objects.
  • You can upload files directly or use the UI to create folder-like structures (treated as files in the backend).


Permissions and Public Access

Making a Bucket Public

  • Go to the Permissions tab.
  • Edit Bucket Policy to allow public read access. Example policy:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::shamim-portfolio/*"
    }
  ]
}
  • Ensure Block Public Access settings are turned off.


Versioning in S3

Why Use Versioning?

  • Keeps a history of object changes.
  • Allows recovery of previous versions of a file.
  • Useful for backup and archival.

Enabling Versioning

  1. Navigate to the bucket.
  2. Go to Properties > Versioning > Enable.
  3. Upload the same file with different content.
  4. Check the Versions option to see all versions of the file.

Benefits of Versioning

  • Backup and recovery of files.
  • Ability to restore accidentally deleted or modified files.


Hosting a Static Website

  1. Create a Bucket:
  • Use a meaningful name like shamim-portfolio.
  • In advanced settings, enable Static Website Hosting.
  1. Upload Files:
  • Upload the index.html and error.html files for the website.
  • For React applications, upload the dist or build folder (including index.html and assets).
  1. Access Website:
  • Copy the Bucket URL from the Properties tab.
  • Paste it in the browser to see the website.

Redirecting Static Websites

  • Create another bucket.
  • In advanced settings, configure a Redirect Rule:
  • Redirect /docs to https://shamim-portfolio.com/documents.


Cross-Origin Resource Sharing (CORS)

Need for CORS

  • Enables web applications to access resources from different origins (e.g., API hosted on a different service).

Configuring CORS

  1. Navigate to the bucket.
  2. Go to Permissions > CORS Configuration.
  3. Add rules to allow cross-origin access:
[
  {
    "AllowedHeaders": ["*"],
    "AllowedMethods": ["GET", "PUT", "POST"],
    "AllowedOrigins": ["*"]
  }
]


Replication in S3

Why Use Replication?

  • Data backup across multiple regions.
  • High availability and durability.

Steps to Enable Replication

  1. Enable Versioning on both source and destination buckets.
  2. Go to the Management tab of the source bucket.
  3. Configure Replication Rules:
  • Select All Objects or use prefixes.
  • Specify the destination bucket.
  • Choose IAM roles for replication.
  1. Upload a new file to the source bucket and verify its replication in the destination bucket.

Benefits of Replication

  • Cross-account replication for collaboration.
  • Disaster recovery for critical data.


Behind-the-Scenes of S3

  • Files uploaded to S3 are automatically replicated within the same region to ensure durability.
  • For cross-region replication, users explicitly create replication rules.


Practical Tips

  • Always use meaningful bucket names and prefixes for better organization.
  • Enable versioning for critical buckets to safeguard data.
  • Use lifecycle policies to automate archival or deletion of old files.
  • Monitor S3 usage and costs regularly to optimize expenses.


With these concepts and practical insights, you are now well-equipped to use Amazon S3 for a variety of purposes, from hosting static websites to implementing advanced features like cross-origin policies and replication.