0
0
AWScloud~5 mins

Why S3 matters for object storage in AWS - Why It Works

Choose your learning style9 modes available
Introduction
Storing files safely and making them easy to access is a common challenge. Amazon S3 solves this by providing a simple way to save and retrieve any amount of data from anywhere on the internet.
When you want to store photos or videos for a website without worrying about running out of space.
When you need to back up important files and want them safe even if your computer breaks.
When you want to share large files with friends or coworkers easily and securely.
When you run an app that needs to save user data like documents or logs without managing your own storage servers.
When you want to archive old data that you rarely use but must keep for future reference.
Config File - bucket-policy.json
bucket-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowPublicRead",
      "Effect": "Allow",
      "Principal": "*",
      "Action": ["s3:GetObject"],
      "Resource": ["arn:aws:s3:::example-bucket/*"]
    }
  ]
}

This JSON file defines a bucket policy that allows anyone on the internet to read objects inside the bucket named example-bucket. The Version is the policy language version. The Statement contains rules; here it allows public read access to all files inside the bucket.

Commands
This command creates a new S3 bucket named 'example-bucket' where you can store your files.
Terminal
aws s3 mb s3://example-bucket
Expected OutputExpected
make_bucket: example-bucket
This uploads the file 'photo.jpg' from your computer to the 'example-bucket' in S3.
Terminal
aws s3 cp photo.jpg s3://example-bucket/
Expected OutputExpected
upload: ./photo.jpg to s3://example-bucket/photo.jpg
This applies the public read policy to the bucket so anyone can view the files inside it.
Terminal
aws s3api put-bucket-policy --bucket example-bucket --policy file://bucket-policy.json
Expected OutputExpected
No output (command runs silently)
--bucket - Specifies the bucket name to apply the policy
--policy - Points to the JSON file containing the policy
This lists all files currently stored in the 'example-bucket' so you can verify your upload.
Terminal
aws s3 ls s3://example-bucket/
Expected OutputExpected
2024-06-01 12:00:00 12345 photo.jpg
Key Concept

If you remember nothing else from this pattern, remember: Amazon S3 lets you store and access files easily and safely from anywhere without managing your own storage hardware.

Common Mistakes
Trying to upload files before creating the bucket.
The upload fails because the bucket does not exist yet.
Always create the bucket first using 'aws s3 mb' before uploading files.
Not setting a bucket policy when you want public access.
Files remain private and cannot be accessed by others over the internet.
Apply a bucket policy that grants read permissions to the public if you want files accessible.
Using incorrect bucket names with uppercase letters or spaces.
S3 bucket names must be lowercase and follow naming rules; otherwise, commands fail.
Use only lowercase letters, numbers, and hyphens in bucket names.
Summary
Create an S3 bucket to hold your files using 'aws s3 mb'.
Upload files to the bucket with 'aws s3 cp'.
Set a bucket policy to control who can access your files.
List files in the bucket to confirm your uploads.