Cloudflare tutor

A morning desk for Kotha · Gold Coast

· Australia/Brisbane · Lesson

R2: file storage for images, PDFs, and backups

R2 (object storage)

What it is

R2 is Cloudflare’s object storage. Instead of rows or keys with short values, you store whole files, called objects, inside a bucket: photos, PDFs, audio, zip archives, or JSON exports. Each object has a key (its name, which can look like a path such as uploads/2026/scan.pdf), the file bytes, and a little metadata such as its content type.

Like KV and D1, a bucket reaches a Worker through a binding. In wrangler.toml that is an [[r2_buckets]] block with a binding name (for example FILES) and the bucket_name. In the script you then call env.FILES.put(key, body), env.FILES.get(key), env.FILES.list(), or env.FILES.delete(key). A bucket is private by default; nothing in it is on the web until a Worker serves it or you turn on public access.

R2 speaks the same API as Amazon S3, so many backup and upload tools can talk to it with an access key. Cloudflare does not charge egress (download bandwidth) fees for R2, and the free tier covers a modest amount of storage and requests each month, which suits small personal apps.

Why it matters

The last two lessons covered KV for small keyed values and D1 for rows and SQL queries. Neither is meant for big files. When an app needs to keep an uploaded photo, a scanned PDF, or a nightly backup, R2 is the right home, and a D1 row can simply store the R2 key that points to the file.

Several of your ideas are really file stores: photo and PDF uploads with a journal, lecture videos, or exported notes. Putting those files in R2 keeps them on the same Cloudflare account as the Worker that serves them, without a separate storage service to sign in to.

Because a bucket is private unless you open it, you decide exactly who can read each file. Never upload identifiable patient documents to a learning bucket, and think twice before turning on public access for any bucket.

On your account

This tutor (cloudflare-tutor on cloudflare.kosan.xyz) stores no files at all. Its lessons are text in worker.js and its wrangler.toml has no [[r2_buckets]] block. That stays true after today.

A photo and PDF upload app such as mypics.kosan.xyz is the classic R2 shape: the Worker receives an upload, saves the file with env.FILES.put("uploads/<date>/<name>", body), records the key, date, and caption in a D1 table, and later streams the file back with env.FILES.get(key) when someone opens it. KV would hold a setting, D1 the list, and R2 the actual file.

The pattern is: one bucket per app, bound only to that app’s Worker, private by default. Nothing in this lesson creates, binds, opens, or changes a bucket on your account.

Do this yourself

These stay in the dashboard, on your side. This site does not change DNS, routes, or other Workers.

  1. In the Cloudflare dashboard, open R2 Object Storage (under Storage & Databases; the wording may differ slightly) and note which buckets already exist, read only. Then, as a throwaway only, create a bucket named tutor-r2-lesson, upload one small harmless file such as a text file containing lesson8, open the object to see its key, size, and content type, then delete that object and delete that same tutor-r2-lesson bucket. Do not turn on public access, and do not bind it to, or touch the buckets of, cloudflare-tutor, ainews, aiweekly, or any live app.