2

I have this handler in my worker:

const data = await event.request.formData();

const key = data.get('filename');
const file = data.get('file');

if (typeof key !== 'string' || !file) {
  return res.send(
    { message: 'Post body is not valid.' },
    undefined,
    400
  );
}

await BUCKET.put(key, file);

return new Response(file);

If I comment out the await BUCKET.put(key, file); line, then I get the response of the file as expected. But with that line in the function, I get the error:

Uncaught (in promise) Error: Network connection lost.

I have confirmed that by changing the put to a get, I can retrieve files from that bucket, so there doesn't seem to be a problem with the connection itself.

kristianp
  • 5,496
  • 37
  • 56
Andy Jessop
  • 225
  • 1
  • 13

1 Answers1

0

Are you still having this problem? I'll need your account ID to figure out what's going on. If you DM me (Vitali) on Discord your account ID (& this SO link for context) I can probably help you out (or email me directly at cloudflare.com using vlovich as the account if you don't have/don't want to sign up on Discord). I'm the tech lead for R2.

EDIT 2022-09-07.

I just noticed that you're calling formData on the request. This is causing you to read the object into RAM. Workers has a 128 MiB limit so what's likely happening is that you're exceeding that limit (probably egregiously since we do give some buffer) and thus Cloudflare is terminating your Worker.

What you'll want to do is make sure you upload the file raw (not as a form) and access the raw ReadableStream. Alternatively, you can try writing a TransformStream to parse out the payload in a streaming fashion if you're confident the file payload (& any metadata you need) will come after the name. Usually it's easier to change your upload mechanism.

Vitali
  • 3,411
  • 2
  • 24
  • 25
  • I've encountered the same issue when I send the file directly in the request body and get it in the worker it says " "TypeError: Unrecognized Content-Type header value. FormData can only parse the following MIME types: multipart/form-data, application/x-www-form-urlencoded." even though I'm not using formData here – Sid Barrack Dec 23 '22 at 23:45
  • 1
    You'd have to share the code that you're using. I recommend filing a separate SO question and/or asking on Discord (Discord people will likely be able to help you pretty quickly vs SO being a bit slower for the asynchronous back and forth). – Vitali Dec 24 '22 at 20:19
  • it's alright now, it turned out to be a test issue on my side that didn't deploy the latest changes and there was still the version with formData. – Sid Barrack Dec 26 '22 at 05:08