[英]@aws-sdk/client-lambda] - Invoke Lambda - Payload Response in Unit8Array - Convert to String
I am using the @aws-sdk/client-lambda
npm package for invoking lambdas.我正在使用@aws-sdk/client-lambda
npm package 来调用 lambda。 I have two Lambdas.我有两个 Lambda。 Lambda A & Lambda B. Lambda A is trying to invoke Lambda B. Lambda A & Lambda B. Lambda A 正在尝试调用 Lambda B。
Lambda A invokes Lambda B by running the following code: Lambda A 通过运行以下代码调用 Lambda B:
const { LambdaClient, InvokeCommand } = require('@aws-sdk/client-lambda');
module.exports = {
getGitHubToken: async () => {
const client = new LambdaClient({ region: process.env.REGION });
const params = {
FunctionName: process.env.GITHUB_TOKEN_FUNCTION,
LogType: 'Tail',
Payload: '',
};
const command = new InvokeCommand(params);
try {
const { Payload } = await client.send(command);
console.log(Payload);
return Payload;
} catch (error) {
console.error(error.message);
throw error;
}
},
};
The expected response from Lambda B should look like this: Lambda B 的预期响应应如下所示:
{
statusCode: 200,
body: JSON.stringify({
token: '123',
}),
};
However, Payload looks to be returning this from the line console.log(Payload);
但是,Payload 看起来是从console.log(Payload);
行返回的。 : :
I looked on the AWS SDK Website and it looks like Payload
returns a Uint8Array
.我查看了AWS SDK 网站,看起来Payload
返回一个Uint8Array
。 I guess this is because it's from a promise?我想这是因为它来自 promise?
I have tried doing Payload.toString()
however that comes back as simply a string of the values in the Unit8Array
.我尝试过Payload.toString()
,但是它只是作为Unit8Array
中的一串值返回。 Example being:示例是:
2021-04-13T14:32:04.874Z worker:success Payload: 123,34,115,116,97,116,117,115,67,111,100,101,34,58,50,48,48,44,34,98,111,100,121,34,58,34,123,92,34,116,111,107,101,110,92,34,58,92,34,103,104,115,95,111,114,101,51,65,109,99,122,86,85,74,122,66,52,90,68,104,57,122,122,85,118,119,52,51,50,111,67,71,48,50,75,121,79,69,72,92,34,125,34,125
My Question:我的问题:
How do I resolve data from Unit8Array
to the data I was expecting from the Lambda response?如何将Unit8Array
中的数据解析为 Lambda 响应中的数据? Which is a JSON Object? JSON Object 是哪个?
I have confirmed the requested Lambda (Lambda B in this case) is returning the data correctly by going to CloudWatch.我已通过转到 CloudWatch 确认请求的 Lambda(在本例中为 Lambda B)正在正确返回数据。 Thanks.谢谢。
Okay, I found a way to get this working.好的,我找到了一种方法来让它工作。
You have to specify a text encoder:您必须指定一个文本编码器:
const asciiDecoder = new TextDecoder('ascii');
Then decode it so it looks like this:然后对其进行解码,使其看起来像这样:
const data = asciiDecoder.decode(Payload);
I have logged an issue on their repository asking why this isn't included in the module.我在他们的存储库上记录了一个问题,询问为什么模块中不包含它。 I will post an update on any movement on this.我将发布有关此方面的任何动态的更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.