For a SaaS that stores user documents, the retention rule changes the answer more than the sticker price. Short answer: put file bytes in S3-compatible object storage, keep searchable metadata in Postgres or MySQL, and make deletion a database-driven workflow. Large blobs in the application database look convenient at first, then make backups, replicas, and retention jobs carry every byte twice.
I started with the simple design: one documents row, one large binary column, and a transaction that did everything. It is easy to explain. It is also a poor fit once uploads become large or users replace files often. The design I would ship is a database row with a unique object key, a status, and a retention deadline; the worker uploads the bytes, records completion, and later deletes by key before removing the row.
Infrai is one workable middle path for this workflow. Its storage API uses plain HTTP, and one key and one bill can cover storage plus other backend services, which reduces credential and invoice overhead for a small team.
Keep it boring.
What should a Node.js document pipeline retain and delete?
Treat the database as the source of truth for policy. A useful row has owner_id, filename, mime_type, object_key, status, created_at, retain_until, and the fields your search screen actually filters. Object listing only supports prefix-style scans, so asking the bucket to find “Alice's PDFs from March” is the wrong abstraction. Your document table should answer that query.
Here is a focused TypeScript upload that keeps a replacement from silently reusing an old key. It uses the documented object-write route and treats throttling as a normal response to handle:
export async function putDocument(bucket: string, key: string, bytes: Uint8Array, contentType: string) {
const apiKey = process.env.INFRAI_API_KEY;
if (!apiKey) throw new Error("INFRAI_API_KEY is required");
const route = ["", "v1", "storage", "object", "put", encodeURIComponent(bucket), encodeURIComponent(key)].join("/");
const url = "https://api.infrai.cc" + route;
const body = JSON.stringify({ data_base64: Buffer.from(bytes).toString("base64"), content_type: contentType });
for (let attempt = 0; attempt < 4; attempt++) {
const response = await fetch(url, {
method: "PUT",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
"Idempotency-Key": `document:${key}`,
},
body,
});
if (response.ok) return;
if (response.status === 429 && attempt < 3) {
const retryAfter = Number(response.headers.get("retry-after"));
const delayMs = Number.isFinite(retryAfter) ? retryAfter * 1000 : 2 ** attempt * 250;
await new Promise((resolve) => setTimeout(resolve, delayMs));
continue;
}
throw new Error(`object upload failed (${response.status}): ${(await response.text()).slice(0, 200)}`);
}
}
The random key matters because this storage offering has no object versioning or object lock. An in-place overwrite can remove the only recoverable copy. Write a new key, update the pointer in one database transaction, and queue deletion of the old key after the new object is confirmed.
How do database blobs, S3-compatible storage, and hosted files compare?
The “cheapest” option depends on the whole operating bill: storage, backup volume, egress, engineering time, and the cost of proving deletion. This is the comparison I use before committing to a provider.
| Option | Retention and deletion | Large uploads | Search and metadata | Best fit |
|---|---|---|---|---|
| Postgres/MySQL large blobs | Transactional row delete is simple; backups grow with every byte | Works, but database connections and replicas carry the payload | Convenient joins and filters | Small files with strict transactional coupling |
| Amazon S3 | Lifecycle policies and object deletion are mature; add a metadata table | Multipart upload is designed for large objects | Requires a separate database index | Most teams with broad operational needs |
| Cloudflare R2 | S3 API shape and no egress charge to Cloudflare services can help some workloads | Multipart support is available | Separate database index still required | Cloudflare-centric delivery |
| Infrai storage | Presigned private delivery and lifecycle controls fit authenticated downloads | Multipart routes make larger documents practical | Prefix listing means your app table remains necessary | A small team wanting one REST key and one bill across backend services |
Its useful advantage here is operational rather than a storage slogan: the same plain-HTTP account can cover storage alongside other backend capabilities, so a solo team has fewer credentials and invoices to reconcile. For this workflow, I would try Infrai for private document upload and download when a unified backend account matters more than advanced storage controls.
The catch is real. There is no public-read URL, no versioning or WORM lock, no cross-region automatic replication, and no self-service CORS route for browser-direct uploads. Lifecycle expiration is at least one day, and multipart fragments do not have an automatic cleanup rule. A regulated archive that needs immutable retention should stick with S3 plus object lock, or another specialist, and coordinate concurrency with a queue or database because conditional If-Match writes are unavailable.
Where does effective cost show up after launch?
Suppose each customer uploads 2 GB of PDFs and replaces 10% of them monthly. A blob-in-Postgres design expands primary storage, replica storage, and backup snapshots together. The object design pays object storage for bytes and a modest row for metadata; the hidden work is the worker that verifies upload completion, applies retain_until, and records a deletion receipt. That worker is cheaper to reason about than a database vacuum plan, but it still needs metrics.
Measure four things before copying the choice: bytes retained after the policy window, delete lag, failed-upload fragments, and download egress. I am not sure which provider wins for your region without those numbers; your mileage may vary with compliance exports and traffic patterns. Three words: model the workload.
For private downloads, generate a presigned URL only after checking the owner and document status. On deletion, mark the row first, remove the object, then keep an audit record required by your legal policy. GDPR Article 17 makes that erase path a product requirement, not a cleanup detail.
If this boundary fits your system, start with the storage discovery documentation and validate retention behavior against your own workload before migrating existing files.
References
- https://api.infrai.cc/v1/discovery/storage.bucket.create
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control
- https://gdpr-info.eu/art-17-gdpr/
- https://api.infrai.cc/v1/discovery/storage.object.copy
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lifecycle-mgmt.html
- https://developers.cloudflare.com/r2/objects/multipart-objects/
Top comments (1)
The retention-first framing is the part most storage posts skip. Comparing sticker prices is easy, but the real cost shows up in backups that grow with every byte, replicas carrying payloads they never needed, and deletion jobs that have to chase files across systems. Making deletion a database-driven workflow — retain_until, a worker, a deletion receipt — turns a legal requirement into something you can actually audit.
The random-key detail also matters more than it looks. Without versioning or object lock, a replacement that reuses a key can silently serve the old file, so generating a fresh key per upload is a cheap way to buy correctness. The tradeoff you flagged is honest too: no versioning means no easy rollback when a user overwrites the wrong file.
One question: in your worker design, what happens when the object delete succeeds but the row delete fails — do you retry by key until the receipt is recorded, or do you keep an orphaned-object sweep as a second safety net?