简体   繁体   English

“TypeError:hashConstructor 不是构造函数”,在 AWS-Lambda 上使用“@aws-sdk/signature-v4”时出错

[英]"TypeError: hashConstructor is not a constructor", error when using "@aws-sdk/signature-v4" on AWS-Lambda

I am developing a lambda in TypeScript that uses the "@aws-sdk/signature-v4" package and when I attempt to run the deployed lambda in the lambda console I receive the following error:我正在使用"@aws-sdk/signature-v4"包的 TypeScript 中开发 lambda,当我尝试在 lambda 控制台中运行已部署的 lambda 时,我收到以下错误:

  "errorType": "TypeError",
  "errorMessage": "hashConstructor is not a constructor",
  "trace": [
    "TypeError: hashConstructor is not a constructor",
    "    at getPayloadHash (/var/task/node_modules/@aws-sdk/signature-v4/dist-cjs/getPayloadHash.js:17:26)",
    "    at SignatureV4.signRequest (/var/task/node_modules/@aws-sdk/signature-v4/dist-cjs/SignatureV4.js:93:71)"
  ]
}

So far I have looked into the known issues for this package and I came across this https://github.com/aws/aws-sdk-js-v3/issues/3590 on Github.到目前为止,我已经研究了这个包的已知问题,我在 Github 上遇到了这个https://github.com/aws/aws-sdk-js-v3/issues/3590 I am also using the "@aws-crypto/sha256-js" package which is supposed to offer a workaround, but so far doesn't seem to have had any effect on this error.我也在使用"@aws-crypto/sha256-js"包,它应该提供一种解决方法,但到目前为止似乎对这个错误没有任何影响。 "esModuleInterop": true is set in my tsconfig.json since "@aws-crypto/sha256-js" is a commonJS module. "esModuleInterop": true在我的tsconfig.json中设置,因为"@aws-crypto/sha256-js"是一个 commonJS 模块。 Any help would be appreciated!任何帮助,将不胜感激!

How the packages are being imported:包的导入方式:

import * as crypto from "@aws-crypto/sha256-js";
const { Sha256 } = crypto;

UPDATE更新

I have logged out the following SignatureV4 object我已经注销了以下SignatureV4对象

const signer = new SignatureV4({
    credentials: defaultProvider(),
    region: AWS_REGION,
    service: "appsync",
    sha256: Sha256,
  });

and I am seeing that Sha256 is coming through as being undefined.我看到 Sha256 未定义。 I am trying to see why this is the case.我想看看为什么会这样。

SOLVED解决了

I had to change the way I was importing the packages, pretty dumb mistake and I hope by posting this others will avoid doing what I did.我不得不改变我导入软件包的方式,这是一个非常愚蠢的错误,我希望通过发布这个其他人可以避免做我所做的事情。

The way that worked行之有效的方式

import crypto from "@aws-crypto/sha256-js";
const { Sha256 } = crypto;

The way that didn't work is at the beginning of this post.不起作用的方式在这篇文章的开头。

Import the packages like this像这样导入包

import crypto from "@aws-crypto/sha256-js";
const { Sha256 } = crypto;

And not like this而不是这样

import * as crypto from "@aws-crypto/sha256-js";
const { Sha256 } = crypto; 

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

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