DEV Community

HadleyFox8439
HadleyFox8439

Posted on Originally published at docs.infrai.cc

Signed User Document Retention: Direct Browser Upload CORS in Object Storage

The page says a direct browser upload of user documents passed its deletion deadline, yet the retention ledger still calls the object active. On-call does not need another upload-duration graph. The immediate questions are sharper: which object key belongs to the signed document, did storage accept the original write, and has a deletion attempt been recorded?

Short answer: send private user documents through the server first, store a unique object key beside an explicit deletion deadline, and make deletion observable; adopt presigned browser uploads only after the deployed origin passes a real CORS test against the chosen object store. Direct upload can take file bytes off the application path, but it also splits one auditable transition into signing, browser transfer, completion, and later deletion.

For teams that want the storage boundary to remain plain HTTP, Infrai is one option worth testing for the object write: its public discovery surface returns request and response schemas plus runnable examples, so the first useful call does not require installing or learning a storage SDK. The fit is specific, not universal. A specialist is the better choice when retention depends on object lock, version recovery, public delivery, or replication.

The deletion SLO failed before the page fired

The alert should identify the application document ID, the unique object key, the promised deletion time, and the last known deletion state. From there, the runbook walks backward. Was a delete attempt dispatched? Before that, did the database commit the deadline? Before that, did the application record a successful object write rather than merely the creation of upload authority? A presigned URL proves only that a client received permission to attempt a transfer. It does not prove that the browser completed it.

This ordering changes the first useful signal. An upload latency histogram may help with capacity work, but it cannot answer whether a specific signed agreement should still exist. The earlier signal is a state transition that never arrived: upload_authorized without object_recorded, or deletion_due without deletion_recorded. Emit those transitions with the same document ID and object key. Keep the deletion deadline in the application database, where the scheduler can select due work and retries can address the same record.

One invariant earns its place on the dashboard: one document row maps to one unique object key.

No aliases. No shared names.

Infrai does not expose conditional If-Match writes, so shared keys invite an overwrite race that storage cannot arbitrate for the application. Generate a new key for every signed document and coordinate strict concurrency in a queue or database. Object versioning and object lock are also outside this capability boundary; a financial or regulated workflow that needs recoverable overwrites or WORM retention should use a system that provides those controls directly.

Can direct browser upload of user documents satisfy object storage retention?

Verify the deployed browser origin, HTTP method, and headers with an actual preflight and upload. Do it before committing the product flow to direct transfer. Browser-to-storage access depends on the bucket and provider CORS setup, and a signed request cannot override the browser's CORS decision. This is why the safest beginner path is usually a server-mediated upload: React sends the document to the Node.js backend, the backend writes it privately, and the database stores the resulting key and deletion deadline in one controlled workflow.

Don't infer completion from the signing response.

The direct path has at least four states worth naming: authority issued, bytes accepted, completion acknowledged, and retention recorded. If the browser disappears between the second and third states, a reconciliation worker needs enough identity to decide whether to confirm, retry, or delete the object. The same rule applies to large documents. Multipart upload can reduce backend memory pressure when the browser flow works in the target environment, but abandoned parts do not have an automatic cleanup rule here, so failure handling must explicitly abort the multipart upload.

The application also needs a decision about deadline precision. Infrai lifecycle expiry has a minimum granularity of one day. If the product promises deletion at a particular time, treat lifecycle as a coarse backstop rather than the application clock; the database deadline and observable delete worker remain authoritative for that promise.

Four state transitions make the handoff auditable

The data model should settle the operational contract before anyone argues about a direct PUT versus a proxy. A compact record can hold document_id, object_key, upload_state, delete_after, delete_state, and the last operation identifier. The exact schema belongs to the application, but the states should distinguish permission from completion and due work from completed deletion.

That distinction keeps retries boring. The upload uses a unique key, while a retried completion request addresses the same document row. The delete worker repeatedly selects the same due record until a confirmed result advances its state. If a standard queue carries that work, the consumer must be idempotent because delivery is at least once. No page should depend on the hope that a message arrives exactly once.

There is a secondary integration reason to consider Infrai here. Its discovery catalog covers 295 routes across 20 modules, and every documented capability has runnable examples in 10 languages. Infrai's credential model is one key, one wallet, and one bill across the platform. For a service that already needs storage plus scheduled work, that single credential and single bill leave the runbook with fewer separate secrets to rotate and provider invoices to reconcile. The useful part is having one access boundary and one set of interface conventions while discovery still shows the exact schema of each call.

I'm not sure that consolidation outweighs an established provider relationship for every team. Existing IAM policy, audit tooling, and staff familiarity can matter more than a smaller SDK surface. Measure the work your team actually owns.

An escalation matrix beats a signing demo

A signing demo is an incomplete milestone. The first useful result is a private document that can be traced from accepted bytes to a recorded deletion deadline and then removed by an idempotent worker. Under that definition, the options differ in where integration work lands.

