AWS阶跃函数结果路径不适用于AWS lambda

2024-09-28 03:17:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在输入中使用ResultPath来存储lambda函数的输出,这样我就可以在step函数的其他状态中使用它作为输入,但是step函数在添加ResultPath后几秒钟内被取消

Lambda函数:

def lambda_handler(event, context):
    # TODO implement
    import boto3

    s3 = boto3.client('s3')
    data = s3.get_object(Bucket='test1', Key='Testing-sandbox/Test_sql_script.sql')
    contents = data['Body'].read()
    print(contents)
    return contents

Lambda函数输出:

Response
"/* Step - 1  */ \r\nSELECT * FROM test1 LIMIT 10;\r\n/* Step - 2  */ \r\nSELECT * FROM test2 limit 10;\r\n"

步骤功能:

{
  "StartAt": "CallFunction",
  "States": {
    "CallFunction": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-west-2:12345678:function:readFile",
      "ResultPath": "$.query",
      "End": true
    }
  }
}

我对AWS比较陌生,无法调试该问题。有人能解释一下/告诉我正确的文档吗

非常感谢您提供的任何帮助/链接


Tags: lambda函数fromsqldatas3stepcontents
1条回答
网友
1楼 · 发布于 2024-09-28 03:17:35

我发现了问题。这是由于数据类型不匹配造成的。返回对象“contents”不是json格式,因此当step函数尝试将其输出存储在ResultPath中时失败。 以下代码适用于我:


   s3 = boto3.resource('s3')

    content_object = s3.Object('test1', 'Testing-sandbox/Test_sql_script.sql')
    file_content = content_object.get()['Body'].read().decode('utf-8')
    print(file_content)
    file_content2 = str(file_content).replace('\n', ' ').replace('\r', ' ')
    print(file_content2)
    print(type(file_content2))
    qr = json.dumps(file_content2)
    print(qr)

    return qr

相关问题 更多 >

    热门问题