R2 là gì? Lưu file trên Cloudflare mà không “sợ” phí egress What is R2? Object storage on Cloudflare without egress sticker shock What is R2? Object storage on Cloudflare without egress sticker shock
R2 là kho object trên Cloudflare, API kiểu S3. Mọi đường truy cập đều không tính egress: Workers, S3 API, r2.dev và custom domain. Chi phí là storage, thao tác Class A/B, và phí lấy dữ liệu nếu dùng Infrequent Access. R2 is Cloudflare object storage with an S3-style API. Egress is free on every access path: Workers, the S3 API, r2.dev, and custom domains. You pay for storage, Class A/B operations, and data retrieval if you use Infrequent Access. R2 is Cloudflare object storage with an S3-style API. Egress is free on every access path: Workers, the S3 API, r2.dev, and custom domains. You pay for storage, Class A/B operations, and data retrieval if you use Infrequent Access.
Lưu nội dung do người dùng tạo Storing user generated content Storing user generated content
Lưu nội dung do người dùng tạo (UGC) trên R2 — kiến trúc nhanh, an toàn, chi phí hợp lý. Store user-generated content in R2 for fast, secure, and cost-effective architecture. Store user-generated content in R2 for fast, secure, and cost-effective architecture.
Thuật ngữ: Concepts: Concepts: R2 · Workers · UGC · Object storage
Sơ đồ chính thức ↗ Official diagram ↗ Official diagram ↗ · Lưu trữ Storage Storage
Object storage là gì — và vì sao không chỉ dùng ổ đĩa hosting? What is object storage — and why not only a hosting disk? What is object storage — and why not only a hosting disk?
Hosting truyền thống thường gắn ổ đĩa với một máy chủ: dung lượng có hạn, backup và scale khó khi traffic tăng. Object storage (như R2) lưu file dưới dạng object có key — ảnh.png, backup/2026-08.zip — trong bucket, truy cập qua API HTTP(S), scale độc lập với app server. Traditional hosting ties disk space to one server: limited capacity, harder backup and scale when traffic grows. Object storage (like R2) stores files as keyed objects — image.png, backup/2026-08.zip — in a bucket, accessed via HTTP(S) APIs, scaling separately from your app server. Traditional hosting ties disk space to one server: limited capacity, harder backup and scale when traffic grows. Object storage (like R2) stores files as keyed objects — image.png, backup/2026-08.zip — in a bucket, accessed via HTTP(S) APIs, scaling separately from your app server.
R2 tương thích API kiểu S3. Trang pricing nói không tính phí egress cho mọi storage class, kể cả khi tải trực tiếp qua Workers API, S3 API và domain r2.dev. Nếu bạn gắn dịch vụ tính phí khác vào bucket, dịch vụ đó có thể tính phí riêng. R2 speaks an S3-compatible API. The pricing page says there are no egress charges for any storage class, including downloads through the Workers API, the S3 API, and r2.dev domains. If you connect another metered service to the bucket, that service may charge you. R2 speaks an S3-compatible API. The pricing page says there are no egress charges for any storage class, including downloads through the Workers API, the S3 API, and r2.dev domains. If you connect another metered service to the bucket, that service may charge you.
Use case điển hình: ảnh upload user, video ngắn, export backup database, static asset cho site, log archive. Không phù hợp thay database quan hệ — dùng D1 hoặc DB chuyên dụng cho dữ liệu có cấu trúc và query phức tạp. Typical uses: user uploads, short video, database backup exports, site static assets, log archives. Not a replacement for relational databases — use D1 or a dedicated DB for structured data and complex queries. Typical uses: user uploads, short video, database backup exports, site static assets, log archives. Not a replacement for relational databases — use D1 or a dedicated DB for structured data and complex queries.
Lưu nội dung do người dùng tạo Storing user generated content Storing user generated content
Lưu nội dung do người dùng tạo (UGC) trên R2 — kiến trúc nhanh, an toàn, chi phí hợp lý. Store user-generated content in R2 for fast, secure, and cost-effective architecture. Store user-generated content in R2 for fast, secure, and cost-effective architecture.
Thuật ngữ: Concepts: Concepts: R2 · Workers · UGC · Object storage
Sơ đồ chính thức ↗ Official diagram ↗ Official diagram ↗ · Lưu trữ Storage Storage
Egress là gì — và R2 giúp gì cho ví tiền? What is egress — and how does R2 help your budget? What is egress — and how does R2 help your budget?
Egress là dữ liệu ra Internet khi user tải file. R2 không tính egress trên mọi storage class và mọi đường truy cập. Bạn trả storage và thao tác Class A/B. Infrequent Access còn có phí data retrieval. Egress is data leaving to the Internet when users download a file. R2 charges no egress for any storage class or access path. You pay for storage and Class A/B operations. Infrequent Access also has a data retrieval fee. Egress is data leaving to the Internet when users download a file. R2 charges no egress for any storage class or access path. You pay for storage and Class A/B operations. Infrequent Access also has a data retrieval fee.
Đừng đọc “không egress” thành “miễn phí mọi thứ”. Phần vượt free tier tính storage và Class A/B. Infrequent Access không nằm trong free tier và có phí lấy dữ liệu. Bài này không báo giá — đối chiếu trang pricing. Do not read “no egress” as “everything is free.” Usage above the free tier is billed as storage and Class A/B operations. Infrequent Access is outside the free tier and adds a data retrieval fee. This post is not a quote — check the pricing page. Do not read “no egress” as “everything is free.” Usage above the free tier is billed as storage and Class A/B operations. Infrequent Access is outside the free tier and adds a data retrieval fee. This post is not a quote — check the pricing page.
So với giữ ảnh trên cùng VPS chạy WordPress: R2 tách storage khỏi compute — VPS restart không làm mất file; scale CDN/Worker không cần copy ảnh sang máy mới. Phù hợp lộ trình Developer Platform trên hub. Vs keeping images on the same VPS running WordPress: R2 separates storage from compute — VPS restarts do not wipe files; scaling CDN/Workers does not require copying images to new machines. Fits the Developer Platform track on this hub. Vs keeping images on the same VPS running WordPress: R2 separates storage from compute — VPS restarts do not wipe files; scaling CDN/Workers does not require copying images to new machines. Fits the Developer Platform track on this hub.
Tối ưu phân phối ảnh với Image Resizing và R2 Optimizing image delivery with Cloudflare image resizing and R2 Optimizing image delivery with Cloudflare image resizing and R2
Giải pháp tối ưu phân phối ảnh, mở rộng được và hiệu năng cao — Image Resizing kết hợp R2. Learn how to get a scalable, high-performance solution to optimizing image delivery. Learn how to get a scalable, high-performance solution to optimizing image delivery.
Thuật ngữ: Concepts: Concepts: Image Resizing · R2 · Transformations · Cache
Sơ đồ chính thức ↗ Official diagram ↗ Official diagram ↗ · Phân phối nội dung Content Delivery Content Delivery
Gắn R2 với Workers — binding thay vì hard-code key Attach R2 to Workers — bindings instead of hard-coded keys Attach R2 to Workers — bindings instead of hard-coded keys
Trong wrangler.toml hoặc dashboard, bạn khai báo binding R2 (ví dụ MY_BUCKET) gắn với bucket. Worker gọi env.MY_BUCKET.put(key, file) hoặc .get(key) — credential do platform inject, không nằm trong source frontend. In wrangler.toml or the dashboard, declare an R2 binding (e.g. MY_BUCKET) tied to a bucket. The Worker calls env.MY_BUCKET.put(key, file) or .get(key) — credentials are injected by the platform, not in frontend source. In wrangler.toml or the dashboard, declare an R2 binding (e.g. MY_BUCKET) tied to a bucket. The Worker calls env.MY_BUCKET.put(key, file) or .get(key) — credentials are injected by the platform, not in frontend source.
Pattern upload an toàn: browser gửi file tới Worker (hoặc presigned URL ngắn hạn do Worker cấp) → Worker validate loại file/kích thước → put R2 → trả URL public hoặc signed. Không expose secret S3 key trong JavaScript client. Safer upload pattern: browser sends file to Worker (or a short-lived presigned URL from Worker) → Worker validates type/size → put to R2 → return public or signed URL. Never expose S3 secrets in client JavaScript. Safer upload pattern: browser sends file to Worker (or a short-lived presigned URL from Worker) → Worker validates type/size → put to R2 → return public or signed URL. Never expose S3 secrets in client JavaScript.
Kết hợp Image Resizing hoặc CDN: lưu bản gốc trên R2, phục vụ biến thể resize qua Cloudflare — giảm bandwidth và tăng tốc mobile. Bài CDN trên hub giải thích cache; R2 giải thích “file gốc nằm ở đâu”. Combine Image Resizing or CDN: store originals on R2, serve resized variants through Cloudflare — less bandwidth, faster mobile. The CDN post on this hub explains cache; R2 explains “where the original file lives.” Combine Image Resizing or CDN: store originals on R2, serve resized variants through Cloudflare — less bandwidth, faster mobile. The CDN post on this hub explains cache; R2 explains “where the original file lives.”
Pages có thể đọc static từ build output; R2 cho nội dung động user tạo. Nhiều app full-stack: Pages UI + Worker API + R2 storage — xem diagram fullstack trên hub. Pages can serve static build output; R2 holds dynamic user-generated content. Many full-stack apps: Pages UI + Worker API + R2 storage — see the fullstack diagram on this hub. Pages can serve static build output; R2 holds dynamic user-generated content. Many full-stack apps: Pages UI + Worker API + R2 storage — see the fullstack diagram on this hub.
Bắt đầu tuần đầu — và lỗi thường gặp Your first week — and common mistakes Your first week — and common mistakes
Tuần 1: tạo bucket test; upload vài file qua dashboard hoặc wrangler; viết Worker GET trả một object; thử public access vs private + signed URL. Đọc docs lifecycle nếu cần xóa log cũ tự động. Week 1: create a test bucket; upload files via dashboard or wrangler; write a GET Worker returning one object; try public access vs private plus signed URLs. Read lifecycle docs if you need automatic old-log deletion. Week 1: create a test bucket; upload files via dashboard or wrangler; write a GET Worker returning one object; try public access vs private plus signed URLs. Read lifecycle docs if you need automatic old-log deletion.
Lỗi phổ biến: bucket public toàn bộ — lộ file nhạy cảm; không giới hạn kích thước upload — bị spam; quên CORS khi frontend gọi trực tiếp; nhầm R2 thay SQL. Sao lưu: R2 không thay chiến lược backup ứng dụng — vẫn cần versioning hoặc export định kỳ. Common mistakes: fully public buckets leaking sensitive files; no upload size limits — spam abuse; missing CORS for direct frontend calls; treating R2 as SQL. Backups: R2 does not replace app backup strategy — still version or export on a schedule. Common mistakes: fully public buckets leaking sensitive files; no upload size limits — spam abuse; missing CORS for direct frontend calls; treating R2 as SQL. Backups: R2 does not replace app backup strategy — still version or export on a schedule.
Tiếp theo: bài Pages vs Workers, Developer Platform overview, use case build serverless app. Một demo upload avatar nhỏ học R2 nhanh hơn đọc mười trang pricing. Next: Pages vs Workers post, Developer Platform overview, build-serverless-app use case. A tiny avatar-upload demo teaches R2 faster than ten pages of pricing. Next: Pages vs Workers post, Developer Platform overview, build-serverless-app use case. A tiny avatar-upload demo teaches R2 faster than ten pages of pricing.
Câu hỏi thường gặp Frequently asked questions Frequently asked questions
R2 có thay hosting WordPress không? Can R2 replace WordPress hosting? Can R2 replace WordPress hosting?
Không trực tiếp. WordPress cần PHP và database trên server. R2 phù hợp lưu media, backup, hoặc static asset — kết hợp với hosting/Worker xử lý logic. Not directly. WordPress needs PHP and a database on a server. R2 fits media, backups, or static assets — combined with hosting or Workers for logic. Not directly. WordPress needs PHP and a database on a server. R2 fits media, backups, or static assets — combined with hosting or Workers for logic.
File trên R2 có tự cache CDN không? Are R2 files automatically CDN-cached? Are R2 files automatically CDN-cached?
Khi bạn phục vụ qua Worker hoặc custom domain gắn Cloudflare với cache rule phù hợp, có thể cache edge. Bucket private + signed URL thì cache phức tạp hơn — thiết kế cố ý. When served through a Worker or a Cloudflare custom domain with sensible cache rules, edge cache is possible. Private buckets plus signed URLs make caching trickier — design intentionally. When served through a Worker or a Cloudflare custom domain with sensible cache rules, edge cache is possible. Private buckets plus signed URLs make caching trickier — design intentionally.
R2 và S3 khác nhau chính ở đâu cho người mới? What is the main R2 vs S3 difference for beginners? What is the main R2 vs S3 difference for beginners?
API tương thích S3 và không tính egress trên mọi đường (Workers, S3, r2.dev, custom domain). Chi phí nằm ở storage, thao tác, và retrieval nếu dùng Infrequent Access. S3-compatible API and no egress on every path (Workers, S3, r2.dev, custom domains). Cost is storage, operations, and retrieval if you use Infrequent Access. S3-compatible API and no egress on every path (Workers, S3, r2.dev, custom domains). Cost is storage, operations, and retrieval if you use Infrequent Access.
Học tiếp trên hub (on-page backlinks) Keep learning on this hub (on-page links) Keep learning on this hub (on-page links)
- Workers (trang sản phẩm) Workers (product page) Workers (product page)
- Lộ trình Developer Platform Developer Platform track Developer Platform track
- Use case: app serverless Use case: build serverless app Use case: build serverless app
- Content Delivery trên hub Content Delivery on this hub Content Delivery on this hub
- Cloudflare 101 Cloudflare 101 Cloudflare 101
- Sản phẩm: R2 Product: R2 Product: R2
Nguồn tham khảo (blog.cloudflare.com) Sources (blog.cloudflare.com) Sources (blog.cloudflare.com)
Nội dung được viết lại để dễ hiểu hơn; luôn đọc bài gốc trên blog.cloudflare.com và docs chính thức khi cần chi tiết kỹ thuật hoặc cập nhật mới nhất. Content is rewritten for clarity; always read the original posts on blog.cloudflare.com and official docs for technical detail or the latest updates. Content is rewritten for clarity; always read the original posts on blog.cloudflare.com and official docs for technical detail or the latest updates.