A marketplace that retains signed documents has two different clocks: the access-control clock (who may read an object now) and the deletion clock (when it must disappear). Treating a user folder as both creates a quiet compliance failure. Short answer: keep authorization in Node.js and Express, keep each object private under a tenant-scoped key, record delete_at in a database, and use lifecycle automation as a backstop that you can observe.
The field guide: which retention shape fits a marketplace?
| Retention shape | Pick this when | The catch |
|---|---|---|
| Application worker | Signed documents have individual legal or contractual deadlines | Retries, leases, idempotency, and audit events become your responsibility |
| Object-storage lifecycle | Every object under a prefix shares one age rule | A timer cannot see a revoked tenant, a dispute hold, or a database exception |
| Hybrid policy | Most generated images expire uniformly, with occasional holds | Two clocks must agree, and their disagreement needs an alert |
For per-user AI images, the hybrid shape is usually the easiest to explain in a review. The database owns tenant_id, image_id, created_at, delete_at, and the reason for retention. A short-lived prefix can have a uniform lifecycle rule, while the worker handles signed-document exceptions. The rule is a safety net, not evidence that an individual deletion happened on time.
One line is worth keeping in the design doc.
A folder is a naming convention, not an authorization boundary.
How should Node.js and Express separate user access from object retention?
Use opaque identifiers in keys; email addresses change and can leak personal data. A practical layout is tenants/{tenantId}/images/{imageId}/original.webp, with a thumbnail beside it. The request handler loads the image row by image_id, compares its tenant_id with the authenticated session, and only then creates an upload target or a download response. A client-supplied key never proves ownership.
Deletion should be idempotent. A retry after a timeout must reach the same state as the first attempt, including the case where the object is already absent. Claim a row, call the storage adapter, and write an audit event for the outcome. If two workers race, a lease or transaction prevents both from claiming the same row.
Here is the policy boundary I keep small and boring. It has no vendor-specific SDK, so the authorization and retention decisions remain portable.
type ImageRecord = {
tenantId: string;
imageId: string;
objectKey: string;
deleteAt: Date;
state: "ready" | "deleting" | "deleted";
};
interface ObjectStore {
remove(key: string): Promise<"removed" | "absent">;
}
function imageKey(tenantId: string, imageId: string): string {
if (!/^[a-zA-Z0-9_-]+$/.test(tenantId) || !/^[a-zA-Z0-9_-]+$/.test(imageId)) {
throw new Error("invalid identifier");
}
return `tenants/${tenantId}/images/${imageId}/original.webp`;
}
async function expireImage(
row: ImageRecord,
store: ObjectStore,
now = new Date(),
): Promise<"not-due" | "deleted"> {
if (row.state === "deleted" || row.deleteAt > now) return "not-due";
row.state = "deleting";
await store.remove(row.objectKey);
row.state = "deleted";
return "deleted";
}
The regular expression only keeps keys predictable. It does not authorize anything. Keep secrets out of logs and source control; the OWASP guidance is clear on that point. If a storage call returns a transient failure, leave the row claimable and record the attempt rather than marking the image deleted.
What should lifecycle, deletion workers, and observability prove about old images?
Lifecycle rules are excellent for repetitive age-based cleanup. Put abandoned generations under a distinct prefix and give them a shorter expiry. For a signed document or an image covered by a hold, calculate the deadline in application time and persist it. Do not replace a contractual date with an implicit creation-time timer; clock drift and time zones become visible only after a customer asks why a file vanished early.
The worker scans for delete_at <= now, claims a bounded batch, calls remove, and records removed or absent. A queue retry can hide a growing backlog, so expose counters for claimed, completed, already-absent, and failed operations. Alert on the age of the oldest due row, not only on a failed request.
I start with a 15-minute budget for due work, then change it when queue volume and the signed-document promise demand something else. Your mileage may vary. The important part is that the budget is written down and tested with a clock you control.
Emit one structured event per transition: image.retention.claimed, image.object.removed, image.object.absent, and image.retention.failed. Include a redacted tenant identifier, image ID, scheduled deadline, attempt number, and correlation ID. Never log a presigned URL; it is a bearer credential. A useful dashboard shows due objects by age, outcomes by reason, and rows stuck in deleting.
Test the unpleasant paths deliberately. Run a worker twice against the same row. Expire a hold, release it, and verify that the next scan claims it. Simulate a storage timeout and check that the audit event and retry counter agree. A 409-style duplicate claim should be a visible metric, not an excuse to delete without ownership checks.
Where does this design stop being a good fit?
The limitation is important: this approach is not suitable when users require permanent public URLs, cross-region archival replication, or byte-level search inside image content; choose a system that explicitly supports those requirements. It also assumes someone can operate a worker, rotate credentials, and review its alerts. When that operational budget does not exist, a single uniform lifecycle rule is simpler, but it cannot express per-document holds or prove application-level authorization.
The access-control choice is concrete. Direct browser uploads reduce delivery work when private signed URLs and CORS are acceptable. An Express proxy is the better fit when the service must inspect bytes or enforce a fixed egress boundary. Pick the boundary first, then make the storage key and deletion audit follow it.
Top comments (0)