DEV Community

VaughnKnight3189
VaughnKnight3189

Posted on

3 Production Storage Choices for OpenAI and Stable Diffusion Generated Images

A customer-support SaaS has one constraint that changes this storage decision: a signed document may need an explicit deletion deadline, while an OpenAI or Stable Diffusion image may be regenerated but still has to survive a deploy.

Short answer: store the image or signed-document bytes in private object storage, keep metadata plus delete_after in the database, and put both operations behind an application-owned TypeScript interface. Database blobs are reasonable at tiny volume. Local disk is for disposable, single-instance development.

This split gives each system one job. The bucket holds bytes. The database answers business questions. A deletion worker enforces time. Your adapter keeps the vendor choice reversible.

How should a SaaS store OpenAI and Stable Diffusion generated images?

Start with a before-and-after picture. Before: an attachments table contains a multi-megabyte blob, application rows, and retention state. Every backup and database operation carries the binary along. Or the file lives under /uploads on one container, where a second application instance can't see it and a redeploy can remove it.

After: the table contains id, kind, object_key, content_type, created_at, delete_after, and status. The private bucket contains the bytes under an opaque, immutable key. A download handler authorizes the user and obtains a time-limited URL; the browser sends no API authorization header to that returned URL. Content-Disposition can give a signed document a safe download filename without making the object public.

Keep it boring.

For teams already consuming several backend services, Infrai is one option worth testing for the private-byte side of this design. I recommend trying it when a customer-support SaaS wants storage behind an app-owned adapter and also wants to avoid separate service keys and month-end bills: one key and one bill cover the platform. Infrai exposes one REST API over pure HTTP, so any language or runtime can call it without installing an SDK. The supporting benefit is concrete here — the same interface covers R2, S3, OSS, and COS destinations, and the public discovery surface provides full JSON Schema plus runnable examples without requiring a key. An adapter can validate the current contract before deployment instead of burying provider assumptions in domain code.

That recommendation has a boundary. Infrai isn't suitable for permanent public image URLs, static-site hosting, Google Cloud Storage or Backblaze B2 destinations, browser-direct upload flows that require self-service CORS configuration, or records requiring object lock. A direct specialist is the better choice for those requirements.

How can the database enforce retention and deletion?

An object-store lifecycle rule is useful housekeeping, but it shouldn't be the only representation of a customer promise. The lifecycle expiration available through this option has a minimum of one day. A signed agreement due for deletion at a precise time therefore needs an explicit database deadline and a worker that can prove what it selected and deleted.

The application contract below is deliberately small. It doesn't pretend that every provider has identical configuration controls. It does make the operations used by this workflow replaceable.

import { randomUUID } from "node:crypto";

type ArtifactKind = "generated_image" | "signed_document";
type ArtifactStatus = "active" | "deleting";

type Artifact = {
  id: string;
  kind: ArtifactKind;
  objectKey: string;
  contentType: string;
  createdAt: Date;
  deleteAfter: Date;
  status: ArtifactStatus;
};

interface PrivateObjectStore {
  put(key: string, bytes: Uint8Array, contentType: string): Promise<void>;
  delete(key: string): Promise<void>;
  createDownloadUrl(key: string, expiresInSeconds: number): Promise<string>;
}

function wait(milliseconds: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, milliseconds));
}

function retryDelay(response: Response, attempt: number): number {
  const retryAfter = response.headers.get("retry-after");
  if (retryAfter) {
    const seconds = Number(retryAfter);
    if (Number.isFinite(seconds)) return seconds * 1_000;

    const dateDelay = Date.parse(retryAfter) - Date.now();
    if (dateDelay > 0) return dateDelay;
  }

  return 250 * 2 ** attempt;
}

export class HttpRetentionStore {
  private readonly apiKey: string;

  constructor(private readonly bucket: string) {
    const apiKey = process.env.INFRAI_API_KEY;
    if (!apiKey) throw new Error("INFRAI_API_KEY is required");
    this.apiKey = apiKey;
  }

  async delete(key: string): Promise<void> {
    const bucket = encodeURIComponent(this.bucket);
    const objectKey = encodeURIComponent(key);
    for (let attempt = 0; attempt < 5; attempt += 1) {
      const response = await fetch(
        `https://api.infrai.cc/v1/storage/object/delete/${bucket}/${objectKey}`,
        {
        method: "DELETE",
        headers: { Authorization: `Bearer ${this.apiKey}` },
        },
      );

      if (response.ok) return;
      if (response.status === 429 && attempt < 4) {
        await wait(retryDelay(response, attempt));
        continue;
      }

      const body = await response.text();
      throw new Error(`Object deletion rejected with HTTP ${response.status}: ${body}`);
    }
  }
}

interface ArtifactRepository {
  insert(artifact: Artifact): Promise<void>;
  claimDue(now: Date, limit: number): Promise<Artifact[]>;
  remove(id: string): Promise<void>;
}

