Manzoor Wani
Manzoor Wani
CDCloudflare Developers
Created by Manzoor Wani on 4/17/2024 in #workers-help
How to stream R2 file inside FormData in fetch request body?
I was wondering 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());
},
};
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());
},
};
9 replies