Write to R2 with ReadableStream

Im trying to write a ReadableStream to R2, however, I cannot pass a stream to the r2 .put without giving it a fixed size first, so I am piping an email objects stream to a FixedLengthStream as so:
let { readable, writeable } = new FixedLengthStream(message.rawSize);
message.raw.pipeTo(writeable);
let { readable, writeable } = new FixedLengthStream(message.rawSize);
message.raw.pipeTo(writeable);
and then saving it to R2 as follows:
await env.BUCKET_NAME.put(identifier, readable);
await env.BUCKET_NAME.put(identifier, readable);
Im seeing the entry identifier in my R2 bucket, however the content is empty. message.rawSize is showing the number of bytes I would expect. Am I using the stream pipes wrong? Thank you
3 Replies
advanced
advanced10mo ago
it's possible to use a readable stream in the put request. your code looks correct but I would check if message is correctly implementing the streaming interface. I assume message.raw is a readbleStream so try to use that in a basic handler like return new Request(readable,{headers}) and see if you are getting something back
thornAvery
thornAveryOP9mo ago
message is an EmailObject that is getting passed to the email worker. Logging the raw size gives me the number of bytes I expect. I can read the stream into a string using a loop and I get what I ekpect, but passing the stream directly as above is giving me zero bytes in R2
advanced
advanced9mo ago
I have yet to use the email handler so I'm not sure what an EmailObject is supposed to be. Have you tried creating a dummy stream and passing that to r2? I can attest that it's possible to use a stream as I have been doing it to transfer data from ftp using the tcp socket api. If you are willing to share your code maybe I can have a closer look
Want results from more Discord servers?
Add your server