Cloudflare R2 Integration
R2 is an object storage service provided by Cloudflare that seamlessly integrates with Cloudflare's CDN service, serving as a static file storage and distribution solution.
There are many scenarios requiring file storage, for example:
- User avatar storage
- AI-generated images and videos saved to the cloud
- Blog image storage
Once these scenario requirements are identified, administrators also need a file management backend.
The Nexty.dev boilerplate addresses these needs by providing file upload and deletion methods built on Cloudflare R2, along with a backend for viewing and managing files.
In this chapter, we'll complete the Cloudflare R2 configuration.
Configuring Cloudflare R2
- Visit the Cloudflare R2 page and start creating a Bucket


- Enter Bucket Settings and copy
R2_BUCKET_NAME
andR2_ACCOUNT_ID
to environment variables

- Set up custom domain


- Add the configured custom domain with
https://
prefix to the environment variableR2_PUBLIC_URL

# .env.local or .env
R2_PUBLIC_URL=https://R2_PUBLIC_URL
- Configure CORS Policy, which controls which origins (domains, protocols, ports) can access resources stored in the R2 Bucket. This is a security mechanism to protect your resources from unauthorized cross-origin access.


[
{
"AllowedOrigins": [
"http://127.0.0.1:3006",
"http://localhost:3006",
"https://kontextimage.com",
"https://flux-kontext.nexty.dev"
],
"AllowedMethods": [
"GET",
"PUT",
"HEAD"
],
"AllowedHeaders": [
"Content-Type",
"Content-Length"
],
"ExposeHeaders": [
"Content-Length",
"Content-Type",
"Content-Disposition",
"ETag",
"Last-Modified"
],
"MaxAgeSeconds": 3600
}
]
- Now return to the R2 homepage and start creating an API Key



- Add the API Key to environment variables
R2_ACCESS_KEY_ID
andR2_SECRET_ACCESS_KEY

Verification
After starting the source code repository, you can test R2-related functionality in the following modules:
/dashboard/settings
page to test avatar upload/ai-demo
page to test AI-generated image and video upload/dashboard/images
page to manage AI Demo generated images and videos