Simple API to upload, download, and generate a preview link (signed URL) for images/files stored in Cloudflare R2.
- Install
pnpm install- Create
.env
This environment blocks dotfiles in the workspace, so copy env.example → .env on your machine:
cp env.example .envFill your real R2 values in .env.
pnpm devServer: http://localhost:3000
In a separate terminal (with server running):
pnpm test:apiOptional:
API_BASE=http://localhost:3000 pnpm test:api-
Health
GET /health
-
Upload (multipart)
POST /upload- form field:
file - optional:
?key=my-file-name(without extension; extension inferred)
Example:
curl -F "file=@./my-image.jpg" "http://localhost:3000/upload"- Download / inline preview
GET /download/*- Note:
:keyis the object key returned from upload (including any prefix). - Tip: If you set
R2_PREFIX=uploads/, the returned key already includesuploads/...— you should pass it exactly as-is.
Example:
curl -L "http://localhost:3000/download/uploads/12345-abcd.jpg" --output out.jpg- Get signed preview link
GET /preview/*- optional:
?expiresIn=3600
Example:
curl "http://localhost:3000/preview/uploads/12345-abcd.jpg"