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

推荐订阅源

宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
罗磊的独立博客
V
Visual Studio Blog
爱范儿
爱范儿
H
Help Net Security
J
Java Code Geeks
I
InfoQ
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recorded Future
Recorded Future
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
S
SegmentFault 最新的问题
S
Securelist
L
LINUX DO - 热门话题
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
博客园_首页
B
Blog
F
Fortinet All Blogs
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Schneier on Security
Project Zero
Project Zero
Martin Fowler
Martin Fowler
C
Cybersecurity and Infrastructure Security Agency CISA
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic

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.