This is a workaround solution to serve Cloudlflare R2 images when developping locally. Made with Bun and Hono.
Waiting for this feature from wrangler: cloudflare/workers-sdk#3687
Clone the repo at the root of your project.
git clone https://github.com/emilienbidet/cloudflare-r2-dev-serverInstall dependencies:
cd cloudflare-r2-dev-server
bun installSetup env variables in a .env file, grab them from .wrangler directory :
This is a example .env file:
R2_BUCKET_NAME="kuma"
R2_BUCKET_DABASE_NAME="4728dfd2675ad89b0cdad6672d49dacf952327a5040562ad905e0baa743495c6"
R2_BUCKET_PATH="../.wrangler/state/v3/r2/"
PORT="3000" # optionalStart the server
bun run startTest it by uploading a image to the bucket and by trying access it with http://localhost:3000/:key
🎉 Bravo! All your uploaded r2 images when developping locally are now available from http://localhost:3000/:key
Add cloudflare-r2-dev-server/ to your .gitignore file
Add cloudflare-r2-dev-server to your tsconfig.json file
{
"exclude": [
"node_modules",
"dist",
"cloudflare-r2-dev-server"
]
}