The important design choice is to make tenant isolation a storage invariant, not a filter added during export. For a fintech application that stores AI-generated product images, each object needs an immutable tenant boundary, metadata that can be checked independently, and an export job that refuses ambiguous records. I would accept a little duplicated validation to get that property; a fast export that can mix two tenants is a security incident.
Short answer: store the bytes separately from searchable metadata
Put the image in object storage under a tenant-scoped key, and put its metadata in a database with tenant_id as part of every lookup and uniqueness rule. Keep the object key, content type, byte size, checksum, prompt revision, and creation time in the row. Generate an export from rows selected for one tenant, then retrieve only the corresponding object keys. A Node.js service can call this same contract; the policy should not depend on its runtime.
The flow is straightforward: the image-generation worker writes a temporary object, verifies its digest and media type, records a metadata row in the same application workflow, and marks the asset ready. An authenticated export request creates a snapshot of ready rows for one tenant. A worker reads that snapshot, streams objects into an archive, and emits a manifest containing the metadata. The archive is then delivered through an authorization check, rather than by making the whole bucket public.
This split keeps binary retrieval efficient while making questions such as “which images belong in this tenant’s export?” answerable without opening every file.
What should the implementation enforce before it writes an image?
Validation should be boring. Boring is good here. A client-provided filename is not a tenant boundary, and a prompt is not an identifier. The server should derive the object key from authenticated tenant context and a server-generated asset ID. Metadata supplied by a model or browser is untrusted input.
The first acceptance test uses 2 tenants with deliberately similar asset names. If one tenant can retrieve the other tenant's bytes, the storage design has failed before performance matters.
That boundary is the feature.
Here is a small storage adapter with a generic interface. The adapter deliberately does not name a cloud API, so the application can test the policy without contacting a provider.
from dataclasses import dataclass from hashlib import sha256 from typing import Protocol from uuid import UUID, uuid4 class BlobStore(Protocol): def put(self, key: str, data: bytes, content_type: str) -> None: ... def get(self, key: str) -> bytes: ... @dataclass(frozen=True) class Asset: asset_id: UUID tenant_id: str object_key: str content_type: str byte_size: int sha256_hex: str prompt_revision: str def save_asset( store: BlobStore, tenant_id: str, image_bytes: bytes, content_type: str, prompt_revision: str, ) -> Asset: if not tenant_id or "/" in tenant_id: raise ValueError("invalid tenant") if content_type not in {"image/png", "image/jpeg", "image/webp"}: raise ValueError("unsupported media type") if not image_bytes: raise ValueError("empty image") asset_id = uuid4() digest = sha256(image_bytes).hexdigest() key = f"tenants/{tenant_id}/assets/{asset_id}" store.put(key, image_bytes, content_type) return Asset( asset_id=asset_id, tenant_id=tenant_id, object_key=key, content_type=content_type, byte_size=len(image_bytes), sha256_hex=digest, prompt_revision=prompt_revision, ) The database insert belongs immediately after the object write, with a retry policy that makes the operation recoverable. In production I would also persist a state such as pending, ready, or quarantined; the exporter should select ready only. If the database write fails after the object write, a reconciliation job can find unreferenced objects and apply retention rules. That is easier to reason about than pretending two independent systems have a shared transaction.
I started by thinking the object key was enough. It was not. The key protects the path, while the metadata row protects the query and audit trail. Those are different failure surfaces.
How can an export prove it stayed inside one tenant?
An export query should require the tenant twice in the application design: once to choose the snapshot and again to load each row. The second check looks repetitive, but it makes a future refactor less dangerous. Never accept a list of arbitrary object keys from the caller. Accept an export ID that the authorization layer has already bound to a tenant.
A minimal repository contract can make that boundary visible in code:
from dataclasses import dataclass from typing import Iterable @dataclass(frozen=True) class ExportItem: object_key: str filename: str content_type: str sha256_hex: str def items_for_export( rows: Iterable[dict], export_tenant_id: str, ) -> list[ExportItem]: items: list[ExportItem] = [] for row in rows: if row["tenant_id"] != export_tenant_id: raise PermissionError("tenant mismatch") if row["state"] != "ready": continue items.append( ExportItem( object_key=row["object_key"], filename=f"{row['asset_id']}.{row['extension']}", content_type=row["content_type"], sha256_hex=row["sha256_hex"], ) ) return items The real query should use a parameterized statement with WHERE tenant_id = ? AND state = 'ready'. Add a uniqueness constraint that prevents the same tenant from recording one asset ID twice, and record the snapshot timestamp or row version. Without a snapshot boundary, an export that runs for several minutes can silently contain a moving target.
The manifest deserves the same care as the images. Include the asset ID, relative filename, media type, byte length, checksum, and prompt revision. Do not put raw prompts or customer data into a public filename just because a ZIP viewer makes names visible. Encrypt the archive in transit and at rest according to the organization’s control requirements, and set an expiration on the download authorization.
Which storage behavior matters more than the brand?
The decision criteria are mostly about boundaries and operations. Ask whether the storage API supports private objects, conditional writes, server-side encryption controls, audit events, lifecycle policies, and a way to stream reads without loading an entire export into application memory. Ask the database question separately: can every asset and export query carry tenant scope in its schema and access layer?
A strong architecture can use many backends. Amazon S3 uses object keys and access policies; Google Cloud Storage uses buckets, objects, and IAM; Azure Blob Storage uses containers, blobs, and authorization controls. Those products differ in policy languages, event systems, and operational defaults. None of them removes the need for an application-level tenant check or a manifest. The portable part is the contract: private storage, scoped keys, authenticated reads, immutable metadata, and observable export jobs.
For a Python RAG or agent pipeline, I would also separate generation cost from storage cost in the eval harness. Test retrieval by tenant, metadata filtering, corrupt-content rejection, duplicate retries, and an export containing zero assets. Token accounting belongs in the generation path; it should not be hidden in an image-storage benchmark. A 3-case happy-path test is not enough. The same evaluations should run against a Node.js adapter if that is the service boundary, because changing languages must not change authorization semantics.
| Approach | Access model | Setup cost | Good fit | Main limitation |
|---|---|---|---|---|
| Managed object storage | Private objects plus IAM or signed authorization | Lower operational setup | Large image collections and asynchronous exports | Policy configuration is easy to mis-scope |
| Self-hosted S3-compatible storage | Application credentials and local policy | Higher operations burden | Teams that need control over deployment and data location | The team owns durability, upgrades, and recovery |
| Private filesystem | Process or host permissions | Small initial setup | Single-node prototypes and temporary processing | It complicates horizontal workers, audit, and durable export access |
The trade-off is explicit: managed storage reduces infrastructure work, while a filesystem can be simpler for a short-lived worker. Neither choice is appropriate as the sole isolation mechanism. If tenant boundaries or audit evidence are the primary concern, the schema and authorization layer must carry that responsibility.
CORS is another boundary, but it is a browser boundary. If a web client reads images directly, configure allowed origins and methods deliberately, and do not confuse a successful preflight with authorization. The browser’s same-origin policy and CORS response headers govern whether browser code can read a response; the storage authorization decision still has to happen on the server.
What should the operational checklist look like?
Before launch, run a tenant-crossing test with two fixtures whose asset names are intentionally similar. Verify that a token for tenant A cannot fetch, list, or export tenant B’s row. Test retries after the object write and after the metadata write. Test a truncated object, a mismatched checksum, an unsupported media type, and a worker restart halfway through an archive.
Watch counts for pending, ready, quarantined, and orphaned objects. Log export IDs, tenant IDs, asset counts, and failure categories, while keeping prompts and customer payloads out of ordinary logs. Alert on repeated authorization failures and on an unusual gap between object writes and metadata rows.
Finally, make deletion a workflow instead of a single database command: mark the row, revoke active export access, delete or quarantine the object according to retention policy, and record the result. The exact retention period is a business and regulatory decision, not a magic storage setting.
The useful rule is compact: derive the key from authenticated tenant context, make the metadata query tenant-scoped, export from an immutable snapshot, and verify every boundary in tests. That design scales across storage implementations because it treats isolation as application behavior with evidence, not as a hopeful naming convention.
Sources
- MDN, “Cross-Origin Resource Sharing (CORS)”: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS
- RFC 9110, “HTTP Semantics”: https://www.rfc-editor.org/rfc/rfc9110
- OWASP, “Authorization Cheat Sheet”: https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html
- NIST, “Security and Privacy Controls for Information Systems and Organizations (SP 800-53 Rev. 5)”: https://csrc.nist.gov/pubs/sp/800/53/r5/upd1/final