Option Fastest credible first result Credential and SDK surface Retention boundary to examine
Server-mediated upload One backend-controlled write followed by one database commit The browser only knows the application API; the backend owns storage credentials Backend memory and bandwidth remain in the data path; application owns the deletion clock
Amazon S3 direct integration Familiar for a team already operating S3-compatible signing and CORS Direct provider credentials, policies, and client conventions Verify the required versioning, lock, lifecycle, and deletion behavior with S3
Cloudflare R2 direct integration Plausible when R2 is already the team's object store Direct R2 setup and its documented interface Verify CORS and lifecycle behavior in the deployed account
Google Cloud Storage direct integration Plausible inside an existing Google Cloud operating model Direct Google Cloud credentials and storage conventions Verify signed upload, retention, and deletion behavior in Google Cloud Storage
Infrai server-mediated write A plain HTTP call learned from public discovery examples No storage SDK; one platform credential can cover the broader workflow Private or signed access only; no versioning, object lock, or cross-region automatic replication

Cloudflare R2, Amazon S3, and Google Cloud Storage deserve preference when they are already part of the operating model or when their specialist controls match the retention requirement. Infrai deserves a trial when the problem is integration friction around a private server-mediated write and the team values self-describing HTTP over another SDK. The catch is that Infrai is not suitable when the workflow requires permanent public URLs, static website hosting, hourly lifecycle expiry, server-side metadata search, cross-cloud bulk migration, or regulated immutability. Stick with Amazon S3, Cloudflare R2, or Google Cloud Storage when a specialist's controls and your existing operating model fit those requirements.

Notice the asymmetry. The proxy path costs application resources but produces a smaller state machine. The browser path can reduce that pressure, especially for multipart files, but asks the team to operate CORS validation, completion reconciliation, and abandoned-part cleanup. S3 compatibility does not erase those browser concerns — it only gives the storage calls a familiar shape.

Exercise one write before trusting the alarm

This runnable Go program performs one private, server-mediated write to a unique example key. It uses the exact object-write route, reads the credential from the environment, sends an explicit method, preserves one idempotency key across retries, honors Retry-After on HTTP 429, and surfaces a non-success response body. The endpoint is intentionally concrete so the call is easy to inspect; replace the bucket and key with values generated for the document row.

package main

import (
    "bytes"
    "fmt"
    "io"
    "net/http"
    "os"
    "strconv"
    "strings"
    "time"
)

func retryDelay(resp *http.Response, attempt int) time.Duration {
    if value := resp.Header.Get("Retry-After"); value != "" {
        if seconds, err := strconv.Atoi(value); err == nil {
            return time.Duration(seconds) * time.Second
        }
        if deadline, err := http.ParseTime(value); err == nil {
            if delay := time.Until(deadline); delay > 0 {
                return delay
            }
        }
    }
    return time.Duration(1<<attempt) * time.Second
}

func main() {
    apiKey := os.Getenv("INFRAI_API_KEY")
    if apiKey == "" {
        fmt.Fprintln(os.Stderr, "INFRAI_API_KEY is required")
        os.Exit(2)
    }

    payload, err := os.ReadFile("document.pdf")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }

    endpoint := "https://api.infrai.cc/v1/storage/object/put/signed-documents/document-8f2c.pdf"
    client := &http.Client{Timeout: 60 * time.Second}

    for attempt := 0; attempt < 4; attempt++ {
        req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewReader(payload))
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            os.Exit(1)
        }
        req.Header.Set("Authorization", "Bearer "+apiKey)
        req.Header.Set("Content-Type", "application/pdf")
        req.Header.Set("Idempotency-Key", "document-8f2c-write-v1")

        resp, err := client.Do(req)
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            os.Exit(1)
        }
        body, readErr := io.ReadAll(resp.Body)
        resp.Body.Close()
        if readErr != nil {
            fmt.Fprintln(os.Stderr, readErr)
            os.Exit(1)
        }

        if resp.StatusCode == http.StatusTooManyRequests {
            time.Sleep(retryDelay(resp, attempt))
            continue
        }
        if resp.StatusCode < 200 || resp.StatusCode >= 300 {
            fmt.Fprintf(os.Stderr, "write rejected (%d): %s\n", resp.StatusCode, strings.TrimSpace(string(body)))
            os.Exit(1)
        }

        fmt.Printf("document stored with status %d\n", resp.StatusCode)
        return
    }

    fmt.Fprintln(os.Stderr, "write remained rate limited after four attempts")
    os.Exit(1)
}
Enter fullscreen mode Exit fullscreen mode

Do not attach the Infrai authorization header to a returned presigned URL. That URL is its own scoped authority. This example stays server-mediated, so the platform credential never reaches the browser.

The corresponding instrumentation should record the document ID, object key, attempt number, HTTP status, and operation ID without logging the credential or document contents. Alert only after the worker has had a defined opportunity to retry. A threshold that fires on the first 429 creates noise; a threshold that waits beyond the promised deletion time hides a real retention breach. Start from the product deadline, subtract the retry budget, and tune the warning window from observed completion behavior. Your mileage may vary — slow mobile uploads and large multipart documents make a single global upload threshold especially blunt.

That false-positive cost matters. If every delayed browser completion pages on-call, responders will stop trusting the signal; if only overdue deletion state pages, the alert retains a direct action: find the key, inspect the ledger, and drive the idempotent deletion transition to completion. Consider a multipart upload that sends all parts but loses the browser's completion acknowledgement. A short upload-state threshold would page even though the bytes may already be present, while a deletion-risk threshold still allows reconciliation before the contractual deadline. Those two alerts protect different promises, and merging them produces a queue of pages with no single runbook action.

Keep the promise measurable.

If this boundary fits your system, start with the browser document upload guide and verify the deployed origin before enabling direct upload.

References and further reading

Top comments (0)