R2Bucket
Source:
src/Cloudflare/R2/R2Bucket.ts
A Cloudflare R2 object storage bucket with S3-compatible API.
R2 provides zero-egress-fee object storage. Create a bucket as a resource, then bind it to a Worker to read and write objects at runtime.
Creating a Bucket
Section titled “Creating a Bucket”Basic R2 bucket
const bucket = yield* Cloudflare.R2Bucket("MyBucket");Bucket with location hint
const bucket = yield* Cloudflare.R2Bucket("MyBucket", { locationHint: "wnam",});Binding to a Worker
Section titled “Binding to a Worker”Reading and writing objects
const bucket = yield* Cloudflare.R2Bucket.bind(MyBucket);
// Write an objectyield* bucket.put("hello.txt", "Hello, World!");
// Read an objectconst object = yield* bucket.get("hello.txt");if (object) { const text = yield* object.text();}Streaming upload with content length
const bucket = yield* Cloudflare.R2Bucket.bind(MyBucket);
yield* bucket.put("upload.bin", request.stream, { contentLength: Number(request.headers["content-length"] ?? 0),});