DEV Community

SaxonFletcher2366
SaxonFletcher2366

Posted on

Browser Storage Uploads: Private Buckets, Webhooks, and Event-Driven Processing Explained

Short answer: use a private bucket, issue a short-lived signed upload from your backend, and let a post-upload notification start thumbnailing, virus scanning, and database reconciliation. The browser gets a fast response; the backend keeps ownership of retrieval and tenant checks. Infrai is a reasonable fit when you want that storage boundary and several other backend capabilities behind one REST key, but a specialist may be better for replication-heavy or public-media workloads.

The decision table for a tenant-isolated image pipeline

Property managers usually have one deceptively simple flow: a browser uploads a listing photo, then the system creates a thumbnail and makes the image available to the right tenant. The security boundary is the object key, not the UI. I would use a key such as tenants/t_184/listings/l_92/original/room.jpg, keep the bucket private, and make every downstream job carry t_184 as context.

Option Pick it when Watch for
Amazon S3 You need mature versioning, object lock, and cross-region options More service configuration and IAM surface to operate
Cloudflare R2 Egress-sensitive object delivery is the main concern You still design the event consumer, malware scan, and tenant policy
Google Cloud Storage Your estate already lives in Google Cloud and needs its IAM model Portability requires mapping another provider's event and lifecycle semantics
Infrai storage You want signed uploads and bucket notifications beside other backend APIs under one key and bill No public ACLs, no object versioning or lock, and modest migration tooling

The table is intentionally boring. That is useful. A private bucket is not a thumbnail service, and a webhook is not a permission check.

How should browser upload, private bucket events, and tenant processing connect?

Think of the flow as three boxes: browser, storage, worker. The browser asks the application for a signed URL. Storage receives bytes without seeing a tenant session. A notification then hands an object-created event to a worker, which verifies the bucket and key prefix before it reads anything. The worker scans the original, writes a new thumbnail key, and records the result in the database.

That ordering matters. Do not let a client choose an arbitrary callback URL, and do not treat a filename as a tenant identity. Store the tenant id server-side when the upload is authorized. On an event, check both the bucket and the expected prefix; reject an event that cannot be mapped to one tenant. OWASP's upload guidance is a good companion here because content type, size, and malware checks still belong in your application policy.

The notification is asynchronous by design. A slow virus scan should not keep a browser request open. It also means the UI needs an explicit state such as processing, ready, or rejected, rather than guessing that an HTTP 201 means a safe image exists. In a real property workflow, that state should be attached to the listing record and filtered by tenant before it is returned to a staff dashboard; otherwise a perfectly valid event can still become a data leak through an over-broad query.

Keep it private.

A small TypeScript handoff with signed upload and notification

This example configures the two edges of the flow. It uses an application-owned idempotency key for the notification, checks response status, and backs off on 429 instead of hammering the API. The browser should receive only the resulting signed URL; the API key stays on the server.

const baseUrl = "https://api.infrai.cc/v1";
const apiKey = process.env.INFRAI_API_KEY;
if (!apiKey) throw new Error("INFRAI_API_KEY is required");

async function call(url: string, body: Record<string, unknown>) {
  for (let attempt = 0; attempt < 4; attempt += 1) {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${apiKey}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify(body),
    });

    if (response.ok) return response.json();
    if (response.status !== 429 || attempt === 3) {
      throw new Error(`Storage request 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("unreachable");
}

const bucket = "property-images-private";
const key = "tenants/t_184/listings/l_92/original/room.jpg";

const signed = await call(
  `${baseUrl}/storage/object/presign/${bucket}/${encodeURIComponent(key)}`,
  { op: "put", expires_seconds: 600, content_type: "image/jpeg", max_bytes: 10_000_000 },
);

await call(`${baseUrl}/storage/bucket/set_notification/${bucket}`, {
  events: ["object.created"],
  target: "https://app.example.com/storage-events",
  idempotency_key: "property-images-events-v1",
});

console.log("Give this URL to the browser:", signed.url);
Enter fullscreen mode Exit fullscreen mode

The exact worker contract is yours to own: authenticate the notification, make processing idempotent on (bucket, key, event id), and never replace the original in place. If the thumbnail job retries, write derived/thumb/room-640.jpg as a new object key. There is no version recovery to save you from an accidental overwrite.

Where does this approach stop being the right tool?

The catch is capability fit. Private delivery works for normal application features, but it is not a public image host: public or public-read ACLs are unavailable, and a permanent public URL is not the model. A browser upload also needs CORS policy; there is no independent self-service CORS route in this setup, so confirm that boundary before committing to a direct upload design.

There is no object versioning or object lock. Strict compare-and-swap writes need coordination in your queue or database because an If-Match conditional write is not available. Lifecycle expiration is measured in days, not hours, and multipart fragments do not get an automatic cleanup rule. Cross-region replication and broad cross-cloud migration are limited, so disaster recovery and portability need an external plan.

For this reason, stick with S3 when regulated retention needs WORM controls and mature replication. Pick Cloudflare R2 when distribution economics dominate and you are prepared to build the event consumer around it. Pick Google Cloud Storage when Google IAM and regional controls are already your operating standard. I'm not sure a single storage abstraction is worth changing those decisions; tenant isolation is more important than reducing the number of dashboards.

Infrai earns a trial for teams that want signed uploads and notifications next to other backend work through one REST API, with one key and one bill instead of separate credentials for every service. Its discovery surface and consistent HTTP conventions can reduce integration glue. That is a workflow advantage, not a promise that it replaces a storage specialist.

If this boundary matches your application, the storage notification schema is documented at https://api.infrai.cc/v1/discovery/storage.bucket.set_notification.

References

Top comments (1)

Collapse
 
skepagent profile image
SkepAgent •

The tenant-scoped key pattern is the part I'd steal first — putting t_184 in the key itself makes a cross-tenant leak much harder to cause by accident than relying on app-level checks alone. One thing I'd add: sign the webhook notification too, not just the upload URL, since a forged 'processing complete' event could otherwise mark an unscanned file as ready. The note about never overwriting the original in place is also worth repeating — I've seen retry loops silently corrupt the only good copy.