Skip to content
Back to Cloud Services

Cloudflare Pages & R2 Storage

Deploy full-stack apps on Cloudflare Pages with R2 (S3-compatible, zero egress fees) and D1 edge SQLite.

CloudflareIntermediate

Why

R2 charges $0 egress (vs S3 $0.09/GB), saving 80% on image/video heavy apps. Pages Functions run on Workers runtime (300+ edge locations, 50ms cold start).

Combined: full-stack edge apps with no egress bill.

When

Choose Pages + R2 for image galleries, video hosting, file-sharing apps, and any workload where egress dominates cost. Avoid for apps needing long-running compute (use Cloud Run) or VPC-private resources.

How

1. Create Pages project — npx wrangler pages project create myapp 2. Bind R2 — wrangler r2 bucket create assets, add binding in dashboard 3. Pages Functions — functions/api/[[route]].ts for API, on-demand at edge 4. Upload to R2 — wrangler r2 object put assets/file.jpg --file=./file.jpg 5. Serve via binding — env.ASSETS.get("file.jpg") returns ReadableStream 6. Custom domain — Pages dashboard > Custom domains > add + verify DNS 7. Deploy — wrangler pages deploy ./dist (or connect Git for auto-deploy) 8. Monitor — Cloudflare Analytics (R2 operations, Pages requests, all free)

Example

// wrangler.toml — Pages + R2 + D1 bindings name = "myapp" pages_build_output_dir = "./dist" [[r2_buckets]] binding = "ASSETS" bucket_name = "my-assets" [[d1_databases]] binding = "DB" database_name = "mydb" // functions/api/upload.ts — R2 upload at edge export const onRequestPost = async (ctx: EventContext) => { const form = await ctx.request.formData(); const file = form.get("file") as File; const key = `uploads/${crypto.randomUUID()}-${file.name}`; await ctx.env.ASSETS.put(key, file.stream(), { httpMetadata: { contentType: file.type }, }); return Response.json({ url: `/api/file/${key}`, size: file.size }); }; // functions/api/file/[key].ts — serve from R2 export const onRequestGet = async (ctx: EventContext) => { const key = ctx.params.key; const obj = await ctx.env.ASSETS.get(`uploads/${key}`); if (!obj) return new Response("Not found", { status: 404 }); return new Response(obj.body, { headers: { "Content-Type": obj.httpMetadata.contentType || "application/octet-stream" }, }); };

Common Problems

  • ⚠R2 object not found — key is case-sensitive, check exact path and leading slashes
  • ⚠Pages Functions 50ms CPU limit — image processing hits wall, use Image Resizing instead
  • ⚠Multipart upload >100MB — R2 single PUT limit, use multipart upload API
  • ⚠D1 write conflicts — single primary, concurrent writes fail, use KV for counters

Best Practices

  • ✓Use R2 for all user uploads — zero egress means CDN serving is free
  • ✓Cache R2 responses with Cache API — set Cache-Control, edge caches for free
  • ✓Use Cloudflare Images for transforms — offloads resize/compress from your code
  • ✓Git-connected Pages for auto-deploy — push to main, preview deploy per PR

FAQ

What is Cloudflare Pages & R2 Storage and what problem does it solve?

Deploy full-stack apps on Cloudflare Pages with R2 (S3-compatible, zero egress fees) and D1 edge SQLite. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with Cloudflare Pages & R2 Storage in 5 minutes?

Two steps to run it: (1) Create Pages project — npx wrangler pages project create myapp; (2) Bind R2 — wrangler r2 bucket create assets, add binding in dashboard. Validate on the free tier first, then wire into production.

What is the #1 pitfall with Cloudflare Pages & R2 Storage?

The most frequent issue is: R2 object not found — key is case-sensitive, check exact path and leading slashes. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for Cloudflare Pages & R2 Storage?

Best practices for Cloudflare Pages & R2 Storage include: Use R2 for all user uploads — zero egress means CDN serving is free; Cache R2 responses with Cache API — set Cache-Control, edge caches for free; Use Cloudflare Images for transforms — offloads resize/compress from your code. The first one — "Use R2 for all user uploads — zero egress means CDN serving is free" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with Cloudflare Pages & R2 Storage?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most Cloudflare Pages & R2 Storage options offer a free tier — validate within free quota before scaling.