How to upload a zipfile to R2?

I'm trying to upload a zipfile to R2. My worker receives the zip file as formdata: const formData = await req.formData(); then I get the file inside formData: const file = formData.get('file'); and put it in R2 await env.MODELS_BUCKET.put(file); also, I've tried await env.MODELS_BUCKET.put(file.stream()); but it gives the following error:
{
"name": "TypeError",
"message": "Failed to execute 'put' on 'R2Bucket': parameter 2 is not of type 'ReadableStream or ArrayBuffer or ArrayBufferView or string or Blob'.",
"stack": "TypeError: Failed to execute 'put' on 'R2Bucket': parameter 2 is not of type 'ReadableStream or ArrayBuffer or ArrayBufferView or string or Blob'.\n at main.js:79:44\n at async Object.handle [as fetch] (main.js:57:30)\n at async jsonError (main.js:98:12)"
}
{
"name": "TypeError",
"message": "Failed to execute 'put' on 'R2Bucket': parameter 2 is not of type 'ReadableStream or ArrayBuffer or ArrayBufferView or string or Blob'.",
"stack": "TypeError: Failed to execute 'put' on 'R2Bucket': parameter 2 is not of type 'ReadableStream or ArrayBuffer or ArrayBufferView or string or Blob'.\n at main.js:79:44\n at async Object.handle [as fetch] (main.js:57:30)\n at async jsonError (main.js:98:12)"
}
Can anyone help me with this? What am I doing wrong?
2 Replies
kian
kian16mo ago
You’re not providing a filename put is filename and then body - two args
Roberto
RobertoOP16mo ago
Oh I got it. Im putting the file in the first parameter, where it supposed to be the filename. Btw, I found a great example online. Want to share it here as a reference because I did exactly the same https://reffect.co.jp/en/cloudflare/cloudflare-r2-basic/#workers-configuration Tagged as solved already, @kiannh. Thanks
Want results from more Discord servers?
Add your server