Cloudflare Worker Proxy R2 Bucket Access
Introduction
Cloudflare R2 is a S3-compatible object storage service that provides a cost-effective and scalable solution for storing and serving data. It is much cheaper than traditional cloud storage services, such as Amazon S3, because it does not charge for data egress. To expose Cloudflare R2 buckets to the public internet with more control, we can use Cloudflare Workers to create a proxy that allows us to access R2 buckets.
In this blog post, I would like to share how to access Cloudflare R2 buckets via Cloudflare Workers.
Cloudflare Worker Proxy R2 Bucket Access
Suppose I have a Cloudflare R2 bucket called r2-trial, and there is an image 2026-02-28-2026-Brazen-Victory-10K/IMG_4332.JPEG in the bucket. My goal is to expose this image to the public internet. Although it can be exposed easily using a Cloudflare-managed https://r2.dev subdomain for non-production use cases, features like WAF custom rules, caching, access controls, or bot management are not available when using the r2.dev development url. To use these features, we must configure our bucket behind a custom domain or a Cloudflare Worker.
Purchasing a custom domain just for hosting images is not necessary. I created Cloudflare Worker delicate-water-7fed.dukeleimao.workers.dev just from the hello world template. To bridge the Worker to the R2 bucket, I will have to follow the steps below:
- Go to Worker settings > Settings > Variables.
- Under R2 Bucket Bindings, click Add Binding.
- Variable name:
MY_BUCKET(This variable name will be used in the Worker code to access the bucket) - R2 bucket: Select
r2-trialfrom the dropdown. - Save and Deploy.
Then I could edit the Worker code to access the R2 bucket and return the image in the response. The Worker code is as follows:
1 | const BUCKET_BINDING = "MY_BUCKET"; |
Then we can access the image via the URL https://delicate-water-7fed.dukeleimao.workers.dev/2026-02-28-2026-Brazen-Victory-10K/IMG_4332.JPEG.
Web application firewalls (WAF) are an essential security layer for web applications, and Cloudflare Workers can be used to implement custom WAF rules to protect our R2 bucket. For example, I can restrict access to the images in the R2 bucket to only our GitHub Pages site by checking the Referer header in the Worker code. If the Referer header does not match our GitHub Pages site, we can return a 403 Forbidden response. The modified Worker code with this WAF rule is as follows:
1 | const BUCKET_BINDING = "MY_BUCKET"; |
Conclusions
Compared to other free hacky solutions for hosting files, such as using GitHub repositories, Cloudflare R2 buckets is a professional, scalable, and manageable solution for hosting files, especially for hosting images for blogs. Compared to the similar solutions from other cloud providers, such as Amazon S3, Cloudflare R2 buckets is much cheaper because it does not charge for data egress. By using Cloudflare Workers as a proxy to access R2 buckets, we can have more control over how the files are accessed and served, and we can also implement custom WAF rules to protect our files from unauthorized access.
Cloudflare Worker Proxy R2 Bucket Access
https://leimao.github.io/blog/Cloudflare-Worker-Proxy-R2-Bucket-Access/