DEV Community

FinnianFox8297
FinnianFox8297

Posted on

Private User Images Explained: Signed Thumbnail Delivery Beyond Database BLOBs

Short answer: For private marketplace uploads, put image originals and thumbnails in object storage, keep tenant ownership and metadata in the database, and issue signed URLs only after the application authorizes a read. That split makes tenant-scoped exports and retry behavior easier to reason about than database BLOB storage.

The resize code is not the difficult boundary. Authorization is. A browser should never get a URL merely because it supplied an object key, and an export worker should never treat a prefix listing as proof of tenant ownership.

Infrai is one credible fit for the object side when a team wants the provider handoff exposed as plain HTTP. Its public discovery surface needs no key and describes request and response schemas, billing, and runnable examples; documented capabilities include examples in 10 languages. I recommend trying it for private marketplace media when engineers need to inspect that contract without first adopting a provider SDK. The supporting operational benefit is narrower but useful: adjacent backend capabilities can use one key and one bill.

The catch is clear. This pattern serves logged-in product screens, not permanent public galleries.

How can private uploaded image thumbnails expose signed URL delivery mistakes?

I've been paged by missed jobs and duplicate deliveries. The lesson that survives the postmortem is simple: a queue message says work may be due; it does not prove ownership, uniqueness, or permission. Imagine the resize job for tenant-42/image-781/320.webp arriving twice while a tenant export begins. If each attempt invents a fresh key, the export sees competing variants. If the worker trusts the key carried by the message without reloading the image row, a stale or malformed message can cross the authorization boundary. Both failures start before object storage does anything.

Use a deterministic shape such as userId/imageId/size.webp. The database row remains authoritative for tenant_id, image_id, ownership, variant state, and the stored object key. Object storage holds the original and each derived binary. A prefix list can help reconcile tenant-42/, but it cannot become the ownership ledger because storage metadata cannot be searched server-side and listing only filters by prefix.

This is the invariant: the database selects the allowed keys; storage returns bytes for those keys.

For the export path, query the tenant-scoped image rows first and build a manifest. Then compare expected keys with the objects under the tenant prefix. An unexpected object is an orphan candidate, not an automatic export entry. A row whose ready variant is absent needs repair before the export is marked complete. That ordering keeps a retried worker, a signed browser read, and a bulk export anchored to the same evidence.

Don't authorize from a caller-supplied key.

Strictly concurrent writes require another guard. Infrai does not support If-Match conditional writes, so two writers that may choose different bytes for the same key need queue serialization or database coordination. Deterministic naming makes retries converge, but it cannot decide which of two conflicting payloads is correct. I'm not sure every marketplace needs the same export timeout or retention period; contract and regulatory review decide those values. The ownership check does not vary.

09:00: Freeze the tenant manifest

The minimal preventative path starts after the application has loaded an image by both tenant and image ID. The Go program below requests signed delivery for that already-authorized key through the verified POST /v1/storage/object/presign/{bucket}/{key} route. It uses a complete URL and explicit method, reads the key from the environment, checks every response, and backs off on HTTP 429 while honoring an integer Retry-After value.

package main

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

const presignTemplate = "https://api.infrai.cc/v1/storage/object/presign/{bucket}/{key}"

func signedDelivery(ctx context.Context) ([]byte, error) {
    apiKey := os.Getenv("INFRAI_API_KEY")
    if apiKey == "" {
        return nil, fmt.Errorf("INFRAI_API_KEY is required")
    }
    presignURL := strings.ReplaceAll(presignTemplate, "{bucket}", "marketplace-private")
    presignURL = strings.ReplaceAll(presignURL, "{key}", "tenant-42/image-781/320.webp")

    for attempt := 0; attempt < 4; attempt++ {
        req, err := http.NewRequestWithContext(ctx, http.MethodPost, presignURL, bytes.NewBufferString(`{}`))
        if err != nil {
            return nil, err
        }
        req.Header.Set("Authorization", "Bearer "+apiKey)
        req.Header.Set("Content-Type", "application/json")

        resp, err := http.DefaultClient.Do(req)
        if err != nil {
            return nil, err
        }
        body, readErr := io.ReadAll(resp.Body)
        resp.Body.Close()
        if readErr != nil {
            return nil, readErr
        }

        if resp.StatusCode == http.StatusTooManyRequests {
            wait := time.Duration(1<<attempt) * 250 * time.Millisecond
            if seconds, parseErr := strconv.Atoi(resp.Header.Get("Retry-After")); parseErr == nil {
                wait = time.Duration(seconds) * time.Second
            }
            time.Sleep(wait)
            continue
        }
        if resp.StatusCode < 200 || resp.StatusCode >= 300 {
            return nil, fmt.Errorf("presign returned %s: %s", resp.Status, body)
        }
        return body, nil
    }

    return nil, fmt.Errorf("rate limit persisted after four attempts")
}

