HonoRequest - Hono

Hey everyone! I've got an annoying problem that I cannot solve with uploading multiple files over a form-data request to R2 with Hono.
const { assets, collection_name }: { assets: File[] | File; collection_name: string } = await context.req.parseBody({
all: true,
});
const { assets, collection_name }: { assets: File[] | File; collection_name: string } = await context.req.parseBody({
all: true,
});
The parseBody() method is a utility from Hono https://hono.dev/api/request#parsebody to unpack the assets Files to a single entry, then I try to loop over them and upload each one to R2 like so:
assets.forEach(async (asset) => {
if (asset instanceof File) {
const { name, size, type, lastModified } = asset;
const filename: string = await getFilenameHash(name);
const { uploaded, httpEtag }: R2Object = await bucket.put(
filename,
asset,
{
httpMetadata: { contentType: type },
customMetadata: {
name,
collection_name,
size: size.toString(),
lastModified: new Date(lastModified).toString(),
},
},
);
console.log(`${uploaded} - ${httpEtag}`);
}
});
assets.forEach(async (asset) => {
if (asset instanceof File) {
const { name, size, type, lastModified } = asset;
const filename: string = await getFilenameHash(name);
const { uploaded, httpEtag }: R2Object = await bucket.put(
filename,
asset,
{
httpMetadata: { contentType: type },
customMetadata: {
name,
collection_name,
size: size.toString(),
lastModified: new Date(lastModified).toString(),
},
},
);
console.log(`${uploaded} - ${httpEtag}`);
}
});
But no files are uploaded to R2, Miniflare reports [mf:err] TypeError: Can't read from request stream because client disconnected. while on CF infra I can see that no data is passed to the logs, only empty objects {} If I try with a single file it works and the file is uploaded correctly, what am I doing wrong?
HonoRequest - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Lagon, Node.js, and others. Fast, but not only fast.
No description
1 Reply
mihaaai
mihaaai10mo ago
Later Update: In case someone encounters the same problem, it's the forEach loop that is broken, had problems with it in the past as well, just use a normal for loop and it will upload as many files you can hold into the Worker RAM. So Workers API for R2 can uplaod multiple files from one request!
Want results from more Discord servers?
Add your server