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

推荐订阅源

Project Zero
Project Zero
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
C
Check Point Blog
S
SegmentFault 最新的问题
腾讯CDC
IT之家
IT之家
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
博客园 - Franky
V
Visual Studio Blog
The Register - Security
The Register - Security
GbyAI
GbyAI
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
T
Troy Hunt's Blog
小众软件
小众软件
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
TaoSecurity Blog
TaoSecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
C
CERT Recently Published Vulnerability Notes
aimingoo的专栏
aimingoo的专栏
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
G
GRAHAM CLULEY
T
Tenable Blog
P
Palo Alto Networks Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
H
Help Net Security
WordPress大学
WordPress大学
The Cloudflare Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
W
WeLiveSecurity
I
InfoQ
P
Privacy International News Feed

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 汉堡王 The Mandalorian and Grogu 套餐 Tilden Regional Parks Botanic Garden 参观 Tilden Regional Park Tilden Regional Parks Botanic Garden Tilden Regional Park 徒步 《寻秦记》电影版 ICML 2026 Area Chair Experience 2026 Foster City 5K Fun Run 竞赛 2026 年 3 月和 4 月该入手的模型手办 Docker Container GUI Display Using Wayland 马拉松破二 2026 Heart & Soles Run 5K 竞赛 How Is FARS, The Fully Automated Research System? 算计: 七天的死亡游戏 Lake Chabot Regional Park 徒步 Lake Chabot Regional Park 2023 年恐怖电影《感恩节》 2026 Airport Runway Run at San Carlos Airport 5K 竞赛 Page Table for Page-Locked Host Memory Don Edwards San Francisco Bay National Wildlife Refuge - Ravenswood 徒步 Don Edwards San Francisco Bay National Wildlife Refuge - Ravenswood 法外风云 PyTorch Graph Symbolic Integer Contra Costa Canal Regional Trail 徒步 Contra Costa Canal Regional Trail 娑婆诃 PyTorch Export 2026 Western Pacific 5K 竞赛 浮躁的科研和胡扯的自媒体 Connecting Logitech Devices On Linux 2026 Oakland Half Marathon 竞赛 Del Valle Regional Park 徒步 Del Valle Regional Park CUDA_LAUNCH_BLOCKING=1 踏切时间 Wildcat Canyon Regional Park 徒步 Wildcat Canyon Regional Park Credit Card Unauthorized Transaction While In Possession 莎拉的真伪人生 2026 Union City Superhero Fun Run 5K 竞赛 McLaughlin Eastshore State Park 徒步 McLaughlin Eastshore State Park Zorro 和 Batman Fix MacBook Pro Space Key Stuck Problem 2026 年 1 月和 2 月该入手的模型手办 2026 Brazen Victory 10K 竞赛 儿时的玩伴李峰 Marsh Creek Regional Trail 徒步 Marsh Creek Regional Trail Perfetto GPU Flow Artifacts 百万人推理 System Performance Optimizations QQ 幻想 2026 Brazen Bay Breeze 5K 竞赛 CUDA Shared Memory Bank Conflict-Free Vectorized Access Dota 闪电站出售 Mountain View Downtown 徒步 Mountain View Downtown C++ Latch and Barrier 2025 年跑步总结 2026 Rotary Mission Ten Half Marathon 竞赛 狗的素质等于人的素质 CUDA Rendezvous Stream Pleasanton Ridge Regional Park 徒步 Pleasanton Ridge Regional Park Xfinity Internet 多年来的使用感受 Randomized SVD Don Castro Regional Recreation Area 徒步 Don Castro Regional Recreation Area 拖车公司的大汉们
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.