Resize uploads in the background instead of in the request: upload originals to a private bucket, publish an event, generate thumbnails in a subscriber and serve them from a public bucket. A Go example with Encore, plus when to use on-the-fly resizing instead.

Resizing an image inside the upload request is a quick way to make an API slow. Decoding a 12-megapixel photo takes tens of milliseconds of CPU and a lot of memory, and a few simultaneous uploads from a phone app can push an instance over.
A short pipeline works better: upload the original, publish an event, resize in a worker and store the results. This guide builds it in Go with two buckets and a topic.
Four pieces: a private bucket for originals, a place to put thumbnails, a queue between them and a worker that does the resizing. The client uploads straight to storage with a signed URL, which keeps large files off your API. The upload triggers an event carrying the object key, and the worker produces a fixed set of sizes, for example 200 and 800 pixels wide.
Deriving thumbnail keys from the original key and the width, such as thumbs/<id>/200.jpg, makes regeneration idempotent, since a retried job overwrites the same objects.
Pre-generated sizes are simple and serve from a bucket or CDN with no compute at request time. They fit when the UI uses a few known sizes. On-the-fly resizing, where a URL parameter selects the size and a service generates and caches it, fits when sizes vary widely, but it adds a service to run and secure. Start pre-generated and move only when the list of sizes stops being manageable.
The standard library decodes JPEG and PNG through the image packages, and the golang.org/x/image/draw package provides scalers, with CatmullRom giving good quality for downscaling, and the Go blog's image/draw article explains the model. Preserve the aspect ratio by computing the height from the target width, and encode JPEG at quality around 80 for photos. Read EXIF orientation before resizing if the source is a phone photo, since a rotated image otherwise ends up sideways. Limit the maximum accepted dimensions before decoding, because a small compressed file can describe a very large bitmap.
Encore is a backend framework with built-in infrastructure, so the two buckets and the Pub/Sub topic are declared in the Go service that uses them. On deploy Encore provisions S3 and SNS with SQS in your AWS account, or Cloud Storage and Pub/Sub on GCP, and encore run provides local versions.
Run in your terminal to get started locally.
// images/images.go
package images
import (
"bytes"
"context"
"fmt"
"image"
"image/jpeg"
_ "image/png"
"io"
"time"
"encore.dev/pubsub"
"encore.dev/rlog"
"encore.dev/storage/objects"
"github.com/google/uuid"
"golang.org/x/image/draw"
)
var originals = objects.NewBucket("originals", objects.BucketConfig{})
var thumbs = objects.NewBucket("thumbnails", objects.BucketConfig{Public: true})
type Uploaded struct {
ImageID string `json:"image_id"`
}
var UploadedTopic = pubsub.NewTopic[*Uploaded]("image-uploaded", pubsub.TopicConfig{
DeliveryGuarantee: pubsub.AtLeastOnce,
})
var widths = []int{200, 800}
func resize(src image.Image, width int) image.Image {
b := src.Bounds()
height := b.Dy() * width / b.Dx()
dst := image.NewRGBA(image.Rect(0, 0, width, height))
draw.CatmullRom.Scale(dst, dst.Bounds(), src, b, draw.Over, nil)
return dst
}
var _ = pubsub.NewSubscription(UploadedTopic, "make-thumbnails", pubsub.SubscriptionConfig[*Uploaded]{
Handler: func(ctx context.Context, m *Uploaded) error {
r := originals.Download(ctx, "originals/"+m.ImageID)
defer r.Close()
data, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("read original: %w", err)
}
src, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
return nil // not an image, retrying will not help
}
for _, w := range widths {
var buf bytes.Buffer
if err := jpeg.Encode(&buf, resize(src, w), &jpeg.Options{Quality: 80}); err != nil {
return err
}
key := fmt.Sprintf("thumbs/%s/%d.jpg", m.ImageID, w)
up := thumbs.Upload(ctx, key, objects.WithUploadAttrs(objects.UploadAttrs{ContentType: "image/jpeg"}))
if _, err := up.Write(buf.Bytes()); err != nil {
up.Abort(err)
return err
}
if err := up.Close(); err != nil {
return err
}
}
rlog.Info("thumbnails created", "image", m.ImageID)
return nil
},
MaxConcurrency: 4,
})
type UploadURL struct {
URL string `json:"url"`
ImageID string `json:"image_id"`
}
//encore:api public method=POST path=/images
func CreateUpload(ctx context.Context) (*UploadURL, error) {
id := uuid.NewString()
u, err := originals.SignedUploadURL(ctx, "originals/"+id, objects.WithTTL(15*time.Minute))
if err != nil {
return nil, err
}
return &UploadURL{URL: u.URL, ImageID: id}, nil
}
type NotifyParams struct {
ImageID string `json:"image_id"`
}
//encore:api public method=POST path=/images/uploaded
func Notify(ctx context.Context, p *NotifyParams) error {
_, err := UploadedTopic.Publish(ctx, &Uploaded{ImageID: p.ImageID})
return err
}
Add the image package with go get golang.org/x/image. The subscriber returns nil for files that do not decode, since a retry cannot fix them, and limits itself to four images at a time per instance. Add an auth handler so only signed-in users can create uploads, and each upload appears in a trace. Thumbnail objects live in a public bucket, so a front end can use their public URLs directly.
A coding agent can add a new thumbnail width by editing the widths slice in one change, and verify the pipeline locally by uploading a file to the local bucket and reading the output keys.
Best fit: Go teams that accept user uploads and want background resizing with originals kept private and thumbnails public in their own AWS or GCP account.
Pre-generating a fixed set of sizes at upload time is simple and cheap to serve, and fits when you control the sizes the UI uses. On-the-fly resizing behind a CDN fits when sizes vary widely, but it needs an image service that caches results.
Have the client upload the original to object storage with a signed URL, then publish an event with the object key. A subscriber downloads the original, resizes it, uploads the thumbnails and records the result, so the request path does no image work.
Use separate buckets, with originals private and thumbnails public or served through a CDN, and derive thumbnail keys from the original key and size. That keeps access rules simple and makes regeneration idempotent.
Declare a private bucket for originals, a public bucket for thumbnails and a Pub/Sub topic for uploads in a Go service. A subscription downloads the original, resizes it with golang.org/x/image/draw and uploads the result, and Encore provisions the buckets and topic in your cloud account.
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.