Skip to the workspace
Pebellslearn by building
R2 Storage and the Developer Toolchain  ›  Lesson

R2 — Buckets of Objects

Object Storage 15 minR2 stores objects — a key, a streamed body and metadata — in buckets, with strong consistency and no egress fees, and is the place for files rather than small lookup values.

cloudflare / R2 — Buckets of Objects

Explore each scene at your own pace.

01 · The rule

The rule

R2 — Buckets of Objects

Hoplink users now want to shorten links to files: a PDF menu, a holiday photo, a slide deck. KV is the wrong home — it is tuned for small values read everywhere, not for megabytes of binary data. R2 is Cloudflare's object storage: it holds files of any size, speaks the same API as Amazon S3, and charges nothing for data going out (no egress fees), which matters when a popular file is downloaded thousands of times.

The rule

A bucket holds objects; an object is a key, a body and metadata.

  • Bucket — a named container, such as hop-files. You create it with wrangler r2 bucket create hop-files and bind it as FILES.
  • Key — the object's name, like priya/menu.pdf. There are no real folders; slashes are just characters that make keys look like paths.
  • Body — the bytes, handled as a stream.
  • Metadata — httpMetadata (content type, cache rules) and your own customMetadata.
// upload: stream the request body straight into R2
await env.FILES.put("priya/menu.pdf", request.body, {
  httpMetadata: { contentType: "application/pdf" },
  customMetadata: { owner: "priya" },
});

// download
const object = await env.FILES.get("priya/menu.pdf");
if (object === null) return new Response("Not found", { status: 404 });

const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set("etag", object.httpEtag);
return new Response(object.body, { headers });

The other bucket operations:

  • head(key) — metadata only, no body. Cheap for "does it exist and how big is it?"
  • delete(key) — accepts one key or an array of keys.
  • list({ prefix: "priya/", cursor }) — returns objects, a truncated flag and a cursor for the next page. Add delimiter: "/" to get folder-like groupings.

Unlike KV, R2 is strongly consistent: once put finishes, every reader everywhere sees the new object.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.