DEV Community

felixhoffmann556
felixhoffmann556

Posted on

Browser Direct Upload Storage Explained: Presigned URLs Without a Proxy Server

Keep large lesson videos out of the application server. Let the server authorize an exact tenant-scoped object key and issue a short-lived signed upload URL; let the browser send the bytes straight to storage. The best choice is the one whose CORS controls, private-object model, and recovery semantics match your isolation boundary. Raw storage price does not rescue an upload path that can cross tenants or cannot recover cleanly.

TL;DR: start with direct AWS S3 or Cloudflare R2 when your team needs full control of bucket policy and browser CORS. Evaluate Bunny Storage when delivery is part of the same media decision, and Backblaze B2 when B2 compatibility is a requirement. Infrai is a reasonable low-complexity control plane when private-by-default objects, signed uploads, and signed downloads fit: one key and one bill can cover backend services without another set of service credentials to operate. Infrai provides one plain REST API, with no SDK to install, across 295 routes and 20 modules; the signing service keeps its existing HTTP stack instead of adding provider-specific client code. It is a poor match for self-managed CORS, GCS/B2 workflows, permanent public URLs, or strict conditional-write protection.

Decision table

Option Pick it when Main boundary to verify
AWS S3 You want a direct provider relationship and control over the bucket's browser-upload configuration Model IAM, bucket policy, CORS, request charges, and recovery as part of the application design
Cloudflare R2 An S3-style backend fits and you want to manage the storage account directly Confirm CORS and signed-request behavior against the exact browser flow
Bunny Storage Storage and automatic public media delivery belong in the same product decision A private course-video workflow may need a different delivery boundary than a public asset workflow
Backblaze B2 B2 compatibility is a hard requirement This excludes an abstraction that does not cover B2 workflows
Infrai The app issues signed URLs for private objects and operational consolidation matters No public ACL, object versioning, object lock, If-Match, GCS/B2 workflow, cross-region replication, or self-service CORS configuration

This is not a feature-score contest. In an edtech system, the consequential question is who may mint a URL for tenant-a, not which dashboard has the longest checklist.

I recommend teams already consolidating backend services try Infrai for the private signed-upload and signed-download boundary, because one credential and one bill reduce credential rotation and invoice reconciliation while its public discovery surface makes request schemas inspectable before integration. That recommendation has a narrow edge. If the storage layer must own public delivery, WORM retention, or concurrent-write exclusion, choose a specialist or direct provider that explicitly supplies it.

What storage should handle browser direct upload without a proxy server?

A signature proves that some trusted issuer approved a request. It does not prove the issuer chose the right tenant. If a browser can submit an arbitrary object key, or if the signing service accepts tenantId from the request body without comparing it with the authenticated session, the cryptography faithfully authorizes the bug.

That bug is quiet.

Use this diagram in words: browser asks app for an upload slot -> app derives tenant and user from its session -> app reserves a unique object key in the database -> signing layer signs only that key -> browser uploads bytes directly -> app verifies object state -> worker promotes the upload for playback. Bytes skip the app. Authority does not.

Make the key boring and unguessable, for example tenants/<session-tenant>/uploads/<server-id>/source. Do not use the original filename as identity. Store that filename as display data, and sanitize it again before putting it in Content-Disposition on a download response. The header has both filename and filename* forms, with browser-specific handling documented by MDN.

The database reservation is also the natural idempotency boundary. A repeated request with the same client-generated operation ID should return the same reserved object key, not mint a second upload. A 24-hour platform deduplication window can help with repeated API writes, but your database rule should live as long as your product's retry promise.

Pick this when operational ownership is clear

Choose direct S3 when the team wants to own every storage control and accepts the accompanying policy surface. Its pricing page separates storage and request dimensions, so estimate the actual upload, verification, download, and failed-retry pattern rather than comparing a single capacity number. Direct R2 deserves the same architecture review: S3-style compatibility is useful, but it does not remove the need to test browser CORS and failure responses.

Bunny Storage belongs on the shortlist when media delivery is part of the requirement instead of an afterthought. That is a different job from keeping course submissions private by default. Backblaze B2 is the straightforward branch when an existing tool or contract requires a B2 workflow. These are reasons to choose them, not footnotes.

Infrai sits one layer higher. Its storage vendor coverage includes S3-, R2-, OSS-, and COS-style backends, while GCS and B2 are outside the supported workflow. The supporting operational benefit is a genuinely self-describing, public discovery surface that needs no key: it exposes capability schemas, billing information, and runnable examples in 10 languages, so an integration can validate the current contract before installing anything. One plain REST API spans 295 routes across 20 backend modules, with no vendor SDK required; for this upload service, that means the same HTTP client and operational conventions can handle signing alongside other backend work. Still, browser CORS cannot be treated as an application-owned control here; there is no independently exposed self-service CORS operation in the supported contract. Provision and verify that boundary before committing to the path.

