Skip to content

🪣 R2

Buckets

Specify R2 Buckets to add to your environment as follows:

const mf = new Miniflare({
r2Buckets: ["BUCKET1", "BUCKET2"],
});

Manipulating Outside Workers

For testing, it can be useful to put/get data from R2 storage outside a worker. You can do this with the getR2Bucket method:

import { Miniflare } from "miniflare";
const mf = new Miniflare({
modules: true,
script: `
export default {
async fetch(request, env, ctx) {
const object = await env.BUCKET.get("count");
const value = parseInt(await object.text()) + 1;
await env.BUCKET.put("count", value.toString());
return new Response(value.toString());
}
}
`,
r2Buckets: ["BUCKET"],
});
const bucket = await mf.getR2Bucket("BUCKET");
await bucket.put("count", "1");
const res = await mf.dispatchFetch("http://localhost:8787/");
console.log(await res.text()); // 2
console.log(await (await bucket.get("count")).text()); // 2