Phần 3: Chọn storage đúng Part 3: Pick the right storage · Bài 3/3 Lesson 3/3

R2 cho file và tài liệu R2 for files and assets

PDF, slides, upload user — object storage. Có thể phục vụ public hoặc signed URL sau này. PDFs, slides, user uploads — object storage. Serve public or via signed URLs later.

R2 for files and assets
Minh họa từ Cloudflare Reference Architecture (developers.cloudflare.com) Illustration from Cloudflare Reference Architecture (developers.cloudflare.com)

Các bước thực hiện Step-by-step

  1. Dashboard → R2 → Create bucket. Dashboard → R2 → Create bucket.
  2. Binding `R2_BUCKET` trong Worker/Pages. Bind `R2_BUCKET` in Worker/Pages.
  3. Upload object: `bucket.put(key, stream)`. Upload object: `bucket.put(key, stream)`.
  4. Serve public qua custom domain hoặc signed URL. Serve publicly via custom domain or signed URL.

Giải thích chi tiết Detailed explanation

R2 = S3-compatible, egress miễn phí — lý tưởng asset, backup, user upload. R2 is S3-compatible with zero egress fees — ideal for assets, backups, user uploads.

Lưu ý (best practices) Note (best practices)

Truy cập R2 qua binding `env.MY_BUCKET` — không dùng REST API từ Worker. Binding không cần network hop hay authentication. Access R2 via the `env.MY_BUCKET` binding — do not use the REST API from a Worker. Bindings require no network hop or authentication.

Nguồn: Source: Workers Best Practices Workers Best Practices

Ví dụ triển khai (Cloudflare Resources) Deployment examples (Cloudflare Resources)

Tutorial, solution guide và reference từ developers.cloudflare.com/resources ↗ — gợi ý theo chủ đề bài học. Tutorials, solution guides, and reference docs from developers.cloudflare.com/resources ↗ — matched to this lesson topic.

Duyệt toàn bộ catalog → Browse full catalog →

Tài liệu Cloudflare Developers Cloudflare Developer docs

Sơ đồ kiến trúc (Cloudflare Docs) Architecture diagrams (Cloudflare Docs)

Use Case 1: Secure User Uploads to R2 via Signed URLs

Storing user generated content Storing user generated content

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: R2 · Workers · UGC · Object storage

Sơ đồ chính thức ↗ Official diagram ↗ · Storage Storage

Figure 1: Cloudflare Image Resizing and R2

Optimizing image delivery with Cloudflare image resizing and R2 Optimizing image delivery with Cloudflare image resizing and 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: Image Resizing · R2 · Transformations · Cache

Sơ đồ chính thức ↗ Official diagram ↗ · Content delivery Content Delivery

Sản phẩm liên quan Related products