File storage
Files people upload or the app makes: photos, documents, exports.
In stackly.json and the CLI it is storage. Its use, live or draft, is counted as file reads and file writes; see Live app prices.
storage · optional · needs backend · R2 · binding APP_STORAGE
Files people upload or the app makes: photos, documents, exports. The draft and live each have their own bucket.
Turn it on
"features": ["backend", "storage"],
"bindings": ["APP_STORAGE"]
stackly features on storage does both.
Use it
import { env } from "cloudflare:workers";
const key = `uploads/${crypto.randomUUID()}.jpg`;
await env.APP_STORAGE.put(key, await file.arrayBuffer(), file.type);
const stored = await env.APP_STORAGE.get(key); // { body: ArrayBuffer, contentType } | null
await env.APP_STORAGE.delete(key);
- Keys are scoped to this app's environment. Keep the key in a database row next to the record it belongs to.
- Serve a file through an API route that checks who may read it, unless it is public.
- Check the type and size of an upload on the server before saving it.
Locally
stackly dev has a local bucket. stackly storage ls lists what the app wrote.
Related
image-resizing for thumbnails of stored images.