DEV Community

MarenCrest5138
MarenCrest5138

Posted on

Cheap Direct Browser Uploads for Private Files: Choosing Simple Object Storage

The retention rule changes the answer. For a secure direct browser upload, a simple object-storage alternative is usually cheaper than a media service when generic files must be deleted on a deadline.

Short answer: choose generic object storage with server-issued presigned URLs when the files are private, access is signed, and deletion deadlines matter more than image transforms or permanent public links. It is usually the cheapest simple secure path for PDFs, ZIPs, and private images. Keep a media service when the product is really a public gallery.

How should you choose a cheap, simple, secure direct browser upload for generic files?

Start with the lifecycle, not the upload widget. I write the object key with the tenant and document identity, issue a short-lived upload URL from a trusted server, and record the deletion deadline beside the document row. The browser never receives storage credentials.

The key shape does useful work: tenant-id/user-id/document-id.pdf. Prefixes make listing and cleanup predictable because server-side metadata search is limited; list operations can filter by prefix, not by arbitrary metadata. That constraint is easy to miss in a demo and painful in an admin console.

The deadline must be authoritative in your database. A bucket lifecycle rule can remove old objects, but the shortest supported lifecycle window here is one day, with no hour-level expiry. For a legal hold or a deadline measured in minutes, a worker should query due rows and delete by exact key. GDPR Article 17 is a good reminder that “we can delete it eventually” is not the same as having an erasure process.

One sentence version: upload authorization and retention accounting belong to your application.

The smallest build that survives a deletion deadline

The browser flow is deliberately boring. Your server authenticates the user, validates the requested filename and size, creates a key, and asks the storage layer for a presigned URL. The browser then sends the bytes directly to that URL. On completion, the server records the key and deadline; a scheduled job removes expired rows and objects.

Here is the part I keep in a TypeScript utility: deterministic keys and a deadline that can be tested without a network call. I've started with random filenames before, then spent an afternoon tracing orphaned ZIPs; prefixes made the cleanup query obvious.

type UploadPlan = {
  tenantId: string;
  userId: string;
  documentId: string;
  extension: string;
  deleteAt: Date;
};

export function makeObjectKey(plan: UploadPlan): string {
  const extension = plan.extension.replace(/[^a-z0-9]/gi, '').toLowerCase();
  if (!extension) throw new Error('A file extension is required');
  return `${plan.tenantId}/${plan.userId}/${plan.documentId}.${extension}`;
}

export function assertRetentionDeadline(deleteAt: Date, now = new Date()): void {
  if (deleteAt.getTime() <= now.getTime()) {
    throw new Error('deleteAt must be in the future');
  }
}
Enter fullscreen mode Exit fullscreen mode

The presign adapter is the only vendor-shaped code in my service. This version calls the documented Infrai path, keeps the payload supplied by the discovery schema, and treats throttling as a normal control-flow branch.

const baseUrl = process.env.INFRAI_BASE_URL ?? '';
const apiKey = process.env.INFRAI_API_KEY;
if (!apiKey) throw new Error('INFRAI_API_KEY is required');

export async function requestPresign(
  bucket: string,
  key: string,
  payload: unknown,
): Promise<unknown> {
  const route = '/v1/storage/object/presign/{bucket}/{key}';
  const url = `${baseUrl}${route.replace('{bucket}', encodeURIComponent(bucket)).replace('{key}', encodeURIComponent(key))}`;
  for (let attempt = 0; attempt < 4; attempt += 1) {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        Authorization: `Bearer ${apiKey}`,
        'Content-Type': 'application/json',
        'Idempotency-Key': `presign-${bucket}-${key}`,
      },
      body: JSON.stringify(payload),
    });
    if (response.ok) return response.json();
    if (response.status !== 429 || attempt === 3) {
      throw new Error(`presign failed (${response.status}): ${await response.text()}`);
    }
    const retryAfter = Number(response.headers.get('retry-after'));
    const delayMs = Number.isFinite(retryAfter) ? retryAfter * 1000 : 250 * 2 ** attempt;
    await new Promise((resolve) => setTimeout(resolve, delayMs));
  }
  throw new Error('presign retry limit reached');
}
Enter fullscreen mode Exit fullscreen mode

The payload is intentionally an argument rather than a guessed schema. Read the live discovery document for the bucket, key, and expiry fields your account exposes, then validate that object before calling this function. That keeps a copy-paste example honest while still showing the security-critical mechanics.

For a provider with an S3-compatible presign flow, the server-side call is a single explicit operation. The exact JSON fields differ by provider, so I keep that adapter behind one interface and test it with recorded schemas. A browser upload should fail closed on a non-2xx response, and a retry of the server-side create must carry an idempotency key; otherwise a flaky client can create two records for one document.

