Cloudflare R2 is a robust and cost-effective object storage solution that can be an excellent choice for hosting images for your website or application. This guide will walk you through the process of setting up an R2 bucket, adding a custom domain for public access, and understanding the pricing model.
Why Choose Cloudflare R2?
- Zero Egress Fees: Unlike many other cloud storage providers, Cloudflare R2 does not charge for bandwidth (egress) when your images are downloaded. This can lead to significant cost savings, especially for high-traffic websites.
- Integration with Cloudflare CDN: When you use a custom domain with your R2 bucket, you can leverage Cloudflare's powerful CDN to cache your images and deliver them to users with low latency and high speed.
- Affordable Pricing: R2's pricing is competitive, with a generous free tier that is often sufficient for small to medium-sized projects.
Setting Up Your R2 Bucket
- Navigate to R2 in your Cloudflare dashboard.
- Create a new bucket: Give your bucket a unique name.
- Upload your images: You can upload images directly through the Cloudflare dashboard or use the R2 API for programmatic uploads.
Adding a Custom Domain for Public Access
Using a custom domain for your R2 bucket is the recommended approach for production use. It allows you to have a branded URL for your images (e.g., images.yourdomain.com) and take full advantage of Cloudflare's features.
- Go to your R2 bucket settings.
- Under "Custom Domains", click "Connect Domain".
- Enter the subdomain you want to use (e.g.,
images.yourdomain.com) and click "Continue". - Cloudflare will automatically add the necessary CNAME record to your DNS settings. Review and confirm.
Once the domain is connected (it may take a few minutes to become active), your images will be accessible via your custom domain.
For development and testing, you can also enable a public r2.dev URL for your bucket. However, this is not recommended for production due to rate limiting and the lack of support for features like the WAF and caching.
Cloudflare R2 Pricing
Cloudflare R2's pricing is based on two main components: storage and operations.
Storage
- Standard: $0.015 per GB-month. Ideal for frequently accessed images.
- Infrequent Access: $0.01 per GB-month. A more cost-effective option for images that are not accessed as often.
Operations
Operations are categorized as Class A (writes, listings) and Class B (reads).
- Class A (Writes & Lists): $4.50 per million requests.
- Class B (Reads): $0.36 per million requests.
Free Tier
R2 has a generous free tier that includes:
- 10 GB of storage per month
- 1 million Class A operations per month
- 10 million Class B operations per month
- Free data egress
For many websites, the free tier is more than enough to get started and may even cover all of their image hosting needs.
Conclusion
Cloudflare R2 provides a powerful and affordable solution for hosting images. With its zero egress fees, seamless integration with the Cloudflare CDN, and easy-to-use custom domain setup, it's an excellent choice for developers and businesses looking to optimize their image delivery and reduce costs.