在@aws-sdk/client-lambda v3中解码InvokeCommandOutput.Payload遇错求助
问题描述
我在Express.js + TypeScript环境中调用Lambda函数的代码如下:
import type {InvokeCommandOutput} from '@aws-sdk/client-lambda'; import {InvokeCommand} from '@aws-sdk/client-lambda'; const command = new InvokeCommand({ FunctionName: this.client_name, InvocationType: 'RequestResponse', Payload: Buffer.from(JSON.stringify(event)), }); const response: InvokeCommandOutput = await this.client.send(command); const payLoad = response.Payload; if (payLoad != undefined) { return response.Payload; }
我期望从Lambda函数返回的InvokeCommandOutput.Payload中得到如下格式的响应:
{ "challenge_name": "CHALLENGE_NAME", "session": "session string" }
我使用的是AWS SDK v3的Lambda客户端。
尝试过AWS官方示例以及SDK中的toUtf8和fromUtf8函数解码Uint8Array,但出现以下错误:
{"errorType":"TypeError","errorMessage":"The \"key\" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received null","trace":["TypeError [ERR_INVALID_ARG_TYPE]: The \"key\" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received null"," 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 createHmac (node:crypto:162:10)"," at AuthService.initiateAuth (/var/task/main.js:57511:48)"," at processTicksAndRejections (node:internal/process/task_queues:96:5)"," at async Runtime.handler (/var/task/main.js:34:9)"]}
相关说明
使用的是AWS SDK v3中Lambda客户端的InvokeCommand类。
更新内容
我把这段代码:
if(payLoad != undefined){ console.info(payLoad); return payLoad; }
替换为:
if(payLoad != undefined){ console.info(payLoad); return JSON.parse(Buffer.from(payLoad).toString()); }
得到的Payload输出如下:
Uint8Array(777) [ 123, 34, 101, 114, 114, 111, 114, 84, 121, 112, 101, 34, 58, 34, 84, 121, 112, 101, 69, 114, 114, 111, 114, 34, 44, 34, 101, 114, 114, 111, 114, 77, 101, 115, 115, 97, 103, 101, 34, 58, 34, 84, 104, 101, 32, 92, 34, 107, 101, 121, 92, 34, 32, 97, 114, 103, 117, 109, 101, 110, 116, 32, 109, 117, 115, 116, 32, 98, 101, 32, 111, 102, 32, 116, 121, 112, 101, 32, 115, 116, 114, 105, 110, 103, 32, 111, 114, 32, 97, 110, 32, 105, 110, 115, 116, 97, 110, 99, 101, 32, ... 677 more items ]
但仍然出现相同错误,说明Payload解码存在问题:
'The "key" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received null'
内容的提问来源于stack exchange,提问作者James Nganga
相关产品推荐
相关产品推荐