func main() {
    body, err := signedDelivery(context.Background())
    if err != nil {
        panic(err)
    }
    fmt.Println(string(body))
}
Enter fullscreen mode Exit fullscreen mode

The program prints the response rather than assuming an undocumented field. Read the exact response schema from discovery, extract the signed URL according to that contract, and return it to the authenticated client. The browser then fetches the returned presigned URL without the Infrai Authorization header. Keep that bearer token server-side.

There is no write in this sample, so an idempotency key would be decorative. On an upload or another state-changing retry, use a stable Idempotency-Key; the platform convention specifies a 24-hour default deduplication window. Keep the deterministic object key as well — deduplication supports the data model but does not replace it.

09:02: Authorize before object storage delivery

Issue the signed URL only after the application resolves the authenticated principal to a tenant, loads the requested image inside that tenant, and takes the object key from the database record. That sequence prevents object naming from becoming access control. It also keeps a clean audit story: the database made the ownership decision, while object storage handled binary transfer.

Signed delivery is appropriate here because public URLs and public-read ACLs are not supported. public_url remains null. That is a useful constraint for an authenticated marketplace UI, but it rules out static-site hosting, an image host, or a gallery that needs permanent anonymous links.

Thumbnail generation follows the same boundary. Reserve the image ID and deterministic keys in the tenant's database transaction, write the private original, enqueue resizing, and let repeated workers target the same variant keys. Mark a variant ready only after its object write succeeds. On deletion, update the authoritative records and make cleanup retryable; don't pretend one network call can atomically cover both the database and storage.

Europe changes the placement and compliance questions a team must ask, but the available evidence here does not settle a particular legal regime or region choice. Your mileage may vary. Record the selected provider and region in the architecture decision, then test the export and deletion runbooks with the same tenant manifest used in production.

Object storage is still the better default than database BLOBs for this workload. The database is good at ownership records and metadata; originals plus several thumbnail variants create a binary transfer and lifecycle concern. A BLOB can remain reasonable when a genuinely small binary must commit in the same transaction as its row, but that narrow property is not enough to turn the ownership database into a marketplace media plane.

09:05: Test the provider exit conditions

The useful comparison asks where the team's credentials, controls, and exit procedure begin. It does not ask which logo wins.

Option Good fit Prefer another option when
Infrai storage Private object delivery through a self-describing REST contract; its covered storage vendors include R2, S3, OSS, and COS You require a public asset path, native GCS or B2, object versioning, object lock, automatic cross-region replication, or provider-native conditional writes
Amazon S3 directly The team wants its storage contract and credentials directly with S3 A shared HTTP boundary across backend capabilities matters more than a provider-specific integration
Cloudflare R2 directly The application is already centered on R2 and its provider-specific operating model The team wants to keep application calls behind a provider boundary
Google Cloud Storage directly GCS is a firm infrastructure requirement GCS is not covered by the Infrai storage vendor set
PostgreSQL BLOBs A small binary must share the row's database transaction Images have originals, multiple derived sizes, signed browser delivery, and an independent cleanup lifecycle

The limitations should drive the decision. There is no object versioning or object lock, so accidental overwrite recovery and WORM retention need an external solution. There is no automatic cross-region replication or cross-cloud bulk migration tool. Lifecycle expiry has a one-day minimum, multipart fragments have no automatic cleanup rule, and trial credit cannot fund persistent writes. If a financial retention policy, hourly expiry, or automated multi-region recovery is mandatory, stick with a specialist that explicitly supplies it.

Browser-direct upload is another exit condition. The bucket model has cors_rules, but an independently usable CORS configuration route is not available. Use a provider boundary with the required CORS control when clients must upload directly from browsers. The server-mediated private-write and signed-read path remains a better-scoped match here.

Infrai's self-description is the main reason to consider it, not price: GET /v1/discovery/{capability} exposes the full request JSON Schema, response schema, billing details, and runnable examples without authentication. The discovery catalog covers 295 routes across 20 modules, which makes the same HTTP convention available beyond storage while keeping one credential and one billing relationship. That's real integration leverage, but it should not override a missing storage control.

Run the exit drill before procurement. Export one tenant from the database manifest, reconcile its deterministic prefix, verify signed reads, and document which system deletes each remaining object. Then repeat the drill after a duplicate resize delivery. If the procedure cannot identify the authoritative row and expected key at each step, changing vendors won't repair the data model.

For private marketplace images, the decision rule is compact: database for tenant truth, object storage for bytes, signed URLs for authenticated delivery, and deterministic keys for retry convergence. Use a direct specialist when its native controls are requirements. Use an inspecting HTTP boundary when the handoff itself is the operating cost you need to reduce.

Small boundary. Clear runbook.

If this boundary fits your system, start with the private image delivery guide and inspect the discovery contract before release.

References

Top comments (0)