Menu

1 / 6

Upload files to Cloudflare R2 from Next.js

DEV Community: nextjs·dimah·1 day ago
#wAe9VtPE
#devtonextjs#devto#dev#dimah#server
Long article (1324 words) — no AI summary yet.
Reading 0:00
15s threshold

Cloudflare R2 is S3-compatible object storage with no egress fees, which makes

it a good home for user uploads. The hard part is getting the file there.

Sending the file through your Next.js server works until it doesn't. On Vercel,

a function accepts at most 4.5 MB in its request body, and every byte you pass

through costs function time. The usual fix is a presigned URL: your server

signs a short-lived URL, and the browser uploads straight to R2.

This guide builds that in a Next.js App Router app with dimah-s3. At the end you

have a dropzone that uploads images to R2, a server that picks the object key,

and a callback that runs only after R2 has the file.

How it works

The browser asks your server to upload photo.png on the avatar route.

Your server runs your checks, picks the object key, and signs a PUT URL for R2.

The browser uploads the file straight to R2.

Your server reads the stored object with HeadObject and runs onConfirmed with the size and type R2 reports.

Your R2 keys stay on the…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.