Nov 20, 20266 min read

How to Serve Public Files from an S3 Bucket

Why new S3 buckets block public access, the bucket policy that makes a prefix public, why CloudFront with Origin Access Control is the better default, and how to serve public images from your own AWS account without hand-editing bucket settings.

Illustration for How to Serve Public Files from an S3 Bucket

S3 buckets used to be made public with a checkbox and an ACL, which is how so many of them leaked data. New buckets start with Block Public Access on and ACLs disabled, so the old tutorial steps fail with AccessDenied.

This guide covers the bucket policy route for product images, avatars or downloads, the CloudFront route most production apps use, and a way to get the second from a declaration in code.

Why Is My New S3 Bucket Not Public?

Every bucket created since April 2023 has all four Block Public Access settings enabled and Object Ownership set to bucket owner enforced, which disables ACLs, as AWS announced at the time. Two consequences trip people up:

  • A public-read ACL on an upload is rejected, because the bucket no longer accepts ACLs at all.
  • A public bucket policy is rejected or ignored while BlockPublicPolicy and RestrictPublicBuckets are on, and the account-level settings override the bucket-level ones.

The Block Public Access documentation lists what each of the four settings blocks, and it is worth checking the account level first, because many organizations turn it on there for every account.

How Do You Make One Prefix Public with a Bucket Policy?

If you accept public S3 URLs, keep the public files under one prefix and grant read access to that prefix only. First relax the two policy settings on the bucket and leave the ACL settings on:

~
$ aws s3api put-public-access-block --bucket acme-media \ --public-access-block-configuration \ BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=false,RestrictPublicBuckets=false$ aws s3api put-bucket-policy --bucket acme-media --policy file://policy.json

