Lambda返回有效载荷botocore.response.StreamingBody对象显示,但变量为空 [英] Lambda Return Payload botocore.response.StreamingBody object prints but then empty in variable
问题描述
我正在从另一个函数调用lambda函数,并希望根据响应(非常标准的东西)采取不同的操作.但是我得到了一些意想不到的行为,这可能很明显,但这使我难以理解.我已经在最简单的示例中重新创建了示例,对您的帮助将不胜感激.
I'm invoking a lambda function from another function and want to take a different action depending on the response, pretty standard stuff. However I get some unexpected behavior, it's probably something obvious, but it is eluding me. I've recreated my example in the simplest possible example any help would be much appreciated.
lambda函数
def lambda_handler(event, context):
return 'Just a string'
调用lambda函数的代码
The code to call the lambda function
def invoke_lambda(payload):
r = lambda_client.invoke(
FunctionName='MyLambdaFunction',
InvocationType='RequestResponse',
Payload=bytes(payload)
)
p = r['Payload'].read()
print p #Prints an empty string
print(r['Payload'].read()) #Prints Just a string
invoke_lambda(payload)
推荐答案
以下代码解决了该问题.显然,我需要将streamingbody设置为一个变量,然后将其读入另一个变量.我使用了链接作为参考
The following code solves the problem. Apparently I need to set the streamingbody to a variable, then read it into another variable. I used this link for reference
def invoke_lambda(payload):
r = lambda_client.invoke(
FunctionName='MyLambdaFunction',
InvocationType='RequestResponse',
Payload=bytes(payload)
)
t = r['Payload']
j = t.read()
print j
这篇关于Lambda返回有效载荷botocore.response.StreamingBody对象显示,但变量为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!