TypeError: Key must be a buffer when sending an email with aws-sdk in node.js

TypeError: Key must be a buffer when sending an email with aws-sdk in node.js

我正在尝试使用 nodemailer 设置一个与所使用的传输类型无关的电子邮件系统。

这是我尝试发送电子邮件的方式:

const config = require('config')
const mailerConfig = config.get('mailer')
const transporter = nodemailer.createTransport(mailerConfig.transport)
transporter.sendMail({
  from: mailerConfig.from,
  to: toEmail,
  subject,
  text: textBody,
  html: htmlBody,
})

这是我的 config 文件的样子:

const AWS = require('aws-sdk')
module.exports = {
  mailer: {
    from: 'test_sender@domain.com',
    transport: {
      SES: new AWS.SES({
        accessKeyId: 'secret-key',
        secretAccessKey: 'access-key',
        region: 'region',
      }),
    },
  }
}

然后,每当我尝试发送电子邮件时,都会收到以下错误:

error:  TypeError: Key must be a buffer
    at new Hmac (crypto.js:117:16)
    at Object.Hmac (crypto.js:115:12)
    at Object.hmac (/Users/sebi/Work/node_modules/aws-sdk/lib/util.js:401:30)
    at Object.getSigningKey (/Users/sebi/Work/node_modules/aws-sdk/lib/signers/v4_credentials.js:59:8)
at V4.signature (/Users/sebi/Work/node_modules/aws-sdk/lib/signers/v4.js:97:36)
at V4.authorization (/Users/sebi/Work/node_modules/aws-sdk/lib/signers/v4.js:92:36)
at V4.addAuthorization (/Users/sebi/Work/node_modules/aws-sdk/lib/signers/v4.js:34:12)
at /Users/sebi/Work/node_modules/aws-sdk/lib/event_listeners.js:215:18
at finish (/Users/sebi/Work/node_modules/aws-sdk/lib/config.js:320:7)
at /Users/sebi/Work/node_modules/aws-sdk/lib/config.js:338:9
at /Users/sebi/Work/node_modules/aws-sdk/lib/credentials.js:123:23
at Credentials.refresh (/Users/sebi/Work/node_modules/aws-sdk/lib/credentials.js:194:5)
at Credentials.get (/Users/sebi/Work/node_modules/aws-sdk/lib/credentials.js:121:12)
at getAsyncCredentials (/Users/sebi/Work/node_modules/aws-sdk/lib/config.js:332:24)
at Config.getCredentials (/Users/sebi/Work/node_modules/aws-sdk/lib/config.js:352:9)
at Request.SIGN (/Users/sebi/Work/node_modules/aws-sdk/lib/event_listeners.js:192:22)

请注意,当我在发送电子邮件的同一位置实例化 AWS.SES() 时,代码可以正常工作。 config 破解密码有什么特别之处吗?

Is there anything special that config does to break the code?

没错。 config 打包 deep-merges 所有配置。因此它遍历您的配置,并破坏从 AWS.SES() 返回的对象。您可以尝试将您的密钥放入配置中,并仅在使用时将它们应用于 ses

const AWS = require('aws-sdk')
module.exports = {
  mailer: {
    from: 'test_sender@domain.com',
    transport: {
      SES: {
        accessKeyId: 'secret-key',
        secretAccessKey: 'access-key',
        region: 'region',
      },
    },
  }
}

并使用它:

const config = require('config')
const mailerConfig = config.get('mailer')
const transporter = nodemailer.createTransport({
    SES: new AWS.SES(mailerConfig.transport.SES)
})
transporter.sendMail({
  from: mailerConfig.from,
  to: toEmail,
  subject,
  text: textBody,
  html: htmlBody,
})

作为替代方案,您可以直接要求您的配置文件:

const config = require('./config') // depends on your files structure

我是 node-config 的维护者之一。当您只想返回纯数据时,node-config 向您的配置对象添加了一些方法,这听起来可能是个问题。

Since v1.27.0, config.util.toObject(someValue) was added to allow you to get back a plain old JavaScript object for a given config value. It's documented on our utils wiki page

如果您有旧版本的 node-config 并且不想升级,您也可以在 node-config:

返回的配置结构上执行此操作
var plainOldValue = JSON.parse(JSON.stringify(configValue)

这具有删除 node-config 添加的方法的效果,并且正是新的 .toObject() 方法在内部所做的。

在我的例子中,accessKey 和 secretAccessKey 是未定义的。