miniflare icon indicating copy to clipboard operation
miniflare copied to clipboard

Add getMiniflareR2Bucket helper method - R2 Helper method

Open spigaz opened this issue 3 years ago • 2 comments

By adding the helper method getMiniflareR2Bucket one can get a bucket just by:

const bucket = await getMiniflareR2Bucket('BUCKET');

without having to create a new Miniflare instance

global.getMiniflareR2Bucket = async (bucket) => {
  const plugin = (await mf.getPlugins()).R2Plugin;
  const storage = mf.getPluginStorage("R2Plugin");
  return plugin.getBucket(storage, bucket);
}

spigaz avatar Jul 25 '22 16:07 spigaz

My fault. When working on R2, I added 0 support for jest in the initial PR 😵‍💫 . Thanks for pointing this out!

CraigglesO avatar Jul 26 '22 16:07 CraigglesO

You are kidding right? You did an awesome work!

Do you have any idea of how much you helped me out?

Besides, this is a minor thing!

spigaz avatar Jul 26 '22 16:07 spigaz

Hey! 👋 Apologies for the delayed response. I've recently returned from a long holiday and am just starting to catch up on issues now.

I don't actually think we need this. You can add an R2 bucket binding using the standard r2_buckets/r2Buckets option, then access them globally, or using the existing getMiniflareBindings method. I think this would be better suited to user code if needed. Let me know if I'm missing something.

mrbbot avatar Aug 11 '22 12:08 mrbbot

Hi, welcome back!

I hope your holidays were awesome!

You are quite right! I'm so sorry!

I'm guessing you mean something like this instead:

env.MY_BUCKET.put(key, request.body);

I was using a TDD mindset so when I got to this part, I was thinking on testing and used the sample in the Miniflare R2 documentation as starting point.

Only latter did I went on to use the R2 Bucket directly, and to be honest, I haven't thought about this since.

Thank you all!

spigaz avatar Aug 11 '22 15:08 spigaz

No worries! Yep, that's what I was thinking of. 🙂

mrbbot avatar Aug 11 '22 16:08 mrbbot