AWS SQS触发Python Lambda函数

2024-03-29 07:24:22 发布

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

lambda函数正在从SQS接收以下消息

"Records": [
    {
        "messageId": "a91c1641-fb7e-4ce5-88ad-2dd180aaada6",
        "receiptHandle": "AQEBngcKiVX9S0IjX6SfCm5VAtaDbSxznYLDMMoN2J5pq+uzVLdyzsaBVwXVjOE94dBnzAqkbI2II8RvfIfEeQCwp/8MxV5OLJXEQiBhpm2QwCXlUy+1ludjUbKRXT6pBux0eqjuVDk4I/vQ59m3D+ut2mafo2wgEZtUqT28P5DVgpeCgwFmuHcSRoc/vjcj/5rBDSJ6Mk4T+XTnnPMkZXOY0Nl9+XWzXJe3eX+DJC7vn6Vi6wEMk84nhbjn+CdL/TpHFtA6qCFE03XEJnRQeZCf69sqbpGq+ecIKI+bxb5DMDOTIin+ugb1oqpvfhySkB9nWodYOv+P6ANeLJSm4kBRvsSriDcP9uO/whyQGUMZC1v0Kx0CFVqtVmg2fIoj5R8k3f7QU9zpTKrJO5Bn/K6BwA==",
        "body": "[\n    {\n        \"year\": 2013,\n        \"title\": \"Rush\",\n        \"info\": {\n            \"directors\": [\"Ron Howard\"],\n            \"release_date\": \"2013-09-02T00:00:00Z\",\n            \"rating\": 8.3,\n            \"genres\": [\n                \"Action\",\n                \"Biography\",\n                \"Drama\",\n                \"Sport\"\n            ],\n            \"image_url\": \"http://ia.media-imdb.com/images/M/MV5BMTQyMDE0MTY0OV5BMl5BanBnXkFtZTcwMjI2OTI0OQ@@._V1_SX400_.jpg\",\n            \"plot\": \"A re-creation of the merciless 1970s rivalry between Formula One rivals James Hunt and Niki Lauda.\",\n            \"rank\": 2,\n            \"running_time_secs\": 7380,\n            \"actors\": [\n                \"Daniel Bruhl\",\n                \"Chris Hemsworth\",\n                \"Olivia Wilde\"\n            ]\n        }\n    }\n]",
        "attributes": {
            "ApproximateReceiveCount": "1",
            "SentTimestamp": "1604156768953",
            "SenderId": "983336180496",
            "ApproximateFirstReceiveTimestamp": "1604156768958"
        },
        "messageAttributes": {},
        "md5OfBody": "3d249c6c5e1b6cffc0f7af7101f3ea40",
        "eventSource": "aws:sqs",
        "eventSourceARN": "arn:aws:sqs:us-east-1:983336180496:Parse-SQS-Msg",
        "awsRegion": "us-east-1"
    }
]

现在在正文部分,我想提取一些细节,如“年份”、“标题”和“评级”。如何在不同的变量中提取这些细节,以便在这些变量上编写相应的逻辑?我想提取的详细信息包括年份、标题、类型和评级:

"body": "[
    {
        "year": 2013,
        "title": "Rush",
        "info": {
            "directors": ["Ron Howard"],
            "release_date": "2013-09-02T00:00:00Z",
            "rating": 8.3,
            "genres": [
                "Action",
                "Biography",
                "Drama",
                "Sport"
            ],
            "image_url": "http://ia.media-imdb.com/images/M/MV5BMTQyMDE0MTY0OV5BMl5BanBnXkFtZTcwMjI2OTI0OQ@@._V1_SX400_.jpg",
            "plot": "A re-creation of the merciless 1970s rivalry between Formula One rivals James Hunt and Niki Lauda.",
            "rank": 2,
            "running_time_secs": 7380,
            "actors": [
                "Daniel Bruhl",
                "Chris Hemsworth",
                "Olivia Wilde"
            ]
        }
    }
]

代码片段如下所示:

    from __future__ import print_function
    from pprint import pprint
    from botocore.vendored import requests
    import boto3
    import json
    
    dynamodb = boto3.resource('dynamodb')
    
    def lambda_handler(event, context):
        print("Received event: " + json.dumps(event, indent=2))
        for record in event['Records']:
           print ("test")
           year_e = record['body'][0]
           print(year_e)
           payload=record["body"]
           print(str(payload))

我尝试了网络上的几种排列和组合,但没有任何效果。我是Python新手,非常感谢您的快速帮助


Tags: lambdafromimportinfoeventtitlebodyrecord