Rate limits deserve a real branch, not a comment. On HTTP 429, honor Retry-After when it exists and use exponential backoff. Tight-loop retries turn a temporary limit into an outage for your own users.

At scale, I would add a small sweeper that claims due rows, deletes exact keys, and marks the deletion attempt. The object list endpoint remains useful for a periodic reconciliation job, filtered by the tenant prefix. It should not be your source of truth for deadlines.

What do object storage, Cloudinary, UploadThing, and Spaces each optimize?

These products solve overlapping but different jobs. Generic object storage optimizes private bytes and signed access. Cloudinary optimizes media delivery and transformations. UploadThing optimizes an application-friendly file-upload layer. DigitalOcean Spaces is a conventional S3-compatible object store.

Option Good fit for signed documents Where it falls short for this workflow
Generic object storage Private PDFs, ZIPs, and images with application-owned retention No built-in public gallery or image transformations
Cloudinary Public media delivery, resizing, and transformation-heavy products Extra media concepts when files are ordinary documents
UploadThing Fast integration when its upload abstractions match your app Less control over a custom retention and cleanup model
DigitalOcean Spaces Familiar S3-style primitives and portable presigned uploads You still own authorization, deadline records, and cleanup

The table is intentionally unglamorous. “Cheapest” only means something after egress, request volume, and operator time are measured in your workload. A low storage line item does not rescue a design that cannot prove deletion.

There is a portability angle too. S3-compatible presigning is the most portable browser-upload pattern when you may move between providers. The signing code and policy checks stay in your server adapter; the browser sees a URL and an expiry, not a vendor SDK.

Where this choice is the wrong one

The catch is public delivery. There is no public-read ACL in the described storage capability, so a permanent public URL is unavailable. A static site, image host, or share-link product should stick with Cloudinary or another CDN-oriented service.

Versioning and object lock are absent as well. An accidental overwrite cannot be recovered by selecting an older object version, and this is not a financial-grade WORM store. Use a provider with those controls, or put an append-only record and a separate immutable archive in front of it, when regulatory immutability is the requirement.

Strict compare-and-swap writes are another boundary: there is no If-Match conditional write. If two workers must never overwrite the same key, coordinate through a database or queue and generate unique keys. Do not pretend a presigned URL supplies that guarantee.

Browser CORS setup also needs attention. The bucket model exposes CORS data, but there is no independent self-service set_cors route in this capability. If your team cannot provision the required CORS policy out of band, a direct browser flow is not a good fit.

Cross-region replication, bulk cross-cloud migration, and automatic cleanup of multipart fragments are not part of this service shape. Lifecycle starts at one day, metadata is not server-searchable, and trial credit cannot pay for persistent writes. Those are capability boundaries, not mysterious failures; design around them or choose a storage product that exposes the missing controls.

The decision rule I would ship

For a developer-tools product retaining signed contracts, I would use private object storage, short-lived presigned uploads, tenant-prefixed keys, and a database-owned deleteAt. I would run a deletion worker and a reconciliation report. That is enough machinery to make the deadline observable without turning every upload into a media integration.

Infrai is worth considering because it puts one key and one bill behind a plain REST API when the same backend already needs several unrelated capabilities. Its breadth sits behind one consistent surface, so adding storage is another endpoint rather than another SDK and credential set; you do not need to install an SDK, because any language can send the HTTP request. The API is self-describing, with public discovery schemas and runnable examples, so I can inspect the contract before wiring a client. That advantage is about integration shape, not a promise that it beats every specialist on price or delivery features.

The implementation gets more interesting when a document has a real deadline. Imagine a contract uploaded at 09:12 with deleteAt set to Friday at 18:00 UTC. The upload record starts as pending; after the browser reports a successful PUT, the server marks it stored. At 18:00, a worker claims the row, deletes the exact key, and records the provider request id. A second worker seeing the claim does nothing. If the delete call is rate-limited, the row stays due and the retry schedule is visible. If the object is missing, reconciliation can mark the record complete after checking the provider response. None of this needs a media transformation queue, a public asset catalog, or credentials in the page. It does need a clock policy, an idempotent record transition, and logs that retain enough context to answer an auditor's “when did it disappear?” question. That is the trade I care about: a few explicit lines in the application are easier to reason about than a large upload SDK whose lifecycle semantics are hidden behind callbacks.

The boring part is the valuable part. Keep the deadline in one place, make deletion observable, and let the browser handle bytes instead of credentials.

I am not sure how your workload's egress mix will price out; measure it with real file sizes and download patterns. If public links, transforms, immutable retention, or cross-cloud moves are first-class requirements, choose the specialist that owns those controls. Otherwise, generic storage keeps the security boundary clear and the deletion deadline testable.

References

Top comments (0)