Object Storage
R2, S3, MinIO, B2 and Spaces — one driver, no AWS SDK
The adapter
import { getStorage } from '@basicbenframework/core/storage'
const storage = await getStorage()
await storage.put('media/a.png', bytes, { contentType: 'image/png' })
await storage.get('media/a.png') // → { body, contentType, size, etag }
await storage.head('media/a.png') // → metadata, or null
await storage.delete('media/a.png')
await storage.list({ prefix: 'media/' }) // → { items, cursor }
storage.signedUrl('media/a.png', { method: 'PUT', expiresIn: 900 })
storage.publicUrl('media/a.png')One driver, not two
R2 speaks the S3 API, and so do MinIO, Backblaze B2 and DigitalOcean Spaces. The difference between them is an endpoint and a region, so there is one driver and no branching in application code. Moving from R2 to S3 is two lines of config.
storage: {
driver: 's3',
endpoint: 'https://<account-id>.r2.cloudflarestorage.com',
region: 'auto',
bucket: process.env.S3_BUCKET,
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
publicUrl: 'https://cdn.example.com' // optional CDN or custom domain
}storage: {
driver: 's3',
region: 'us-east-1',
bucket: process.env.S3_BUCKET,
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY
}Configure nothing and the local driver writes to public/uploads, so a new project works before anyone has a cloud account. The app serves that directory itself, in development and in production — for a while it only did so in development, because a production build serves dist/client, which is written before any upload exists.
publicUrl() returns an app-relative path on that driver and an absolute one on S3. The content API resolves both against APP_URL before handing them to a consumer, since a headless reader on another host has nothing to resolve a relative path against. Set APP_URL to the origin the site is reached at.
No AWS SDK
@aws-sdk/client-s3 is tens of megabytes for what is, underneath, one HMAC chain. SigV4 is HMAC-SHA256 and SHA-256, both already in node:crypto, so the signer is written here and dependencies stays empty.
A hand-rolled signer is only worth trusting if it is checked against something outside itself, so it is checked two ways: AWS's own 34 published test vectors, stage by stage, and a real MinIO server — the only thing that proves an actual S3 implementation accepts what it produces. That second check earned its place immediately: S3 does not normalize signed paths the way every other AWS service does, and the published vectors would never have caught it.
How an upload works
The browser PUTs straight to the bucket. This server signs a URL and records a row; the file bytes never pass through Node.
Browser BasicBen R2 / S3
│ POST /api/media/sign │ │
│─────────────────────────>│ (validate, then sign) │
│ { uploadUrl, key, │ │
│ ticket, headers } │ │
│<─────────────────────────│ │
│ PUT <uploadUrl> ────── file bytes ──────────────> │
│ POST /api/media/confirm │ │
│─────────────────────────>│ HEAD, then INSERT │Content type and size are validated before anything is signed, because a caller without a signed URL cannot upload at all — that is the enforcement point, not a courtesy check. HTML, SVG and JavaScript are refused outright: a bucket served from a domain hands those back with the type they were stored under, which is same-origin script execution.
Two things a presigned URL does not do
It does not cap the size. A URL issued for a thumbnail will accept a gigabyte. confirm therefore HEADs the stored object and deletes it if it came back larger than allowed. The declared size is checked too, but only the stored size is believed.
It does not say who uploaded what. The key travels through the browser, so each signed upload carries a ticket — an HMAC over the key, the owner and the expiry — checked at confirm time. Without it a caller could confirm someone else's object as its own media row. It is stateless, so there is no pending-upload table to clean up.
Hooks
hooks.on('media.uploading', (upload) => ({
...upload,
key: `tenant-7/${upload.key}`
}))
hooks.on('media.uploading', (upload) => ({
...upload,
cancel: true,
reason: 'Quota exceeded.'
}))
hooks.on('media.uploaded', ({ key, url }) => purgeCdn(url))
hooks.on('media.deleted', ({ key }) => purgeCdn(key))