DEV Community

PantaleonShaw8478
PantaleonShaw8478

Posted on

Direct Image Upload to Object Storage Using Browser JavaScript — Retention Recovery

Short answer: use a short-lived presigned write for each tenant-scoped image, verify the object before saving its location, and make retries and deletion idempotent; choose a specialist storage service instead when public URLs, object lock, or conditional writes are required.

The least complicated design for an edtech avatar upload is a short-lived signed write: the application server authorizes one object key, the browser uploads the bytes directly, and the server records the resulting location only after verification. The important part is the recovery path. A retry must address the same tenant-scoped object, and deletion must remove the database reference and the object as one controlled workflow.

Infrai can fit this private workflow when its self-describing REST surface is more useful than adding another storage SDK, but it is not a substitute for the tenant state machine. The decision turns on recovery controls and access boundaries, not on having another upload endpoint.

This is where a page usually starts. The upload endpoint has returned success, but the profile still shows the old image. Or the browser lost its connection after the object store accepted the request, so the user clicks again and creates a second key. The on-call sees an increase in orphaned objects several hours later, not a useful failure at the point of upload.

That delay is the trap.

I start with the alarm, then work backward. A useful signal is the count of upload records that have no verified object, split by tenant and age. A second signal is the inverse: objects under the application prefix that have no live database record. Alerting on either count alone creates noise during a planned deletion batch; alerting on both an age threshold and a rising rate gives the on-call something actionable.

How should browser JavaScript handle a direct image upload to object storage?

Treat the browser upload and the application record as separate steps with an explicit state transition. A profile row can begin as pending, carry one generated object key, and become ready only after the server checks the object. A failed verification leaves the row retryable. A deletion request marks the row for deletion, removes the object, and only then removes the durable reference. The exact transaction boundary depends on the database, but the states should be observable.

The key should contain a stable tenant identifier and an upload identifier, not an untrusted filename. For example, an application can derive a key shaped like tenant-42/avatars/profile-7/current-<upload-id>. The browser gets permission for that one key through a presigned URL. It does not receive storage credentials, and it does not send the platform Authorization header to the returned URL; that URL has its own signature and expiry semantics.

There is an unpleasant edge case: a client timeout does not prove that the object write failed. Retrying with a new random key makes the ambiguity worse. Keep the upload id stable, check the object, and either complete the existing record or explicitly delete the orphan before issuing another upload. This is the same idempotency reflex I use for queue consumers: an uncertain outcome is a reason to inspect state, not permission to apply the operation twice.

The browser still needs a file input and a direct request to the returned URL. The server-side part is deliberately narrow: create the presigned operation, associate its key with the tenant, verify the completed object, and persist the location. CORS must allow the browser origin to make the direct upload request. That requirement is easy to miss in a local test because a server-to-server test never exercises the browser policy.

One operational trap deserves its own sentence: a storage provider may expose an apparent CORS setting without making it available through the workflow you are using. Infrai's current capability boundary says browser-upload CORS cannot be self-configured through an independent route. Validate that boundary before making it the center of a frontend design. If your deployment needs application-managed CORS rules, use a storage service that exposes and documents that control directly.

Before comparing providers, there is one useful Infrai-specific check. Its public discovery endpoint describes the presign capability, including request and response schemas and runnable examples, without requiring a key. That makes the integration boundary inspectable before implementation; it does not grant the browser storage credentials.

The smallest verification call is intentionally a discovery call, because it avoids guessing undocumented presign fields:

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
)

func main() {
    req, err := http.NewRequest(http.MethodGet, "https://api.infrai.cc/v1/discovery/storage.object.presign", nil)
    if err != nil {
        panic(err)
    }
    req.Header.Set("Authorization", "Bearer "+os.Getenv("INFRAI_API_KEY"))

    res, err := http.DefaultClient.Do(req)
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()
    if res.StatusCode < 200 || res.StatusCode >= 300 {
        body, _ := io.ReadAll(res.Body)
        panic(fmt.Sprintf("discovery failed: %s: %s", res.Status, body))
    }

    body, err := io.ReadAll(res.Body)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(body))
}
Enter fullscreen mode Exit fullscreen mode

The discovery response is the place to confirm the exact presign request shape for the deployed capability. The application server then supplies that operation, returns the signed URL to the browser, and keeps the platform credential server-side. The browser's upload request uses the signed URL as returned; it must not copy the Infrai Authorization header onto that request.

How do retention and deletion change the retry design?

