0

S3 Isn’t Just a "Dumb" Hard Drive: Hard-Won Lessons from Production

1. The Trap of Convenience

When I first started out in backend development, my mental model for file storage was simple: need to save an image? Dump it on the server. Running out of space? Buy a bigger disk. It worked—until the project actually grew, and managing files across servers turned into an operational nightmare.

That’s usually when Amazon S3 (or S3-compatible alternatives like MinIO and Google Cloud Storage) shows up like a silver bullet.

Here’s the catch: if you bring your traditional filesystem mindset to S3, it will bite you. Treat it like a dumb folder on a remote server, and you’re one deployment away from a terrifying AWS bill—or worse, a massive data leak.

Here are the four lessons I learned the hard way.

2. Never Make a Bucket Public

The fastest way to get an image rendering on the frontend is to flip the bucket to Public and copy-paste the URL. Don’t do it. One misconfigured upload path, and suddenly private invoices, user IDs, or internal exports are indexed by the internet.

Instead, stick to two non-negotiable rules:

  • Private by default: Keep "Block all public access" turned on. No exceptions.

  • Least Privilege: Create a dedicated IAM User (or better yet, an IAM Role if you're running on AWS) exclusively for your app. Scope its policy strictly to what it needs—usually just s3:PutObject and s3:GetObject on a specific bucket. Never hardcode Admin credentials into your application.

If everything is private, how do users actually view their files? Enter Presigned URLs.

3. Presigned URLs: The Temporary "VIP Pass"

Instead of handing out permanent public links, your backend (whether you're using Go, Laravel, or Node.js) generates a temporary, cryptographically signed URL that expires in a few minutes—say, 5 minutes.

  • Why it works: Even if someone intercepts or shares the link, it becomes completely useless the moment it expires. Your bucket stays locked down.

  • A quick tip for Gophers: Generating presigned URLs in the AWS SDK for Go v2 is super clean, but always pass a context with a timeout. If there’s a network hiccup or credential refresh stall, an unbounded context.Background() will happily hang your goroutine.

Go

ctx, cancel := context.WithTimeout(req.Context(), 5*time.Second)
defer cancel()

presignedReq, err := presignClient.PresignGetObject(ctx, &s3.GetObjectInput{
    Bucket: aws.String("my-private-bucket"),
    Key:    aws.String(objectKey),
}, s3.WithPresignExpires(5*time.Minute))

4. The "Folder" Illusion

Here’s a mental shift that changes how you architect storage: S3 has no folders.

Under the hood, a bucket is just a massive, flat key-value store. The / you see in the AWS console is just a character in the file's name (called a Prefix) that the UI renders like a directory to make humans feel comfortable.

Knowing this, you should design your object keys intentionally from day one. My go-to pattern is:

Plaintext

<service_name>/<year>/<month>/<day>/<random_uuid>.jpg

Why this structure?

  1. No filename collisions: UUIDs eliminate the risk of users overwriting each other's avatar.png.

  2. Effortless Lifecycle Policies: Because everything is grouped by service and date prefix, setting up automated rules—like deleting temporary exports after 7 days or moving old logs to S3 Glacier for pennies—takes two minutes instead of a custom cleanup script.

5. The Real Cost Trap: Data Transfer Out

S3 storage is dirt cheap. Data Transfer Out (egress) is where your budget goes to die.

Do the quick napkin math: imagine you host a single 5MB image and serve it directly from S3 to 1 million views. That’s 5TB of egress. At standard AWS rates (~$0.09/GB), you’re looking at roughly $450 for a single image.

The fix: Never serve high-traffic assets directly from S3. Always put a CDN (like Amazon CloudFront with Origin Access Control, or Cloudflare) in front of your bucket.

  • Your users get faster load times from edge servers closer to them.

  • Your S3 bucket only gets hit on a cache miss, slashing both load and egress costs.

6. Wrapping Up

S3 is one of the most reliable pieces of infrastructure ever built, but it rewards engineers who understand how it actually works under the hood. Lock your buckets down, use presigned URLs, design your prefixes for the future, and always hide it behind a CDN.

What’s a painful S3 lesson or clever trick you’ve picked up in production? Drop it in the comments—I’d love to hear how you’re handling it.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí