ค่า Egress หรือค่า Data Transfer ออกจาก Cloud Storage คือหนึ่งในค่าใช้จ่ายที่สร้างความเจ็บปวดหัวใจที่สุดในการใช้งาน S3 หรือ Storage Provider ทั่วไป Cloudflare R2 คือ Object Storage ที่ช่วยแก้ปัญหานี้ด้วยการไม่คิดค่า Egress เลย เหมาะอย่างยิ่งสำหรับเว็บไซต์ที่ใช้ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง และต้องการเก็บไฟล์ขนาดใหญ่จำนวนมาก
Cloudflare R2 คืออะไร?
R2 เป็น Object Storage Service ที่ Compatible กับ S3 API ของ Amazon โดยไม่คิดค่า Egress Fee เลย ทำให้คุณประหยัดค่าใช้จ่ายได้อย่างมีประสิทธิภาพเมื่อเทียบกับผู้ให้บริการรายอื่น
คุณสมบัติสำคัญ:
- ไม่มีค่า Egress — ไม่จ่ายค่าเมื่อดึงไฟล์ออก ต่างจาก AWS S3
- S3 Compatible API — ใช้ SDK เดิมได้เลยโดยไม่ต้องแก้โค้ด
- Free Tier — 10 GB Storage + 1 ล้าน Class A Operations/เดือน
- CDN Integration — เสิร์ฟไฟล์ผ่าน Cloudflare CDN ได้ทันที
- Workers Integration — Workers อ่านและเขียน R2 ได้โดยตรง
เปรียบค่าใช้จ่ายระหว่าง R2 กับ S3
| รายการ | Cloudflare R2 | AWS S3 |
|---|---|---|
| Storage | $0.015/GB/เดือน | $0.023/GB/เดือน |
| Egress Fee | ไม่คิดค่า | $0.09/GB ขึ้นไป |
| Class A Ops (Write) | $4.50/ล้าน | $5.00/ล้าน |
| Class B Ops (Read) | $0.36/ล้าน | $0.40/ล้าน |
| Free Tier | 10 GB + 1M Writes/เดือน | 5 GB (1 ปีแรก) |
การใช้งาน R2 กับ Cloudflare Workers
ตั้งค่า R2 Binding
# สร้าง R2 Bucket
wrangler r2 bucket create my-bucket
# เพิ่มใน wrangler.toml
[[r2_buckets]]
binding = "MY_BUCKET"
bucket_name = "my-bucket"
อัปโหลดและดึงไฟล์
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const key = url.pathname.slice(1); // เอาชื่อไฟล์จาก URL
if (request.method === 'PUT') {
// อัปโหลดไฟล์
await env.MY_BUCKET.put(key, request.body, {
httpMetadata: {
contentType: request.headers.get('Content-Type') || 'application/octet-stream'
}
});
return new Response(`File '${key}' uploaded`, { status: 200 });
}
if (request.method === 'GET') {
// ดึงไฟล์
const object = await env.MY_BUCKET.get(key);
if (!object) {
return new Response('File not found', { status: 404 });
}
return new Response(object.body, {
headers: {
'Content-Type': object.httpMetadata?.contentType || 'application/octet-stream',
'Content-Length': object.size.toString()
}
});
}
return new Response('Method Not Allowed', { status: 405 });
}
};
การเชื่อม R2 กับ Custom Domain
สามารถเชื่อม R2 Bucket กับ Custom Domain เพื่อเสิร์ฟไฟล์ผ่าน CDN ได้เลย:
- ไปที่ R2 Bucket Settings → Custom Domains
- คลิก Connect Domain
- ใส่ชื่อ Domain เช่น
files.example.com - Cloudflare จะตั้งค่า CNAME ให้อัตโนมัติ
การใช้ R2 เป็น Backup Storage สำหรับ VPS
สำหรับลูกค้า ผู้ให้บริการโฮสติ้ง ที่ใช้ Cloud VPS สามารถใช้ R2 เป็น Backup Storage ผ่าน AWS CLI หรือ rclone:
# ติดตั้ง rclone และ Config R2
rclone config
# เลือก S3 และใส่ข้อมูล R2:
# Endpoint: https://ACCOUNT_ID.r2.cloudflarestorage.com
# Access Key: R2 Access Key ID
# Secret Key: R2 Secret Access Key
# สำรองโฟลเดอร์ไปยัง R2
rclone sync /var/backups cloudflare-r2:my-bucket/backups
สรุป
Cloudflare R2 เป็น Object Storage ที่คุ้มค่ามากที่สุดในตลาดเนื่องจากไม่คิดค่า Egress เรื่อง Download ไฟล์ เหมาะสำหรับการเก็บไฟล์ Static Assets, Media Files, Backups หรือ Log Files โดยเฉพาะสำหรับเว็บไซต์ที่ใช้ Cloud VPS หรือ Cloud Hosting ของ ผู้ให้บริการโฮสติ้ง ที่ต้องการ Offload ไฟล์ออกจาก Server