The policy allows anonymous GetObject on public/* and nothing else, so listing the bucket or reading keys outside the prefix still fails:

{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadForPublicPrefix", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::acme-media/public/*" } ] }

Objects under the prefix are then readable at https://acme-media.s3.eu-west-1.amazonaws.com/public/logo.png. This works, but every request goes to the bucket's region, you pay S3 request and transfer rates for each one, and the URL shows the bucket name. If you were planning to use the S3 static website endpoint for a custom domain, note that website endpoints serve HTTP only, so HTTPS on your domain needs CloudFront anyway.

Why Is CloudFront the Better Default?

With CloudFront in front, the bucket stays fully private with all four Block Public Access settings on, and only the CloudFront distribution can read from it through Origin Access Control. Users get HTTPS on your own domain and edge caching, and a misconfigured bucket policy can no longer expose the rest of the bucket. The bucket policy names the distribution instead of *:

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowCloudFrontRead", "Effect": "Allow", "Principal": { "Service": "cloudfront.amazonaws.com" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::acme-media/*", "Condition": { "StringEquals": { "AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/EDFDVBD6EXAMPLE" } } } ] }

The setup cost is that you now own a distribution, an OAC, a certificate in us-east-1 for the custom domain, a cache policy and the bucket policy that ties them together, usually in Terraform. Caching also changes how you update files: if you overwrite avatar.png, users can see the old image until the cache entry expires or you pay for an invalidation, so write each new version under a new key and store the key in your database.

What Should Never Go in a Public Bucket?

Anything tied to one user or one purchase, such as invoices, exports, ID documents or paid downloads, belongs in a private bucket served through short-lived presigned URLs, because a public URL works for anyone who gets hold of it and cannot be revoked. Keep public and private files in separate buckets rather than separate prefixes of one bucket, so a policy mistake on one cannot reach the other.

How Does Encore Serve Public Files on AWS?

Encore

Encore brings infrastructure from code, so a public bucket is one Go declaration with Public: true, and on AWS Encore provisions it in your own account with CloudFront in front, which is the setup the previous section recommends. The example below takes product image uploads from an admin client and returns the CDN URL to store with the product.

Set up the app

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

~
$ encore app create media-api --example=hello-world$ cd media-api && mkdir -p media auth

The bucket and the upload endpoint live in the media service. Each upload gets a new key, so a replaced image never fights an old CDN cache entry:

// media/media.go package media import ( "encoding/json" "fmt" "io" "net/http" "regexp" "time" "encore.dev" "encore.dev/beta/errs" "encore.dev/storage/objects" ) var ProductImages = objects.NewBucket("product-images", objects.BucketConfig{ Public: true, }) var productID = regexp.MustCompile(`^[a-z0-9-]{1,64}$`) var extensions = map[string]string{ "image/png": ".png", "image/jpeg": ".jpg", "image/webp": ".webp", } //encore:api auth raw method=PUT path=/products/:id/image func UploadImage(w http.ResponseWriter, req *http.Request) { id := encore.CurrentRequest().PathParams.Get("id") ct := req.Header.Get("Content-Type") ext, ok := extensions[ct] if !productID.MatchString(id) || !ok { http.Error(w, "invalid product id or content type", http.StatusBadRequest) return } key := fmt.Sprintf("products/%s/%d%s", id, time.Now().UnixNano(), ext) wr := ProductImages.Upload(req.Context(), key, objects.WithUploadAttrs(objects.UploadAttrs{ ContentType: ct, })) if _, err := io.Copy(wr, http.MaxBytesReader(w, req.Body, 10<<20)); err != nil { wr.Abort(err) errs.HTTPError(w, err) return } if err := wr.Close(); err != nil { errs.HTTPError(w, err) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(map[string]string{ "key": key, "url": ProductImages.PublicURL(key).String(), }) }

Uploads require a caller with the admin token, checked by an auth handler that reads the token from an Encore secret:

// auth/auth.go package auth import ( "context" "crypto/subtle" "encore.dev/beta/auth" "encore.dev/beta/errs" ) var secrets struct { AdminToken string } //encore:authhandler func AuthHandler(ctx context.Context, token string) (auth.UID, error) { if subtle.ConstantTimeCompare([]byte(token), []byte(secrets.AdminToken)) != 1 { return "", &errs.Error{Code: errs.Unauthenticated, Message: "invalid token"} } return "admin", nil }
~/media-api
$ encore secret set --type dev,local AdminToken$ encore run$ curl -X PUT localhost:4000/products/blue-mug/image \ -H "Authorization: Bearer $ADMIN_TOKEN" \ -H "Content-Type: image/png" --data-binary @mug.png

Locally, encore run stores the objects on your machine, so the upload flow and the admin client can be tried before anything is deployed. Deployed to your AWS account, the same code uses an S3 bucket with CloudFront in front, and Encore provisions an IAM role for the service. The object storage docs cover the rest of the API, including signed download URLs for a private bucket next to this one.

With the bucket and its public flag in the code, an agent adding a "brand assets" bucket writes the declaration and the endpoint in one change and checks the URLs locally, and no one has to open the S3 console to loosen Block Public Access.

Best fit: apps on AWS that serve product images, avatars or other public assets and want them on a CDN in their own account, declared next to the code that writes them.

S3 Public Files Checklist

  • Check Block Public Access at the account level before debugging the bucket.
  • Use bucket policies, not ACLs, on buckets created after April 2023.
  • Grant public read on one prefix, never on the whole bucket with ListBucket.
  • Prefer CloudFront with Origin Access Control and a fully private bucket.
  • Write changed files under new keys instead of overwriting cached ones.
  • Keep user-specific and paid files in a separate private bucket with presigned URLs.

Frequently asked questions

How do I make an S3 bucket public?

Turn off the BlockPublicPolicy and RestrictPublicBuckets settings for the bucket, and at account level if they are on there, then add a bucket policy that allows s3:GetObject for Principal "*" on the objects you want public, ideally a single prefix such as public/*. Anyone can then read those objects at their S3 URL.

Why do I get Access Denied on a public S3 object?

Block Public Access is still on at the bucket or account level, the bucket policy does not cover the object's key, or you tried to use an ACL on a bucket where ACLs are disabled. New buckets have Block Public Access on and ACLs disabled by default since April 2023.

Should I use a public bucket or CloudFront?

For anything served to users, put CloudFront in front of a private bucket with Origin Access Control. You get HTTPS on your own domain, edge caching and a bucket that stays private, and only CloudFront can read from it.

Can I make only one folder of an S3 bucket public?

Yes. S3 has no folders, only key prefixes, but a bucket policy can grant s3:GetObject on arn:aws:s3:::BUCKET/public/* only, so keys under that prefix are public and everything else stays private.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders