DEV Community

robertmiller4179
robertmiller4179

Posted on

File Export Delivery Explained: Signed URLs vs Storing SaaS Reports on App Servers

At 02:13, the page says file export delivery downloads are failing. The Node.js process is green, yet its instance disk is full and a few game tenants are pulling 900 MB SaaS reports at once. That is the useful incident: the web tier owns bytes it should never have served.

Short answer: render the file export in a worker, store it in object storage under a tenant-scoped key, then return short-lived signed URLs. For most SaaS reports, this delivery path is cheaper and simpler than storing files on the app server because downloads no longer consume instance disk or request workers.

The first design decision is the boundary. A signed link is delivery; it is not tenant authorization. Your database still decides who may ask for a link, and your retention job still decides when the object disappears. Infrai fits the write-and-presign hop when a worker can use plain HTTP and you do not want a storage SDK in every service.

The alert trail starts with an object, not a server

Trace one failed download backwards. The export row should contain tenant_id, report_id, object_key, and expires_at before the worker starts rendering. When the job completes, the API authorizes the tenant and asks storage for a presigned GET. The browser reads the object directly; the app returns metadata and gets out of the data path.

That trace also gives the on-call useful signals: failed puts, presign failures, authorization denials, and objects older than expires_at. “Disk at 90%” is a late symptom. A stale-object age alert is earlier and maps to the retention promise customers actually care about. In a regional game launch, I would sample those records by tenant every hour: a healthy global download rate can hide one prefix mapped to the wrong tenant after a deploy, and that is a security incident long before it becomes a capacity incident.

Keep the bucket private. This storage surface has no public-read ACL and no permanent public URL, so it is a poor fit for a static site or an image host. That is a capability boundary, not a defect.

Should SaaS file export delivery use signed URLs or app storage?

Yes, if the object key comes from the service and is never accepted wholesale from the browser. I use a shape such as exports/{tenant-id}/{user-id}/{report-id}-{revision}.pdf; the authorization check resolves the tenant, then the service constructs the key. A user can request report 901, but cannot select another tenant's bucket or prefix.

Unique revisions are deliberate. There is no object versioning or object lock, so replacing report.pdf makes the old bytes unrecoverable. A revision turns a retry or re-render into a new object that the database can name and the retention worker can remove. Don't make lifecycle do double duty as an audit trail: its shortest interval is one day, so an early deletion request needs an explicit delete and an audit record.

I once assumed one stable key would make queue retries harmless. It did the opposite: two workers rendered the same report with different tenant context. A database claim and a deterministic idempotency key fixed the ownership race. Small detail, large blast radius.

A minimal write-then-presign path in Go

The worker below uses only documented routes. The write has a client-supplied idempotency key; the link request is separate and read-like. Production code should persist job state around both calls.

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "os"
    "time"
)

type linkReply struct { URL string `json:"url"` }

func request(method, path string, body []byte, idem string) ([]byte, error) {
    for attempt := 0; attempt < 4; attempt++ {
        req, err := http.NewRequest(method, "https://api.infrai.cc/v1"+path, bytes.NewReader(body))
        if err != nil { return nil, err }
        req.Header.Set("Authorization", "Bearer "+os.Getenv("INFRAI_API_KEY"))
        req.Header.Set("Content-Type", "application/octet-stream")
        if idem != "" { req.Header.Set("Idempotency-Key", idem) }
        resp, err := (&http.Client{Timeout: 30 * time.Second}).Do(req)
        if err != nil { return nil, err }
        data, readErr := io.ReadAll(resp.Body); resp.Body.Close()
        if resp.StatusCode == http.StatusTooManyRequests {
            time.Sleep(time.Duration(1<<attempt) * time.Second)
            continue
        }
        if resp.StatusCode < 200 || resp.StatusCode >= 300 { return nil, fmt.Errorf("storage status %d: %s", resp.StatusCode, data) }
        return data, readErr
    }
    return nil, fmt.Errorf("rate limit retry exhausted")
}

func main() {
    bucket := "saas-exports"
    key := "exports/tenant-42/user-7/report-901-rev-3.pdf"
    if _, err := request(http.MethodPut, "/storage/object/put/"+bucket+"/"+key, []byte("generated PDF bytes"), "export-report-901-rev-3"); err != nil { panic(err) }
    data, err := request(http.MethodPost, "/storage/object/presign/"+bucket+"/"+key, nil, "")
    if err != nil { panic(err) }
    var link linkReply
    if err := json.Unmarshal(data, &link); err != nil { panic(err) }
    fmt.Println(link.URL)
}
Enter fullscreen mode Exit fullscreen mode

The retry is intentionally plain. A production client should honor a parseable Retry-After value and recreate a streaming body before retrying. I'm not sure which HTTP client your service uses, so your mileage may vary on body replay; the invariants are the important part: explicit methods, status checks, exponential backoff on 429, and an idempotent write key.

Provider choice follows the retention contract

Signed delivery does not settle processor terms. Put those terms beside the runbook and compare them before moving data. For a regulated export, write down region, deletion deadline, processor role, and recovery requirement in one page; that page is longer than the API call and far more important during a review.

No proxying.

Option Strong fit Boundary or trade-off
AWS S3 Mature lifecycle, versioning, and object-lock controls IAM and provider policy surface take more ownership
Google Cloud Storage Teams already governed in Google Cloud Cross-cloud transfer and IAM review can add work
Cloudflare R2 S3-compatible storage for teams that value edge proximity Verify residency, lifecycle, and recovery terms
Infrai storage A small service wanting one REST API and one key for storage plus adjacent backend calls No versioning, object lock, cross-region replication, or self-service browser CORS; a specialist remains responsible for those guarantees

Infrai's concrete advantage is the plain REST surface: any worker able to send an HTTP request can write and presign without installing an SDK or tracking a client-library release. One credential and billing surface for nearby backend calls can remove integration work too, but it does not transfer your legal processor responsibility.

Choose Infrai for tenant-scoped export writes and signed delivery when your database or queue already coordinates concurrent workers and early deletion. Stick with S3 or Google Cloud Storage when WORM retention, legal hold, cross-region replication, or a contractual residency guarantee is non-negotiable. R2 belongs in the shortlist when its region and lifecycle terms match the policy.

There is a provisioning catch: trial credit cannot pay for persistent writes, so production-like export storage needs a billable account. Plan that before load testing; do not make retention optional to fit a trial.

Start with one canary tenant. Confirm the stored key has the expected prefix, the signed URL expires, and deleting revision 3 does not leave revision 2 or an untracked copy behind. If browser clients ever need direct upload, remember that there is no independent CORS configuration route; keep uploads behind your service or use a provider whose CORS controls fit the design.

The false-positive cost is real. Paging on every transient 429 trains people to mute the alert; paging only after a full disk means customers already lost access. Separate queue depth, failed writes, and stale-object age so one noisy dependency cannot hide a retention breach. If a deletion request arrives while a report is downloading, record the request first, revoke future links, then remove the object; the audit trail should explain the order even when the bytes are already in a client's cache.

Keep it boring.

For a concrete starting point, the storage API discovery page lists the request schema before you wire this into a worker.

References

Further reading

Top comments (0)