Media Storage
The media library keeps its files in a storage. Without config, files go to ./.uploads on the local disk. To keep them somewhere else, set media.storage in tenon.config.ts:
import { s3 } from "tenoncms/media/s3";
export default defineCms({
// ...
media: {
storage: s3({
bucket: process.env.S3_BUCKET!,
region: process.env.AWS_REGION!,
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
}),
},
});
The other options of media, like maxSize and accept, are in Media.
Pick a storage
| Storage | Import | Runs on | Uploads go |
|---|---|---|---|
local() | tenoncms/media/local | Node, Bun or Deno with a disk | Through your server, part by part |
s3() | tenoncms/media/s3 | Node, Vercel, Workers | From the browser straight to the bucket |
r2() with keys | tenoncms/media/r2 | Node, Vercel, Workers | From the browser straight to the bucket |
r2() with a binding | tenoncms/media/r2 | Cloudflare Workers | Through the Worker, part by part |
- On a Node server with a disk that keeps its files,
local()needs no setup. - Serverless hosts like Vercel have a read-only disk. There, the default storage does not work, and you need a bucket:
s3()orr2()with keys. - On Cloudflare Workers, use
r2()with a binding.tenoncms/media/localthrows there.
On a serverless host or on Workers, a config without media has no media library, and media: true throws.
npm create tenoncms asks for the storage and writes its config and variables. See create-tenoncms.
Local disk
local() keeps the files in a folder. Each folder of the media library is a folder on the disk:
import { local } from "tenoncms/media/local";
export default defineCms({
// ...
media: {
storage: local({ dir: process.env.UPLOADS_DIR ?? ".uploads" }),
},
});
dir, when your web server serves the folder itself, like https://cdn.example.com/uploads. Default: Tenon serves the files at /_tenon/media/files/<key>.- Each part is one request to your server. A reverse proxy's body limit, like
client_max_body_sizein nginx, MUST be larger thanpartSize. nuxt previewruns the server from inside.output/, so a relativedirends up in the build output. Resolve the path innuxt.config.ts, likeuploadsDir: resolve(".uploads")inruntimeConfig, and pass that value.- Keep
.uploadsout of git, and include it in your backups.
S3
s3() works with AWS S3 and any S3-compatible service, like MinIO or Neon's object storage. The browser uploads each part straight to the bucket. Your server only signs the requests, so the request limits of the host do not apply, like 4.5 MB on Vercel.
import { s3 } from "tenoncms/media/s3";
export default defineCms({
// ...
media: {
storage: s3({
bucket: process.env.S3_BUCKET!,
region: process.env.AWS_REGION!,
endpoint: process.env.AWS_ENDPOINT_URL_S3,
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
prefix: process.env.S3_PREFIX,
}),
},
});
eu-central-1.credentials.credentials.http://localhost:9000 for MinIO. Default: AWS.staging. See Share a bucket. Default: the root of the bucket.<publicUrl>/<key>. Default: the bucket stays private, and Tenon serves the files.{ accessKeyId, secretAccessKey, sessionToken?, expiration? }. Tenon calls it again when the credentials expire within 5 minutes.fetch. Default: the global fetch.s3() signs requests with aws4fetch, so it runs on Node and on Workers. The bucket needs CORS.
- MinIO: set
endpoint, likehttp://localhost:9000, to test locally withdocker run -p 9000:9000 minio/minio server /data. - Neon:
endpointis the S3 endpoint of the project, andregionits region. See Vercel. - AWS S3 from the Vercel Marketplace: the bucket has no keys. Pass
credentials: awsCredentialsProvider({ roleArn: process.env.AWS_ROLE_ARN! })from@vercel/oidc-aws-credentials-provider.
Share a bucket
With prefix, several sites share one bucket. Each site keeps its files in its own folder, like staging/ and production/.
- Public file URLs are
<publicUrl>/<prefix>/<key>. The URLs that Tenon serves stay the same. - A new
prefixdoes not move the files that are already stored. - Lifecycle rules and CDN paths MUST include the prefix.
R2
r2() stores files in Cloudflare R2. It has two modes:
With a binding: r2(env.MEDIA) | With keys: r2({ accountId, … }) | |
|---|---|---|
| Through | A bucket binding of the Worker | R2's S3 API, like s3() |
| Runs on | Cloudflare Workers only | Node, Vercel and Workers |
| Uploads go | Through the Worker | From the browser straight to the bucket |
| Keys and CORS | Not needed | Needed. See Set up the bucket |
prefix | Not supported. Give each site its own bucket | Supported |
With a binding
On Workers, bind the bucket in wrangler.jsonc and pass the binding:
import { env } from "cloudflare:workers";
import { r2 } from "tenoncms/media/r2";
export default defineCms({
// ...
media: {
storage: r2(env.MEDIA),
},
});
{
"r2_buckets": [{ "binding": "MEDIA", "bucket_name": "my-site-media" }],
}
wrangler deploy creates the bucket when it does not exist yet, once R2 is enabled on the account. wrangler dev simulates it locally.
env.MEDIA with env from cloudflare:workers./_tenon/media/files/<key>.- Each part goes through the Worker and, with
tenonObject(), through the CMS's Durable Object. The Worker holds each part in memory. - Bindings cannot copy files. A move reads and writes the file, one part at a time, so a large file does not fill the Worker's memory.
With keys
Anywhere else, r2() uses R2's S3 API with the keys of an R2 API token:
import { r2 } from "tenoncms/media/r2";
export default defineCms({
// ...
media: {
storage: r2({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
bucket: process.env.R2_BUCKET!,
accessKeyId: process.env.R2_ACCESS_KEY_ID!,
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY!,
}),
},
});
https://<accountId>.r2.cloudflarestorage.com."eu" for a bucket created with the EU data location. Requests then go to <accountId>.eu.r2.cloudflarestorage.com. Default: the automatic location.fetch. Default: the global fetch.Set up the bucket
A bucket for s3() or r2() with keys needs two settings. A binding needs neither.
Allow uploads from the admin
The browser uploads parts straight to the bucket, so the bucket's CORS policy MUST allow PUT from the origin of the admin and expose the ETag header:
[
{
"AllowedOrigins": ["https://www.example.com"],
"AllowedMethods": ["GET", "PUT"],
"AllowedHeaders": ["*"],
"ExposeHeaders": ["ETag"],
"MaxAgeSeconds": 3600
}
]
Without ETag, an upload fails with a message that names the CORS setting. GET lets the admin load files from a public bucket when it makes missing previews.
Clean up unfinished uploads
Add a lifecycle rule that aborts incomplete multipart uploads after one day. Tenon aborts its own unfinished uploads after a day too, but only when the next upload starts. The bucket rule catches the rest.
With a public bucket, also delete objects under .tenon/ after one day. Backups pass through that folder. See Large archives.
With a prefix, the rules MUST include it, like staging/.tenon/.
Who serves the files
Without publicUrl or baseUrl, Tenon serves every file at /_tenon/media/files/<key> and reads it from the storage. A private bucket stays private.
- When media has no
readrule, files carryCache-Control: public, max-age=300, s-maxage=3600, so a CDN absorbs most reads. A file that is deleted, moved or replaced under the same name can show for up to an hour. - With a
readrule, files carryprivate, no-cache, and each read checks the rule. - With
publicUrl, file URLs point to the bucket, and the bucket serves them. Access rules cannot guard those files.
When the site and the CMS run on different origins, reads return the URLs that Tenon serves as absolute URLs on the CMS. See Images and files.
Related resources
- Media: the media library, its options and uploads from code.
- Image providers: resized images from the files in your storage.
- Backup and restore: backups with media files.
- Deployment: the storage for each host.