How can I put my email into R2

Hello, I want put my email into R2. Now I convert message.raw stream to ArrayBuffer, then save ArrayBuffer to R2.

async function streamToArrayBuffer(stream, streamSize) {
  let result = new Uint8Array(streamSize);
  let bytesRead = 0;
  const reader = stream.getReader();
  while (true) {
    const { done, value } = await;
    if (done) {
    result.set(value, bytesRead);
    bytesRead += value.length;
  return result;
export default {
  async email(message, env, ctx) {
    const rawEmail = await streamToArrayBuffer(message.raw, message.rawSize);
    await env.r2.put(`${}/${message.from}-${new Date().getTime()}.eml`, rawEmail,{customMetadata:{
      time:new Date().getTime(),

It cost more CPU time instreamToArrayBuffer function, I think I can do it like await env.MY_BUCKET.put(key, request.body);.
But when I put message.raw into R2 directly, I got TypeError: Provided readable stream must have a known length (request/response body or readable half of FixedLengthStream) . How can I fix it?thanks