Databases and Storage

Media Storage

Keep media files on disk, in S3 or in R2.

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:

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

StorageImportRuns onUploads go
local()tenoncms/media/localNode, Bun or Deno with a diskThrough your server, part by part
s3()tenoncms/media/s3Node, Vercel, WorkersFrom the browser straight to the bucket
r2() with keystenoncms/media/r2Node, Vercel, WorkersFrom the browser straight to the bucket
r2() with a bindingtenoncms/media/r2Cloudflare WorkersThrough 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() or r2() with keys.
  • On Cloudflare Workers, use r2() with a binding. tenoncms/media/local throws 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:

tenon.config.ts
import { local } from "tenoncms/media/local";

export default defineCms({
  // ...
  media: {
    storage: local({ dir: process.env.UPLOADS_DIR ?? ".uploads" }),
  },
});
The folder that holds the files. It MUST be on a disk that keeps its files between deploys. A relative path starts in the working directory. Use an absolute path in production.The size of one upload part, in bytes. Default: 16 MB.The public URL of 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_size in nginx, MUST be larger than partSize.
  • nuxt preview runs the server from inside .output/, so a relative dir ends up in the build output. Resolve the path in nuxt.config.ts, like uploadsDir: resolve(".uploads") in runtimeConfig, and pass that value.
  • Keep .uploads out 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.

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!,
      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,
    }),
  },
});
The name of the bucket.The region, like eu-central-1.The access key. Leave it out when you pass credentials.The secret key. Leave it out when you pass credentials.The endpoint of an S3-compatible service, like http://localhost:9000 for MinIO. Default: AWS.A folder inside the bucket that holds every file, like staging. See Share a bucket. Default: the root of the bucket.The public URL of the bucket, like a custom domain. File URLs are then <publicUrl>/<key>. Default: the bucket stays private, and Tenon serves the files.The size of one upload part, in bytes. At least 5 MB, as S3 requires. Default: 16 MB.Temporary credentials instead of the two keys. The function returns { accessKeyId, secretAccessKey, sessionToken?, expiration? }. Tenon calls it again when the credentials expire within 5 minutes.Your own 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, like http://localhost:9000, to test locally with docker run -p 9000:9000 minio/minio server /data.
  • Neon: endpoint is the S3 endpoint of the project, and region its 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 prefix does 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, … })
ThroughA bucket binding of the WorkerR2's S3 API, like s3()
Runs onCloudflare Workers onlyNode, Vercel and Workers
Uploads goThrough the WorkerFrom the browser straight to the bucket
Keys and CORSNot neededNeeded. See Set up the bucket
prefixNot supported. Give each site its own bucketSupported

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),
  },
});

wrangler deploy creates the bucket when it does not exist yet, once R2 is enabled on the account. wrangler dev simulates it locally.

The bucket binding, like env.MEDIA with env from cloudflare:workers.The size of one upload part, in bytes. At least 5 MB, as R2 requires. Default: 16 MB.The public URL of the bucket, like its custom domain. Default: Tenon serves the files at /_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:

tenon.config.ts
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!,
    }),
  },
});
Your Cloudflare account ID. Requests go to https://<accountId>.r2.cloudflarestorage.com.The name of the bucket.The access key ID of an R2 API token.The secret access key of an R2 API token.Set "eu" for a bucket created with the EU data location. Requests then go to <accountId>.eu.r2.cloudflarestorage.com. Default: the automatic location.A folder inside the bucket that holds every file. See Share a bucket.The public URL of the bucket, like its custom domain. Default: the bucket stays private, and Tenon serves the files.The size of one upload part, in bytes. At least 5 MB. Default: 16 MB.Your own 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:

cors.json
[
  {
    "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 read rule, files carry Cache-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 read rule, files carry private, 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.