Lộ trình đang học Current learning path

Developer Platform Developer Platform

Build và deploy apps trên Cloudflare. Build and deploy apps on Cloudflare.

Về trang lộ trình Track home

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)

Ví dụ chính thức từ Cloudflare Resources — gợi ý theo chủ đề bài học trong lộ trình này. Official examples from Cloudflare Resources — matched to this lesson within this path.

Xem thêm ví dụ trong lộ trình → More examples in this path →

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

Học xong hoặc muốn đổi hướng? Finished or want a different path?

Ba lộ trình độc lập — mỗi lộ trình chỉ gồm bài học và tài liệu trong phạm vi đó. Chọn lộ trình khác khi sẵn sàng, không cần học song song. Three independent paths — each includes only lessons and materials for that scope. Switch when you are ready; no need to study paths in parallel.

Chưa chắc — làm bài chọn lộ trình Not sure — use the path selector · So sánh cả ba lộ trình Compare all three paths