Skip to content

R2 is object storage. A Worker reaches an R2 bucket through an r2_buckets binding. On one machine, workerd keeps the R2 objects under .wrangler/state/workerd/r2/. In a cluster, workerd keeps each R2 object in the cluster’s bucket, as PREFIX/v1/services/r2/<bucket_name>/<key>, so every instance sees the same objects. Read the Workers API reference for the API.

Example

The R2 example reads, writes, and deletes R2 objects.

index.jsjs
export default {
  async fetch(request, env) {
    const key = new URL(request.url).pathname.slice(1);
    if (!key) return new Response("Use /KEY.", { status: 400 });

    if (request.method === "PUT") {
      await env.FILES.put(key, request.body, {
        httpMetadata: {
          contentType: request.headers.get("content-type") ??
            "application/octet-stream",
        },
      });
      return new Response(null, { status: 204 });
    }

    if (request.method === "DELETE") {
      await env.FILES.delete(key);
      return new Response(null, { status: 204 });
    }

    if (request.method === "GET") {
      const object = await env.FILES.get(key);
      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 });
    }

    return new Response("Method not allowed.", { status: 405 });
  },
};
wrangler.jsoncjsonc
{
  "name": "r2",
  "main": "index.js",
  "compatibility_date": "2026-01-01",
  "no_bundle": true,
  "r2_buckets": [
    { "binding": "FILES", "bucket_name": "example-files" }
  ]
}

R2 is experimental, so it needs --experimental:

npx workerd serve --experimental ./r2/wrangler.jsonc
curl -X PUT -H 'content-type: text/plain' --data 'hello' \
  http://localhost:8787/notes/hello.txt
curl -i http://localhost:8787/notes/hello.txt
curl -X DELETE http://localhost:8787/notes/hello.txt

Differences from Cloudflare

  • An r2_buckets entry names its R2 bucket by bucket_name, else the binding name. workerd ignores preview_bucket_name, jurisdiction, and remote.
  • An R2 object is a store object with the same key and content headers. The user metadata workerd-r2 holds the rest of its record. Other tools can read and write these objects in the cluster’s bucket. The binding reads an object that another tool wrote, with its user metadata as customMetadata.
  • A key with a . or .. segment fails with InvalidObjectName.
  • A put() over 8 MiB is a multipart upload to the store. Its ETag has the multipart form. workerd checks an onlyIf condition when the upload starts, not when it completes.
  • A multipart part over 64 MiB fails.
  • An s3:// bucket limits user metadata to 2 KB. A put() with more customMetadata than fits fails with MetadataTooLarge.
  • workerd ignores ssecKey. It has no public bucket URL, no presigned URL, no lifecycle rules, and no bucket administration.

In a cluster, give the bucket a lifecycle rule that aborts incomplete multipart uploads. Otherwise, an upload that never completes, such as a put() whose body stops early, keeps its parts.

See also Cloudflare compatibility.

Type to search…

↑↓ navigate↵ selectEsc close