Skip to content
Back to Cloud Services

Cloudflare R2 Object Storage

S3-compatible object storage with zero egress fees, ideal for media, backups, and large datasets.

CloudflareIntermediate

Why

R2 charges $0.015/GB/month storage but $0 egress — S3 charges $0.09/GB egress. For a 100GB site serving 1TB/month egress, R2 saves $90/mo.

S3 API compatibility means drop-in replacement.

When

Use R2 for user uploads, video/images served to users, database backups, and ML datasets. Avoid as primary DB or for objects needing frequent strong consistency writes.

How

1. Create bucket — wrangler r2 bucket create my-bucket (or dashboard) 2. S3 API — generate API token, use endpoint https://<account>.r2.cloudflarestorage.com 3. Bind to Worker — [[r2_buckets]] binding="BUCKET" bucket_name="my-bucket" 4. Upload — wrangler r2 object put my-bucket/file.png --file=./local.png 5. Public access — custom domain on bucket, or serve via Worker (signed URLs) 6. Lifecycle — auto-delete after N days via bucket rules 7. Migrate from S3 — rclone or aws s3 sync with R2 endpoint

Example

// Worker serving R2 object with range support (video streaming) export default { async fetch(request: Request, env: Env): Promise<Response> { const url = new URL(request.url); const object = await env.BUCKET.get(url.pathname.slice(1), { range: request.headers.get("Range") || undefined, }); if (!object) return new Response("Not found", { status: 404 }); const headers = new Headers(); object.writeHttpMetadata(headers); headers.set("etag", object.httpEtag); if (object.range) headers.set("Content-Range", `bytes ${object.range.offset}-${object.range.offset + object.range.length - 1}/${object.size}`); return new Response(object.body, { headers }); }, }; // Migrate S3 → R2 rclone copy s3:my-s3-bucket r2:my-r2-bucket --transfers 32

Common Problems

  • ⚠R2 eventual consistency on overwrite — reads may return stale object right after PUT
  • ⚠No bucket-level public access — must serve through Worker or custom domain, unlike S3 website endpoint
  • ⚠Multipart upload >5GB needs S3 API — wrangler CLI doesn't support multipart yet

Best Practices

  • ✓Serve media through Worker with Cache API — edge caching makes R2 reads nearly free
  • ✓Use lifecycle rules for backups — auto-tier to delete after 90 days, avoid manual cleanup
  • ✓Generate presigned URLs for uploads — clients upload directly to R2, save Worker CPU

FAQ

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

S3-compatible object storage with zero egress fees, ideal for media, backups, and large datasets. 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 R2 Object Storage in 5 minutes?

Two steps to run it: (1) Create bucket — wrangler r2 bucket create my-bucket (or dashboard); (2) S3 API — generate API token, use endpoint https://<account>.r2.cloudflarestorage.com. Validate on the free tier first, then wire into production.

What is the #1 pitfall with Cloudflare R2 Object Storage?

The most frequent issue is: R2 eventual consistency on overwrite — reads may return stale object right after PUT. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for Cloudflare R2 Object Storage?

Best practices for Cloudflare R2 Object Storage include: Serve media through Worker with Cache API — edge caching makes R2 reads nearly free; Use lifecycle rules for backups — auto-tier to delete after 90 days, avoid manual cleanup; Generate presigned URLs for uploads — clients upload directly to R2, save Worker CPU. The first one — "Serve media through Worker with Cache API — edge caching makes R2 reads nearly free" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with Cloudflare R2 Object 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 R2 Object Storage options offer a free tier — validate within free quota before scaling.