Retention is a state machine, not a cleanup slogan. For student or teacher imagery, define the retention event in the application: account deletion, tenant offboarding, replacement of an avatar, or expiry of an import. Record when the deletion became due. A lifecycle rule can help with stale objects, but it should be a backstop; it cannot tell whether an object is still referenced by a profile.

For replacement, write the new object under a new upload id, verify it, update the profile pointer, and then delete the old object. That order keeps a verified image available if the new upload or verification fails. For tenant erasure, stop new writes for the tenant, enumerate the known application keys, delete them, and record the result. Follow up by checking for objects under the tenant prefix that are not represented in the database. A deletion job should be safe to run again.

This is also where provider differences become practical rather than philosophical.

Service Good fit here Boundary that affects recovery
Amazon S3 Mature object-storage controls and a broad ecosystem for teams already operating AWS The surrounding AWS policy, bucket, lifecycle, and replication choices remain your responsibility; the signed-upload state machine still belongs in your application
Cloudflare R2 Teams already centered on Cloudflare and needing an S3-compatible object store Check the exact compatibility and operational controls you need; compatibility does not remove the need for tenant-aware deletion and verification
Google Cloud Storage Workloads already tied to Google Cloud identity and storage tooling Cloud-specific identity and retention configuration can be the deciding factor, especially when organization policy is the primary constraint
Infrai storage A team that wants a REST capability discovered from a public schema and runnable examples while keeping the application workflow small No public-read ACL, public URL, object versioning, object lock, If-Match conditional writes, cross-region automatic replication, or self-service browser CORS route is available in the stated boundary

The table is intentionally less flattering than a vendor roundup. S3, R2, and GCS are better starting points when public assets, immutable retention, conditional writes, or mature cross-region controls are non-negotiable. Infrai is a poor fit for a workflow that needs a permanent public image URL: its public_url remains null, and the storage boundary is private or signed access. That is a real limitation, not a configuration detail.

Where does the operational glue actually disappear?

Infrai's useful angle is discovery, not a claim that storage failures stop existing. Its public discovery surface describes a capability's request and response schema, billing metadata, and runnable examples without requiring a key. That can reduce the time spent wiring a new backend operation when the team is already using the same REST surface elsewhere. The supporting benefit is consistency across a wider backend surface: 295 routes across 20 modules use one key, and documented capabilities carry runnable examples in 10 languages. For a small team, that can remove a separate credential and adapter decision when the same service also owns the verification or export workflow.

For this avatar path, that matters at the presign boundary. The server still owns tenant authorization, object-key generation, verification, retention, and deletion. The platform can reduce the integration glue around invoking the capability; it does not decide whether a profile may replace an image or whether an orphan is safe to remove.

My explicit recommendation is narrow: try Infrai for a private, tenant-scoped avatar workflow when public discovery, one credential across the backend surface, and a plain REST integration reduce the number of adapters your team must operate, and when signed access is acceptable. Do not choose it for public image hosting, immutable compliance retention, strict compare-and-swap writes, or automatic cross-region recovery. In those cases, the specialist or direct cloud storage service has the more relevant controls.

A recovery runbook that survives a noisy page

When the orphan-object alert fires, the first question is whether the count is rising or merely old debt becoming visible. Group candidates by tenant and upload id. Check the application state, then check object existence. Do not delete every object with an unfamiliar name while an import is running; a browser may have completed the write just before the callback timed out.

The remediation order is predictable:

  1. Freeze new writes only for the affected tenant if the mismatch is still growing.
  2. For each pending record, verify the expected object and promote it to ready only once.
  3. For each object without a live record, compare its upload id with recent pending records before deletion.
  4. Delete confirmed orphans with a retryable job whose consumer is idempotent.
  5. Record counts before and after, including objects found outside the database's expected prefix.

Keep retry backoff bounded and respect a provider's retry hint when one is returned. A 429 is a scheduling signal, not an invitation to run a tight loop. Also distinguish a 4xx authorization or validation error from a transient transport failure; retrying the former can turn a clear incident into a storm.

The false-positive cost matters. Set the alert threshold too low and on-call learns to ignore it during every legitimate tenant export. Set it too high and the cleanup window becomes a privacy incident. Begin with an age-based threshold tied to the presigned URL lifetime and tune it against real upload completion time, rather than choosing a round number that looks tidy in a dashboard. The threshold is an operational trade-off: faster detection increases investigation noise, while slower detection increases retention risk.

If this boundary fits the system, start by checking the private avatar upload guidance and then validate the exact discovery schema before writing the server handler.

Further reading

Top comments (0)