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.

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.
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:
public-read ACL on an upload is rejected, because the bucket no longer accepts ACLs at all.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.
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:
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.
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.
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.
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.
Run in your terminal to get started locally.
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
}
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.
ListBucket.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.
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.
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.
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.