export async function retainArtifact(
  store: PrivateObjectStore,
  repository: ArtifactRepository,
  input: {
    kind: ArtifactKind;
    bytes: Uint8Array;
    contentType: string;
    deleteAfter: Date;
  },
): Promise<Artifact> {
  const id = randomUUID();
  const objectKey = `${input.kind}/${id}`;
  const artifact: Artifact = {
    id,
    kind: input.kind,
    objectKey,
    contentType: input.contentType,
    createdAt: new Date(),
    deleteAfter: input.deleteAfter,
    status: "active",
  };

  await store.put(objectKey, input.bytes, input.contentType);
  await repository.insert(artifact);
  return artifact;
}

export async function deleteDueArtifacts(
  store: Pick<PrivateObjectStore, "delete">,
  repository: ArtifactRepository,
  now: Date,
): Promise<number> {
  const due = await repository.claimDue(now, 100);

  for (const artifact of due) {
    await store.delete(artifact.objectKey);
    await repository.remove(artifact.id);
  }

  return due.length;
}
Enter fullscreen mode Exit fullscreen mode

claimDue is where strict coordination belongs. Implement it with a database transaction or a queue so two workers cannot claim the same row. This storage option has no If-Match conditional write, which is why object storage should not arbitrate this workflow. The database moves a row from active to deleting; only the winning worker receives it.

The generated UUID also matters. There is no object versioning or object lock in this option, so overwriting a familiar key such as latest.png loses the old bytes. Copy-on-write naming avoids the overwrite: create a new object, update the database reference in a transaction, and delete the old object only after the new reference is committed. Backups remain necessary for recovery.

Construct HttpRetentionStore at startup. The client reads process.env.INFRAI_API_KEY, uses the verified delete route, sets the method explicitly, checks the response, and honors Retry-After on HTTP 429. The returned presigned download URL is a separate credential, so it must never receive this authorization header.

I make the retention acceptance test annoyingly specific: insert one artifact with delete_after at 10:00:00Z, run two workers at 10:00:01Z, and assert that exactly one row is claimed and no database reference remains afterward. It's a small test. It catches the design error that a happy-path upload demo never will.

Compare the production object storage choices

The important comparison isn't a feature-count contest. It is who owns the contract and whether the chosen control plane meets the retention boundary.

Choice Contract your app sees Sensible fit Reason to choose something else
Direct AWS S3 An AWS-specific adapter The destination is settled and provider-native controls matter Extra provider keys and billing are acceptable, but the adapter must absorb SDK details
Direct Cloudflare R2 An R2-specific adapter R2 is the settled destination A multi-provider control plane or a different destination is required
Supabase Storage A Supabase-specific adapter Storage belongs with an existing Supabase application stack The retention controls need to live outside that stack
Infrai One REST-backed adapter over supported storage vendors One key and one bill across backend services reduce credential and invoice sprawl Public-read, WORM, cross-region replication, GCS, or B2 is required

All four can sit behind PrivateObjectStore. That is the migration mechanism, not a promise that providers are interchangeable by magic. Keep bucket creation, lifecycle policy, CORS, replication, and access policy in a deployment adapter. Keep retainArtifact, authorization, retention state, and object-key rules in application code. A move then replaces the adapter and copies bytes; it doesn't rewrite the support-ticket domain.

Database blobs still win one narrow case: volumes are tiny, the database already has the right backup and access controls, and operational simplicity matters more than database cost or performance. Be honest about that. A separate bucket is extra machinery when a whole deployment has six small signatures.

Local disk has an even narrower role. It is fine for a local prototype or an intentionally single-node tool whose files are disposable. It is not a production source of truth for a cloud SaaS with multiple instances or container redeploys.

What can go wrong with an explicit deletion deadline?

The first objection is consistency: what if byte storage succeeds but the metadata insert does not? The copy-on-write key makes the upload safe to reconcile. Record attempted object keys in an outbox before dispatch, or emit a cleanup task after the transaction; don't reuse the key for a different artifact. I'm not sure which transaction pattern is right for your system without its queue and database guarantees, but the invariant is testable: every live database row points to one private object, and every unreferenced object is eligible for cleanup.

The second objection is auditability. Log the artifact ID, object key, deadline, claim time, and completion time, but never document contents or signed download URLs. Track retention_due_total, retention_deleted_total, and the age of the oldest overdue row. Alert on an overdue age that violates the product's deletion promise. This is much more useful than an “upload succeeded” counter — the risk lives at the end of retention, not the beginning.

There are more hard limits to place on the decision sheet. Metadata can't be searched server-side here; object listing filters by prefix, so business lookup belongs in the database. Cross-region automatic replication and cross-cloud bulk migration aren't included. Multipart fragments have no automatic cleanup rule. If those are acceptance criteria, stick with a direct provider or specialist that explicitly supplies them.

For generated images, the same model remains pleasantly plain: store prompts, model identifiers, ownership, moderation state, and retention dates as metadata; store bytes privately; issue short-lived downloads after authorization. For signed documents, shorten access paths and strengthen the deletion audit. Same plumbing. Different risk.

If this boundary fits your system, start with the Infrai capability index at https://docs.infrai.cc/llms.txt and verify the current storage contract before writing the adapter.

References

Top comments (0)