Skip to Content

Files

Store photos, clips and firmware in Floor's blob storage, with signed URLs that browsers and devices use directly.

This page shows how a backend lets a dashboard or a device upload a file and fetch it back, without the bytes passing through the backend. You need a backend (see Your first backend).

Each project has its own folder in Floor's blob storage, an S3-compatible store. On Floor's server that store is Garage, reached at files.usefloor.dev: a hostname of its own, so a stored file can never act as Floor's console. File bytes never go through Floor or your backend: the backend asks Floor for a short-lived signed URL, and the browser or device talks to the store with it.

Keys, sizes and quota

  • Keys are relative to the project's folder. visits/4821/plate.jpg is stored as sac/visits/4821/plate.jpg. A key is 1 to 900 bytes, with no leading / and no . or .. segments.
  • Every upload states its size. Each upload URL is signed for an exact Content-Length, so the store refuses a body of any other length.
  • Each project has a quota, 50 GiB unless an operator sets another. Usage is counted every 15 minutes, plus uploads signed since then. An upload that wouldn't fit is refused.

1. Give the dashboard an upload URL

A guard attaches a photo to a visit. The route checks who's asking, picks the key, and asks Floor for a URL:

// api/visits/[id]/photo.rs  →  POST /api/visits/{id}/photo
use std::collections::BTreeMap;
use floor::prelude::*;

#[derive(Deserialize, Type)]
pub struct PhotoRequest {
    size: u64,
    content_type: String,
}

#[derive(Serialize, Type)]
pub struct UploadTo {
    key: String,
    url: String,
    method: String,
    headers: BTreeMap<String, String>,
}

#[post(can = "visits:write")]
pub async fn post(cx: Cx, Path(id): Path<i64>, Json(req): Json<PhotoRequest>) -> Result<Json<UploadTo>> {
    if req.size > 10 * 1024 * 1024 {
        return Err(Error::bad_request("a photo is at most 10 MB"));
    }
    if req.content_type != "image/jpeg" && req.content_type != "image/png" {
        return Err(Error::bad_request("send a JPEG or PNG"));
    }
    let key = format!("visits/{id}/photo");
    let signed = cx
        .upload_url(Upload::new(&key, req.size).content_type(&req.content_type))
        .await?;
    Ok(Json(UploadTo { key, url: signed.url, method: signed.method, headers: signed.headers }))
}
  • Upload::new(key, size) is required; .content_type(t) makes the upload send that type; .expires_in(d) sets how long the URL works (default 15 minutes).
  • The answer (SignedUrl) has url, method (PUT), headers to send, and expires_at. It doesn't derive Type, so return your own struct as above.
  • The backend decides the key. Don't let the caller choose it, or one visit's photo could overwrite another's.

2. Upload from the browser

The dashboard asks for the URL, then sends the file straight to the store:

const up = await api.visits.$id.photo.post(visitId, { size: file.size, content_type: file.type })
const res = await fetch(up.url, { method: up.method, headers: up.headers, body: file })
if (!res.ok) throw new Error(`upload refused: ${res.status}`)

The browser sets Content-Length itself, which is why it isn't in headers. Floor's file host allows uploads and downloads from any origin (CORS), so this works from any dashboard; the signature is what limits each URL to one key, size and type.

3. Upload from a device

A gate controller sends a snapshot of each plate read. It asks the backend over a device route:

// api/gates/snapshot.rs  →  POST /api/gates/snapshot
use floor::prelude::*;

#[derive(Deserialize, Type)]
pub struct SnapshotRequest {
    event_id: String,
    size: u64,
}

#[derive(Serialize, Type)]
pub struct SnapshotUrl {
    url: String,
}

#[post(device)]
pub async fn post(cx: Cx, Json(req): Json<SnapshotRequest>) -> Result<Json<SnapshotUrl>> {
    let key = format!("snapshots/{}/{}.jpg", cx.device()?, req.event_id);
    let signed = cx
        .upload_url(Upload::new(key, req.size).content_type("image/jpeg"))
        .await?;
    Ok(Json(SnapshotUrl { url: signed.url }))
}

The device then sends the bytes with PUT:

curl -fsS -X PUT -H "Content-Type: image/jpeg" --upload-file plate.jpg "$URL"

4. Hand out a download URL

// api/visits/[id]/photo.rs, next to the POST above
#[derive(Serialize, Type)]
pub struct PhotoLink {
    url: String,
    expires_at: String,
}

#[get(can = "visits:read")]
pub async fn get(cx: Cx, Path(id): Path<i64>) -> Result<Json<PhotoLink>> {
    let signed = cx
        .download_url(&format!("visits/{id}/photo"), Some(std::time::Duration::from_secs(600)))
        .await?;
    Ok(Json(PhotoLink { url: signed.url, expires_at: signed.expires_at }))
}

The URL is a plain GET, so it works as an <img src> or a link. Without an expiry it lasts an hour; at most 7 days. A URL can't be taken back once handed out, so keep them short for anything private.

5. Delete a file

cx.delete_blob(&format!("visits/{id}/photo")).await?;

Deleting a project doesn't delete its files; remove them first if they must go.

Large files: multipart

For anything over about 100 MB, or uploads over unreliable links (camera clips, firmware over 3G), use S3 multipart: each part goes up on its own, so a dropped connection costs one part. Cx doesn't sign multipart uploads yet. Use Floor's HTTP API with a service client's token (Sign-in):

POST /v1/projects/sac/blobs/multipart            { "key": "clips/gate-2.mp4", "content_type": "video/mp4",
                                                   "size": 734003200, "part_size": 16777216 }
  → { "key": "clips/gate-2.mp4", "upload_id": "…", "part_size": 16777216, "parts": 44 }
POST /v1/projects/sac/blobs/multipart/sign       { "key": …, "upload_id": …, "part_numbers": [1, 2, 3] }
  → { "parts": [{ "part_number": 1, "url": "…" }, …] }      PUT each part's bytes to its URL
POST /v1/projects/sac/blobs/multipart/parts      { "key": …, "upload_id": … }   → the parts already stored
POST /v1/projects/sac/blobs/multipart/complete   { "key": …, "upload_id": … }
POST /v1/projects/sac/blobs/multipart/abort      { "key": …, "upload_id": … }
  • Part sizes. part_size is 5 MiB to 5 GiB (default 16 MiB). Every part is exactly that size except the last.
  • Resuming. Ask multipart/parts which parts the store has, sign and upload the rest, then complete.
  • In a browser, Uppy's S3 multipart plugin speaks this flow; set its getChunkSize to the part_size Floor answered.

A single PUT is at most 5 GiB.

From a terminal

floor blob ls sac --prefix snapshots/
floor blob put sac ./firmware-3.0.11.bin --key firmware/3.0.11.bin   # multipart above 64 MiB
floor blob get sac snapshots/GATE-07/e-1042.jpg -o plate.jpg
floor blob url sac firmware/3.0.11.bin --expires-in 86400
floor blob rm sac firmware/3.0.10.bin

Operators can set lifecycle rules: files under a path older than N days are deleted, checked hourly.

floor blob rules sac --rule snapshots/=30 --rule clips/=90

In the console

The project's Data › Files shows what it keeps, by folder: size, file count and lifecycle rule, with a file browser.

Testing

floor::testing::TestApp's fake Floor answers upload_url, download_url and delete_blob, and records each call. See Testing.

On this page