Short answer: put the generated image bytes in object storage and keep ownership, lifecycle state, and a content reference in a relational database. The split matters because a media service has two very different jobs: moving large opaque bytes and answering small, transactional questions about who may retrieve them. A database is excellent at the second job. It is a poor place to make every image row carry the first job.
For a media product accepting private user uploads, I would make tenant isolation the first design constraint. A download link should identify one object, expire predictably, and never become a permission model by accident. The database record is the policy anchor. The object store is the byte container.
Keep the boundary boring.
Why is object storage better than relational databases for storing images?
An AI-generated image is an opaque binary payload. The application usually needs to query its owner, generation request, MIME type, dimensions, retention state, and creation time. It rarely needs SQL to inspect the pixels. That is the useful boundary.
A relational row can hold a blob, but that does not make the row a good media boundary. Large values increase backup and replication work, compete with short transactional queries for I/O, and make ordinary records harder to move between environments. The exact failure profile depends on the database engine and workload, so I would measure it against the image sizes and retention period that the service actually expects. The architectural point survives those details: metadata has relational shape; image bytes do not.
Object storage also gives the service a namespace that can be designed around tenant ownership. A key such as tenants/acme/assets/01J.../original.webp is useful for organization and operations, but it is not an authorization check. The request must still validate the asset's tenant in the database before issuing a time-limited URL.
That distinction prevents a common mistake: treating an unguessable key as proof that the caller is allowed to read it. Randomness reduces accidental discovery. It does not replace policy.
What does an expiring download flow need to prove?
The flow needs to prove three things in order: the caller is authenticated, the asset belongs to the caller's tenant, and the resulting capability expires within the allowed window. I keep those checks in application code because they are visible in tests and logs.
Here is a deliberately small interface. It hides provider-specific signing details and keeps the policy decision next to the database lookup.
type Asset = { id: string; tenantId: string; objectKey: string; contentType: string; status: 'ready' | 'deleted'; }; type AssetRepository = { findForTenant(assetId: string, tenantId: string): Promise<Asset | null>; }; type ObjectSigner = { signGet(input: { key: string; expiresInSeconds: number }): Promise<string>; }; export async function createDownloadLink( repository: AssetRepository, signer: ObjectSigner, input: { tenantId: string; assetId: string }, ): Promise<string> { const asset = await repository.findForTenant(input.assetId, input.tenantId); if (!asset || asset.status !== 'ready') { throw new Error('Asset not available'); } return signer.signGet({ key: asset.objectKey, expiresInSeconds: 300, }); } Five minutes is an example policy value, not a universal answer. A browser preview may need a short window. A background export may need a longer one. The important part is that the duration is explicit, bounded, and covered by tests. Do not put a permanent public URL in the image record and call it an expiring link later.
The signer should constrain the operation to a read of the exact object key. It should not grant a bucket-wide listing permission, and the service should avoid returning storage credentials to the client. At the HTTP layer, set the content disposition and type from trusted metadata, while treating uploaded filenames as display data rather than path input.
Which storage failures show up after the happy path?
The first failure is a race between metadata and bytes. A database transaction can create an asset record before the upload completes, or the upload can finish before the record is committed. A ready state should mean that the service has verified the object exists and has recorded enough integrity information to operate on it. Until then, a download request gets no link.
The second failure is deletion. Removing a database row does not necessarily remove the object, and removing the object first can leave a record pointing into empty space. Use an explicit lifecycle state and an idempotent cleanup worker. Retrying cleanup should be safe. The worker should also carry the tenant and asset identifiers into structured logs so an operator can trace one deletion without dumping user content. This is where storing media in separate object storage is better operationally: the cleanup process can reason about bytes and relational databases can track state without pretending that a missing row proves the file is gone.
The third failure is cross-tenant access caused by a convenient repository method. findById(assetId) is easy to call and easy to misuse. The safer shape is findForTenant(assetId, tenantId), where the tenant predicate is part of the data access contract. I would add a test that creates two tenants, gives each one an asset, and verifies that a valid asset ID from tenant A cannot produce a link for tenant B. That test is more valuable than checking that a random key looks random.
The fourth failure is cache leakage. A signed URL may be valid for only 300 seconds, but an intermediary can cache the response according to its own rules. Choose cache headers intentionally, and make private media private at the delivery layer. A CDN, reverse proxy, or application cache must include the tenant-sensitive identity in its key or be bypassed for protected responses.
What would I change at scale?
At small volume, one metadata table, one object namespace, and one cleanup worker are enough. At larger volume, I would separate original files from derived previews, record a checksum and byte length, and make every transformation produce a new asset record rather than mutating the original. That gives retries a stable target and makes audit history readable.
I would benchmark four operations with representative image sizes: database metadata lookup, direct object upload, signed download, and cleanup delay. The useful measurements are latency distributions, failed-transfer rates, transaction contention, and the number of orphaned objects. A single average upload time hides too much.
The trade-off is operational surface area. Two systems mean two credentials, two health signals, and a reconciliation job. That is real work. It is still a smaller and clearer problem than forcing a transactional database to be the media distribution layer, then discovering that backup windows and tenant deletion rules are coupled to image volume.
The database remains the source of truth for ownership and state. Object storage remains the source of truth for bytes. Neither system should pretend to be the other.
Decision rule
Use object storage for generated images when the application treats the image as an opaque file, needs direct or signed retrieval, and expects independent retention or delivery behavior. Keep relational data for tenant identity, authorization, status, generation parameters, checksums, and audit records.
A blob column can be reasonable for very small files, tightly coupled transactions, or a system whose backup and delivery requirements are deliberately database-shaped. It is not automatically wrong. It is the wrong default for a private media pipeline when tenant isolation, expiring access, and independent file lifecycle are the real constraints.
The design is not “database versus object storage.” It is a narrow policy record paired with a binary transport. Once that boundary is explicit, the security tests, cleanup behavior, observability, and scaling decisions become easier to inspect.
Further reading
- PostgreSQL documentation on binary data: https://www.postgresql.org/docs/current/datatype-binary.html
- Amazon Web Services, S3 presigned URLs: https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-presigned-url.html
- RFC 9110, HTTP Semantics: https://www.rfc-editor.org/rfc/rfc9110
- MDN, HTTP caching: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching
- Backblaze B2 cloud storage pricing: https://www.backblaze.com/cloud-storage/pricing
