intermediate
R2
Use S3-compatible object storage at the edge while accounting for consistency, egress model, signed URLs, and lifecycle needs.
R2 is S3-compatible object storage at the edge with zero egress fees to Cloudflare services — attractive for user uploads, static assets, and backups accessed through Workers or CDN. Use the AWS SDK or Workers R2 binding with buckets, keys, and optional public access via custom domains.
await env.MEDIA.put(`uploads/${userId}/${fileId}`, stream, {
httpMetadata: { contentType: 'image/webp' },
});
const object = await env.MEDIA.get(key);
| Topic | Detail | |-------|--------| | Consistency | Read-after-write for new keys; list eventual | | Access | Private buckets + signed URLs or Worker gate | | Lifecycle | Rules for expiration and tiering | | Egress model | No CF egress charge; external egress billed |
Pair R2 with Workers for auth uploads instead of wide-open public buckets.
On interviews: R2 vs S3 cost model, presigned upload flows, consistency expectations, serving through CDN, and migration from S3.
Common pitfalls: public buckets with predictable keys; no virus or size validation on upload; listing large prefixes synchronously; assuming strong listing consistency for UI pagination.
The trade-off is economical edge-adjacent storage versus fewer ecosystem tools than AWS S3 and external egress costs.
Checklist:
- Authenticate uploads in Worker or API.
- Use lifecycle rules for temp and log blobs.
- Serve public assets via CDN with cache headers.
- Test restore and key naming conventions early.