惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
B
Blog
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
月光博客
月光博客
H
Help Net Security
V
Visual Studio Blog
量子位
A
About on SuperTechFans
博客园 - Franky
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog

Lei Mao's Log Book

2026 FIFA World Cup 备受嘲讽的会徽 Python Debugging Via VS Code In Docker Container Vargas Plateau Regional Park 徒步 Vargas Plateau Regional Park Apex 2026 FIFA World Cup 小组赛赛程 Retaining EXIF Metadata In GIMP San Francisquito Creek Joint Powers Authority 2025 Calendar Photo 麦当劳 The FIFA World Cup 套餐 Ardenwood Historic Farm 徒步 Ardenwood Historic Farm Synchronizations With TorchRec KeyedJaggedTensor Pacific Commons Linear Park 徒步 Pacific Commons Linear Park 2026 San Jose Half Marathon 竞赛 目标 Mountain View Shoreline Park 徒步 Mountain View Shoreline Park PyTorch AOTInductor Hybrid Lowering Carquinez Strait Regional Shoreline 徒步 Carquinez Strait Regional Shoreline PyTorch Triton Kernel Transparent Tracing and Compilation 脸庞 PyTorch Fake Export 2026 BRAIN Foundation 10K 竞赛 2026 Wild and Scenic Film Festival 参观 2026 Wild and Scenic Film Festival 系统工程程序员修 Bug FIFA 官方网站的语言 PyTorch Custom Operation
Cloudflare Worker Proxy R2 Bucket Access
2026-03-06 · via Lei Mao's Log Book

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-trial from 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const BUCKET_BINDING = "MY_BUCKET";

export default {
async fetch(request, env) {
const bucket = env[BUCKET_BINDING];
const url = new URL(request.url);



const key = decodeURIComponent(url.pathname.slice(1));


if (!key) {
return new Response("Bucket connected. Please specify a file path.", {
status: 200,
});
}


const object = await bucket.get(key);

if (object === null) {
return new Response("Image Not Found in " + bucket.name, {
status: 404,
});
}


const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set("etag", object.httpEtag);

return new Response(object.body, { headers });
},
};

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const BUCKET_BINDING = "MY_BUCKET";
const ALLOWED_ORIGIN_LIST = [
"https://yourusername.github.io/",
"https://www.yourdomain.com/",
];

export default {
async fetch(request, env) {
const bucket = env[BUCKET_BINDING];
const url = new URL(request.url);

const referer = request.headers.get("Referer");


const isAllowed =
!!referer &&
ALLOWED_ORIGIN_LIST.some((origin) => referer.startsWith(origin));
if (!isAllowed) {
return new Response("Access Denied: You cannot hotlink these images.", {
status: 403,
});
}



const key = decodeURIComponent(url.pathname.slice(1));


if (!key) {
return new Response("Bucket connected. Please specify a file path.", {
status: 200,
});
}


const object = await bucket.get(key);

if (object === null) {
return new Response("Image Not Found in " + bucket.name, {
status: 404,
});
}


const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set("etag", object.httpEtag);

return new Response(object.body, { headers });
},
};

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.