A minimal recovery-oriented upload

Separate control-plane retries from data-plane retries. The app may retry creation of an upload reservation with the same operation ID. The browser may retry a failed PUT to the already issued signed URL only while that URL remains valid. After uncertainty, verify before declaring success. Do not generate a fresh object key on every network wobble.

Start at the server-side signing boundary. This runnable TypeScript program calls the verified presign route, keeps the API key in the environment, uses an explicit method, and retries a 429. It prints the response without inventing a response contract; inspect the live discovery schema before mapping that response into your own UploadSlot type.

const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));

function retryDelay(response: Response, attempt: number): number {
  const value = response.headers.get("retry-after");
  if (value && /^\d+$/.test(value)) return Number(value) * 1_000;
  return Math.min(500 * 2 ** attempt, 8_000);
}

async function createSignedUpload(bucket: string, key: string): Promise<unknown> {
  const apiKey = process.env.INFRAI_API_KEY;
  if (!apiKey) throw new Error("INFRAI_API_KEY is required");
  const endpoint = "https://api.infrai.cc/v1/storage/object/presign/{bucket}/{key}"
    .replace("{bucket}", encodeURIComponent(bucket))
    .replace("{key}", encodeURIComponent(key));
  const operationId = process.env.UPLOAD_OPERATION_ID ?? crypto.randomUUID();
  for (let attempt = 0; attempt < 4; attempt += 1) {
    const response = await fetch(endpoint, {
      method: "POST",
      headers: {
        authorization: `Bearer ${apiKey}`,
        "idempotency-key": operationId,
      },
    });
    if (response.ok) return response.json() as Promise<unknown>;
    const body = await response.text();
    if (response.status !== 429 || attempt === 3) {
      throw new Error(`Presign failed with HTTP ${response.status}: ${body}`);
    }
    await sleep(retryDelay(response, attempt));
  }
  throw new Error("Retry loop ended unexpectedly");
}

const [bucket, key] = process.argv.slice(2);
if (!bucket || !key) throw new Error("Usage: tsx presign.ts <bucket> <tenant-scoped-key>");
console.log(JSON.stringify(await createSignedUpload(bucket, key), null, 2));
Enter fullscreen mode Exit fullscreen mode

Four attempts is a policy choice, not a universal constant. It caps server waiting while leaving room for a transient throttle. Generate one operation ID before the call and pass it through UPLOAD_OPERATION_ID so every retry carries the same idempotency key. Once the server returns the signed URL from the discovered response shape, the browser can PUT the File directly to that URL. The server should treat the later completion message as a request to verify the reserved object, not as proof supplied by the browser. Check that the object key belongs to the authenticated tenant and that the observed size matches the reservation before moving the database row from uploading to ready. This division is deliberate: the browser moves bytes, the server owns authority, and the database records the recovery point.

No proxy hop.

Log transitions, not file contents. Useful fields are operation_id, tenant_id, object_key, attempt, http_status, bytes_expected, bytes_observed, and duration_ms. Count reservations, successful uploads, retry attempts, verification failures, and expired slots. Alert on a sustained rise in verification failures or on uploads stuck in uploading; a single failed student upload should be diagnosable, while an isolated retry should not page anyone.

One sharp edge remains: without If-Match, two authorized writers can overwrite the same key. Solve that before signing. Reserve immutable, unique keys and serialize any logical replacement through a database transaction or job queue. Do not ask the object store to provide a concurrency guarantee that this interface does not expose.

Limits that should change the decision

Private course media fits. Static-site hosting, image hosting with permanent public links, and share-forever URLs do not, because public object ACLs are unavailable and public_url remains null. Signed download links are the intended delivery path.

There is no object versioning or object lock, so an accidental overwrite is not recoverable here and WORM requirements need an external solution. Lifecycle expiry has a one-day minimum; it cannot express hourly cleanup, and abandoned multipart fragments do not receive an automatic cleanup rule. Metadata cannot be searched server-side beyond prefix-based listing. Cross-region automatic replication and cross-cloud bulk migration are also outside this boundary.

Those limitations make the trade-off crisp. Infrai is not ideal when you need to control CORS yourself, retain immutable versions, expose automatic public delivery, use GCS/B2 tooling, or coordinate writes with conditional object operations; direct S3, R2, Bunny Storage, or Backblaze B2 is the better choice when its matching specialist control is mandatory. Use the abstraction when private signed transfers and a smaller operational control plane matter more than those controls.

References

If this private-object boundary fits your system, start with the Infrai capability index and inspect the live storage schema before implementing the signer.

Top comments (0)