Skip to content
Miniflare
Visit Miniflare on GitHub
Set theme to dark (โ‡ง+D)

๐Ÿชฃ R2

Buckets

Specify R2 Buckets to add to your environment as follows:

wrangler.toml
[[r2_buckets]]
binding = "BUCKET1"
bucket_name = "<ignored>"
[[r2_buckets]]
binding = "BUCKET2"
bucket_name = "<ignored>"
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