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());
},
};