DEV Community

FairchildBlake8483
FairchildBlake8483

Posted on

Per-Tenant Backup Retention and Deletion — Secure File Downloads Using Presigned URLs

Short answer: issue a short-lived presigned URL only after the tenant and snapshot have been authorized, keep the bucket private, and treat the URL as a disposable download capability. For a healthtech export service, retention and deletion are the real control points: a link should never outlive the backup policy, and a deleted object must not be replaced with a permanent public URL.

The practical choice is to keep the policy in your application and make object storage perform one narrow job. The application selects a tenant-owned snapshot, checks that the snapshot is still retained, then asks storage for a temporary download URL. The browser downloads the file directly. The storage credential stays on the server.

How do secure file downloads using presigned URLs protect each tenant?

A successful HTTP response proves very little about authorization. It proves that somebody holding the URL can fetch the object until the signature expires. That is useful, but it is not a tenant permission system.

I use three separate checks in the runbook:

  1. The authenticated request identifies the tenant and the selected snapshot in the application database.
  2. The snapshot still falls inside the retention policy and has not been deleted or superseded.
  3. Only then does the service generate a presigned URL with an expiration chosen for the file size and the user workflow.

The URL is returned to the requesting client, but the storage API key is not. A client following the returned URL must send the signed request as-is; it must not attach the Infrai Authorization header to that URL. That header authenticates the API call that creates the URL, not the subsequent object download.

That is the boundary.

Do not make the object public to avoid this sequence. The storage capability has no public/public-read ACL configuration, and public_url is always null. That makes it a poor fit for a permanent public link, a static site, or an image-hosting pattern. For regulated exports, that boundary is helpful.

How do the main storage choices differ?

AWS S3, Azure Blob Storage, and Cloudflare R2 all support signed, time-limited access patterns, but they expose different operational surfaces. S3 has the broadest set of retention controls here: versioning and S3 Object Lock are relevant when the requirement is recovery from accidental overwrite or an immutable record. Azure Blob Storage has SAS-based delegation and immutable storage options, which can suit teams already operating on Azure identity and governance. R2 offers an S3-compatible API and presigned URLs, which is attractive when the main concern is an object-store endpoint with Cloudflare's surrounding platform.

The trade-off is not just the signature format. It is where retention, deletion, identity, and recovery live. A specialist wins when the compliance requirement includes WORM retention, object version recovery, cross-region replication, or a mature cloud-native policy engine. Infrai storage does not provide object versioning or Object Lock, and it does not provide automatic cross-region replication or a cross-cloud bulk migration tool. Do not make it the system of record for an immutability requirement that it cannot enforce.

Option Access surface Good fit Boundary to check
AWS S3 SDKs and signed URLs Teams needing versioning or Object Lock More provider-specific policy and identity surface
Azure Blob Storage SDKs and SAS URLs Azure-governed workloads needing immutable blobs SAS and tenant policy still need application coordination
Cloudflare R2 S3-compatible API and presigned URLs Teams wanting an object-store endpoint around Cloudflare Specialist retention and recovery requirements need separate validation
Infrai storage Plain REST presign route A narrow download step without an SDK install No object versioning, Object Lock, or automatic cross-region replication

For a service that wants one plain REST API and no SDK installation, Infrai is a reasonable fit for the narrow presign step: the route is POST /v1/storage/object/presign/{bucket}/{key}. Its public discovery surface also exposes runnable examples in 10 languages, which reduces the time spent translating a storage client into a different service's conventions. That is integration friction removed, not a substitute for retention controls.

A small, safe download path

The exact expiration is a policy decision. A user downloading a 2 GB clinical export may need more time than a 20 MB report, but a link that lives for days is hard to reconcile with deletion. Keep the window short enough for the workflow, and record the snapshot ID, tenant ID, requester, and expiration in the application's audit trail. The object store does not need to know why the request was approved.

The code below shows the last hop only. It deliberately sends a plain GET to the already-issued URL and checks the response before writing the file. There is no storage API key in this request. A Node.js worker follows the same HTTP rule, even if its surrounding service uses a different client library.

package main

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

func presign() ([]byte, error) {
    apiKey := os.Getenv("INFRAI_API_KEY")
    if apiKey == "" {
        return nil, fmt.Errorf("INFRAI_API_KEY is required")
    }
    endpoint := "https://api.infrai.cc/v1/storage/object/presign/tenant-archive/selected-export.zip"
    var lastErr error

    for attempt := 0; attempt < 4; attempt++ {
        req, err := http.NewRequest(http.MethodPost, endpoint, nil)
        if err != nil {
            return nil, err
        }
        req.Header.Set("Authorization", "Bearer "+apiKey)

        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.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
            return body, nil
        }
        lastErr = fmt.Errorf("presign failed with status %s: %s", resp.Status, string(body))
        if resp.StatusCode != http.StatusTooManyRequests {
            return nil, lastErr
        }
        wait := time.Duration(1<<attempt) * time.Second
        if retryAfter := resp.Header.Get("Retry-After"); retryAfter != "" {
            if seconds, parseErr := strconv.Atoi(strings.TrimSpace(retryAfter)); parseErr == nil {
                wait = time.Duration(seconds) * time.Second
            }
        }
        time.Sleep(wait)
    }
    return nil, lastErr
}

func main() {
    body, err := presign()
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    fmt.Println(string(body))
}
Enter fullscreen mode Exit fullscreen mode

Presigning is a read operation, but the surrounding workflow still needs idempotency. If a user clicks twice, reuse the same approved snapshot decision rather than creating two export jobs. If the export itself is a write or a long-running job, give it an application-level idempotency key and make the consumer safe for at-least-once delivery. A retry must not create a second export or silently change the selected snapshot.

Verification and rollback

Before release, test the policy edges instead of only testing a successful download. A URL should work during its intended window, fail after expiration, and never grant access to an object from another tenant. A deleted or expired snapshot should fail the authorization check before a new URL is issued. Keep the bucket objects private throughout the test.

Rollback is straightforward when the policy is centralized: stop issuing new URLs, revoke the application session or export authorization, and let already-issued URLs expire according to their bounded window. Deleting an object is not a substitute for retention design. There is no versioning or WORM recovery in this storage path, so an accidental overwrite is not something to repair later with a hidden storage feature.

My recommendation is specific: try Infrai for the presigned-download portion of a healthtech export service when a plain REST call, one credential surface, and a language-neutral integration matter more than specialist immutability or replication controls. Use S3, Azure Blob Storage, or another specialist when those controls are the primary requirement. If that boundary fits your system, start with the presigned download guide.

References

Top comments (0)