You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Nodemailer使用ReadableStreams发送S3托管的邮件附件?

通过Nodemailer发送S3托管附件的流兼容问题

我正在用JS AWS SDK v3,想通过Nodemailer发送包含S3托管文件的邮件。Nodemailer文档里有个示例,是用本地文件生成的可读流作为附件发送:

{   // stream as an attachment
    filename: 'text4.txt',
    content: fs.createReadStream('file.txt')
},

我可以通过以下方式获取S3中文件的ReadableStream:

const s3Stream = await s3Client
    .send(/* ... */)
    .then((response) => {
        // ...
        return response.Body.transformToWebStream() // 返回Promise<ReadableStream>
    })
    .catch(/* ... */)

但直接把s3Stream传给Nodemailer附件的content字段时,会报错:

Type 'ReadableStream' is not assignable to type 'string | Readable | Buffer | undefined'

我试过用Readable类的静态方法fromWeb转换:Readable.fromWeb(s3Stream),但得到错误:

Argument of type 'ReadableStream' is not assignable to parameter of type 'ReadableStream<any>'.
  Type 'ReadableStream' is missing the following properties from type 'ReadableStream<any>': locked, cancel, getReader, pipeThrough, and 4 more.

也试过用Readable对象的wrap方法:

new Readable().wrap(s3Stream)

结果还是报错:

Argument of type 'ReadableStream' is not assignable to parameter of type 'NodeJS.ReadableStream'.
  Type 'ReadableStream' is missing the following properties from type 'ReadableStream': readable, read, setEncoding, pause, and 22 more.

内容的提问来源于stack exchange,提问作者anotherdeveloper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 17:42:10