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)"
}



Can anyone help me with this? What am I doing wrong?
Was this page helpful?