This is my attempt to implement an endpoint that receives a file and uploads it to an R2 bucket binding. But I'm not sure if getRequestWebStream(event) is the only and propery way to get a ReadableStream of the request body.
export default defineEventHandler(async (event) => {
const bucket = event.context.cloudflare!.env.MY_BUCKET
const { key } = getQuery(event)
await bucket.put(key, getRequestWebStream(event)!)
})
However, the above implementation causes the following error to be thrown:
Provided readable stream must have a known length (request/response body or readable half of FixedLengthStream)
another attempt was to use the request.body directly from the cloudflare context object.
await bucket.put(key, event.context.cloudflare!.request.body)
but while this doesn't throw any errors anymore, it just saves an empty file (0B).
Any tips on what causes this and how to resolve it?