How to stream R2 file inside FormData in fetch?

I was playing around with Workers and R2 and wanted to see if it’s possible to stream an R2 object to an external API using FormData instead of reading the whole blob into memory. The file size can be more than 2GB.

export default {
	async fetch(request: Request, env: Env) {
		const key = 'some_video.mp4';

		const object = await env.MY_R2_BUCKET.get(key);

		const form = new FormData();

		const fileStream = object!.body;
		const fileBlob = await object!.blob();

		form.set('some_text_field', 'value here');
		// I want to use the `fileStream` here instead of `fileBlob`
		form.set('video', fileBlob, 'video.mp4');

		const resp = await fetch('https://api.example.com/some/endpoint', {
			method: 'POST',
			body: form,
		});

		console.log(await resp.text());
	},
};

Any solution for this?

Did you figure out how to do it?

You could take a look at this comment on the npm form-data package: https://github.com/nodejs/undici/issues/2202#issuecomment-1662008812

It seems like the form-data package supports streaming.