繁体   English   中英

在 Nodejs 中签署 opensearch 请求时 signer.addAuthorization(credentials, new Date()) 出错 Lambda

[英]Error in signer.addAuthorization(credentials, new Date()) while signing opensearch request in Nodejs Lambda

我正在使用 AWS Lambda 将文档上传到 AWS Openseach。

这是我的代码,我首先使用EnvironmentCredentialsAWS获取凭证,然后将其传递给AWS.Signers.V4().addAuthorization()方法

const AWS = require('aws-sdk');
var path = require('path');

const esDomain = {
  endpoint: 'https://search-ddbsearch-xxxxxxx.us-east-2.es.amazonaws.com',
  region: process.env.LAMBDA_REAGION,
  index: 'type',
  doctype: '_doc'
};

async function saveInOs(doc) {
  const endpoint =  new AWS.Endpoint(esDomain.endpoint);
  let req = new AWS.HttpRequest(endpoint);

  req.method = 'POST';
  req.path = path.join('/', esDomain.index, esDomain.doctype);
  req.region = esDomain.region;
  req.body = doc;
  req.headers['presigned-expires'] = false;
  req.headers['Content-Type'] = 'application/json';
  req.headers['Host'] = endpoint.host;

  const credentials = new AWS.EnvironmentCredentials('AWS');

  let signer = new AWS.Signers.V4(req, 'es');
  signer.addAuthorization(credentials, new Date());

  let send = new AWS.NodeHttpClient();
  send.handleRequest(req, null, function(httpResp) {
      let body = '';
      httpResp.on('data', function (chunk) {
        body += chunk;
      });
      httpResp.on('end', function (chunk) {
        return {
          message: "Document saved"
        }
      });
  }, function(err) {
      return {
        message: err.message
      };
  });
}

module.exports ={
  saveInOs
}

我收到以下错误(第 26 行)

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received undefined
      at new NodeError (node:internal/errors:372:5)
      at prepareSecretKey (node:internal/crypto/keys:580:11)
      at new Hmac (node:internal/crypto/hash:132:9)
      at Object.createHmac (node:crypto:162:10)
      at Object.hmac (/var/runtime/node_modules/aws-sdk/lib/util.js:428:30)
      at Object.getSigningKey (/var/runtime/node_modules/aws-sdk/lib/signers/v4_credentials.js:62:8)
      at V4.signature (/var/runtime/node_modules/aws-sdk/lib/signers/v4.js:98:36)
      at V4.authorization (/var/runtime/node_modules/aws-sdk/lib/signers/v4.js:93:36)
      at V4.addAuthorization (/var/runtime/node_modules/aws-sdk/lib/signers/v4.js:35:12)
      at saveInOs (/var/task/saveInOs.js:26:10) {
    code: 'ERR_INVALID_ARG_TYPE'
  }
}

我在这里遗漏了什么吗?

我参考了这个 repo: https://github.com/aws-samples/amazon-elasticsearch-lambda-samples

您应该使用[AWS.util.date.getDate()]而不是 new Date()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM