How to Stream AI-Generated PNGs — API Routes, Object Storage, Signed Downloads A developer outlined a pattern for serving AI-generated PNGs in Next.js by generating each image inside an API route, uploading it once to private object storage under a unique tenant-prefixed key, and returning a short-lived signed URL instead of proxying bytes through the app runtime. The writeup recommends UUID-based object keys to avoid overwrite races, since the storage surface lacks conditional writes, versioning and object lock, and notes it offers no cross-region replication or public-read ACLs. Short answer: generate each PNG inside the Next.js API route, stream it once into private object storage under a new key, then return that key and a short-lived signed URL. This keeps storage credentials out of the browser and makes large-file throughput depend on one upload rather than an app-server relay on every later view. That's the loop. The deciding constraint is byte movement. A media SaaS serving customers in the US and EU should not repeatedly proxy a finished image through its application runtime. The route should do the privileged work once. Later previews and downloads should go straight to storage through an expiring URL. There is a catch. This pattern is for private generated assets, not permanent public image hosting. Public-read ACLs aren't available in the Infrai option discussed below, and its public url remains null. If the product needs a public CDN origin or static-site asset bucket, choose a storage product and delivery setup designed for that job. Keep the route boring: validate the request, call the app's existing image generator, assign a fresh object key, upload the PNG privately, request a signed link, and return both the key and signing response. Don't send a storage credential to the client. Don't send the bearer token to the signed URL either; that URL is already the scoped authorization. A fresh key matters more than it first appears. Strict overwrite coordination cannot rely on If-Match conditional writes here, and object versioning or object lock isn't available. Reusing latest.png therefore turns two simultaneous regenerations into a race with no recovery guarantee. Imagine an editor clicks Regenerate twice: worker A finishes slowly, worker B finishes quickly, and both target the same name. Whichever write lands last becomes the visible object, even if the database selected the other result. A tenant prefix plus a random generation ID removes that collision without adding a lock service. Each worker writes its own immutable-by-convention location, and the application changes a database pointer only after the chosen generation is ready. The store isn't providing immutability; the naming rule is avoiding overwrites. It also makes deletion and audit records easier to associate with one generation. For example, use a shape such as tenants/acme/generations/550e8400-e29b-41d4-a716-446655440000.png . The UUID is an object identity, not a benchmark result. Keep the database row as the source of truth for which generation the UI currently displays. No shared filename. For regional deployments, pick the bucket placement before this route runs and keep the tenant-to-bucket mapping in application configuration or data. There is no cross-region automatic replication in this surface, so a product that requires automatic US-to-EU replication needs an external replication design or a different provider. I'm not sure which region policy fits a given SaaS without its residency contract and recovery objectives; those two documents should decide it, not a generic latency slogan. The handler below assumes the application already has a typed generatePng function for its chosen image model. That boundary is deliberate: generation vendors have different request schemas, while the storage rule stays the same. The function must return a PNG Uint8Array ; the route never accepts storage credentials or generated bytes from the browser. The storage calls use the two verified operations needed for this flow: PUT /v1/storage/object/put/{bucket}/{key} and POST /v1/storage/object/presign/{bucket}/{key} . The API root and secrets come from environment variables. Set INFRAI API BASE to the service's v1 API root. Each request declares its method, checks non-success responses, and backs off on HTTP 429 while honoring Retry-After . Small surface. Good. js import { randomUUID } from "node:crypto"; import { generatePng } from "@/lib/generate-png"; type GenerateBody = { prompt: string; tenantId: string; }; const apiBase = requiredEnv "INFRAI API BASE" .replace /\/$/, "" ; const apiKey = requiredEnv "INFRAI API KEY" ; const bucket = requiredEnv "MEDIA BUCKET" ; function requiredEnv name: string : string { const value = process.env name ; if value throw new Error Missing environment variable: ${name} ; return value; } function encodePath value: string : string { return value.split "/" .map encodeURIComponent .join "/" ; } async function requestWithBackoff url: string, init: RequestInit, attempts = 4, : Promise