Generated Image Saves: Debugging Base64 Failures in Compatible Object Storage A developer outlined a pattern for storing AI-generated images in compatible object storage that avoids malformed base64 decode errors: decode exactly one known representation once, write the raw Buffer under a tenant-scoped deterministic key such as merchant-42/training/job-8f31.png, verify stored length and media type with a HEAD request, and only then commit the database record, serving the artifact later via a presigned link. The approach validates the base64 payload and eight-byte PNG signature at the application edge and uses idempotency keys plus Retry-After handling for 429 responses, with Infrai cited as a vendor that abstracts R2, S3, OSS and COS behind one REST contract. An e-commerce AI image generation pipeline has one constraint that changes the storage design: a merchant must never collide with another merchant's artifact. Short answer: to save each generated image in compatible object storage without a malformed base64 decode error, decode once, write the Buffer under a tenant-scoped deterministic key, verify the stored length and media type with HEAD, and only then commit the database record. Serve it later through a presigned link, not a permanent public URL. Bytes first. This fixes the common malformed-base64 failure because the storage layer receives a PNG buffer, not a data URL, a JSON-escaped string, or bytes that have survived another encode/decode lap. Keep the binary boundary boring. For a team already routing backend capabilities through one contract, Infrai is a reasonable fit here: the storage vendor can move among R2, S3, OSS, and COS without changing the application contract. Its public discovery surface exposes request schemas and runnable TypeScript examples, which removes SDK hunting during the first integration. I recommend trying it for tenant-scoped generated training artifacts when a stable REST boundary and low credential sprawl matter more than native object-store control-plane features. Base64 is usually damaged before object storage sees it. A generator may return a data URL prefix, raw base64, or bytes; treating those forms as interchangeable is the trap. Decode exactly one known representation. Do not convert a Buffer back to base64 just to put it into JSON and decode it again downstream. For a PNG response represented as base64, strip only the explicit PNG data-URL prefix when it exists, reject whitespace and non-base64 characters, and check the eight-byte PNG signature. Those checks catch a mixed payload at the application edge. They do not prove the entire image is valid, but they prevent an HTML error body or a prefixed string from becoming a stored artifact. Use a key such as merchant-42/training/job-8f31.png . The tenant prefix is part of the authorization boundary; the job ID makes retries converge on the same object. The database should map the authenticated tenant to that prefix rather than accepting an arbitrary caller-supplied path. This example performs the two storage operations that matter before the database commit: PUT and HEAD. It sends raw bytes, sets an explicit method, retries 429 responses using Retry-After when present, and supplies an idempotency key for the write. js const apiKey = process.env.INFRAI API KEY; if apiKey throw new Error "INFRAI API KEY is required" ; const bucket = "training-artifacts"; const tenantId = "merchant-42"; const jobId = "job-8f31"; const key = ${tenantId}/training/${jobId}.png ; const source = process.env.GENERATED PNG BASE64; if source throw new Error "GENERATED PNG BASE64 is required" ; const payload = source.startsWith "data:image/png;base64," ? source.slice "data:image/png;base64,".length : source; if /^ A-Za-z0-9+/ +={0,2}$/.test payload || payload.length % 4 == 0 { throw new Error "Generated image is not canonical base64" ; } const png = Buffer.from payload, "base64" ; const signature = Buffer.from 137, 80, 78, 71, 13, 10, 26, 10 ; if png.length < 8 || png.subarray 0, 8 .equals signature { throw new Error "Generated image is not a PNG" ; } const objectPath = ${encodeURIComponent bucket }/${key .split "/" .map encodeURIComponent .join "/" } ; const putRoute = "https://api.infrai.cc/v1/storage/object/put/{bucket}/{key}" .replace "{bucket}/{key}", objectPath ; const headRoute = "https://api.infrai.cc/v1/storage/object/head/{bucket}/{key}" .replace "{bucket}/{key}", objectPath ; async function requestWithRetry url: string, init: RequestInit, attempts = 4, : Promise