在AWS Lambda-Python中解析SQS消息触发器

时间:2020-04-05 14:06:03

标签: python amazon-s3 aws-lambda amazon-sqs

我收到有关S3存储桶上传的通知,以将消息放入SQS队列中。 SQS队列触发lambda函数。我正在尝试提取从触发lambda函数的SQS消息上载的文件的名称。打印到CloudWatch日志时,我的SQS事件记录如下所示:

{
"Records": [
    {
        "eventVersion": "2.1",
        "eventSource": "aws:s3",
        "awsRegion": "eu-west-2",
        "eventTime": "2020-04-05T13:55:30.970Z",
        "eventName": "ObjectCreated:Put",
        "userIdentity": {
            "principalId": "A2RFWU4TTDGK95"
        },
        "requestParameters": {
            "sourceIPAddress": "HIDDEN"
        },
        "responseElements": {
            "x-amz-request-id": "024EF2A2E94BD5CA",
            "x-amz-id-2": "P/5p5mDwfIu29SeZcNo3wjJaGAiM4yqBqp4p3gOfLVPeZhf+w5sRjnxsost3BuYub1FVf7tuMFs9KoC98+fwSI9NrT5WbjYq"
        },
        "s3": {
            "s3SchemaVersion": "1.0",
            "configurationId": "ImageUpload",
            "bucket": {
                "name": "HIDDEN",
                "ownerIdentity": {
                    "principalId": "A2RFWU4TTDGK95"
                },
                "arn": "arn:aws:s3:::HIDDEN"
            },
            "object": {
                "key": "activity1.png",
                "size": 41762,
                "eTag": "9e1645a32c2948139a90e75522deb5ab",
                "sequencer": "005E89E354A986B50D"
            }
        }
    }
]
}

使用此代码:

import boto3
rek = boto3.client('rekognition')

def test(event, context):
    for record in event['Records']:
       print ("test")
       payload=record["body"]
       fullpayload=str(payload)
       print(fullpayload)

使用['s3] ['object] ['key']访问有效负载字符串上的文件名'activity1.png'给了我这个错误:

's3': KeyError
Traceback (most recent call last):

如何从lambda函数访问文件名?

2 个答案:

答案 0 :(得分:1)

print(fullpayload)的输出是什么?我希望有效载荷为None,因为记录中没有名为body的属性。

从问题中的示例记录中,您应该这样做:

record['s3']['object']['key']

答案 1 :(得分:0)

如果这就是我认为的:S3 对象创建事件 --> SQS <-- lambda 轮询, 我也遇到了这个。我使用了 s3 put 示例测试,还使用了来自 sqs 消息的轮询进行了另一个测试。当它不是在测试中实际读取队列时,我遇到了问题。

print(event) 的输出实际上是整个事件的 json,如下所示:

    {   'Records': 
        [   
            {
            'messageId': '61155c1d-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
            'receiptHandle': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalF5156pb+aSqhRWbEY1XWIAVpingcBgOM8/uv1pIgfVXtfNRwzjtoCcInH6doGo9C38uWG7V48uEzpiAPr6Ao2IkXn5IEQKgxXzgelT5FtW3gpwhsQ3fvsFZdZNkMj2YiBHpdJ9QDgfmjFOWmqEJL+LWHUyksdAHxqVZMFrdaS1Tmno3Xni7DMBg1Ed+HpHkBmAVOWssDfM25lC1RNUivXj8i3iI/gD0yBlCttA4aioAlYNZ0txBrkm8aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaML+jK3JcKXiaslbu+JNZaB7hwevHRNsGIQ2MLuRhX+eHD4BN',
            'body': 
                '{"Records":    
                  [
                    {   
                    "eventVersion":"2.1",
                    "eventSource":"aws:s3",
                    "awsRegion":"us-east-1",
                    "eventTime":"2021-02-24T00:30:07.549Z",
                    "eventName":"ObjectCreated:Put",
                    "userIdentity":{"principalId":"AWS:AROAAAAAAAAAA:Rolehere"},
                    "requestParameters":{"sourceIPAddress":"x.x.x.x"},"responseElements":{"x-amz-request-id":"860A2aaaaaaaB19","x-amz-id-2":"J8epzX+FGaLsliSYSiJaaaaaaaaaaaaaETviVcrVCD/FsQjVLNBJgcv8v/PIh37Y9waaaaaaaaaaaaaaaaoUkoqhlr"},
                    "s3":
                        {"s3SchemaVersion":"1.0",
                        "configurationId":"New arrival",                    
                        "bucket":
                        {"name":"molly-bucketname","ownerIdentity":{"principalId":"A2aaaaaaFMND3"},"arn":"arn:aws:s3:::molly-bucketname"},
                        "object":{"key":"dietcokeofevil.mp3","size":420049,"eTag":"bf153e303affbb6e54feb0a233879d4d","versionId":"B2WJZpLLvpWA4nXP5T5QjVZY09qpnHKa","sequencer":"0060359E131BAA52C0"}
                        }
                    }
                   ]
                 }',
            'attributes': {
                'ApproximateReceiveCount': '1',
                'SentTimestamp': '1614126612305',
                'SenderId': 'AIDAJHaaaaaaaaaaJEBU',
                'ApproximateFirstReceiveTimestamp': '1614126612308'
            },
            'messageAttributes': {},
            'md5OfMessageAttributes': None,
            'md5OfBody': 'c752a7082100075786323ff7e5cdfc26',
            'eventSource': 'aws:sqs',
            'eventSourceARN': 'arn:aws:sqs:us-east-1:#########:queuename',
            'awsRegion': 'us-east-1'
            }
        ]
    }

当 s3 没有传送到 lambda 时,lambda 正在从队列中读取 - 您在 put 示例中实际看到的 json 似乎有一个包装器。如果您尝试在 lambda 中将打印的事件(上图)添加到您的测试中,则会出现 json 错误。我们需要解析初始 Records json & for body 然后 json.load the "body" - 然后从中解析我们的 s3 信息。

import json
import boto3


def lambda_handler(event, context):
    
    #Loops through every file uploaded
    for record in event['Records']:
        #pull the body out & json load it
        jsonmaybe=(record["body"])
        jsonmaybe=json.loads(jsonmaybe)
        
        #now the normal stuff works
        bucket_name = jsonmaybe["Records"][0]["s3"]["bucket"]["name"]
        print(bucket_name)
        key=jsonmaybe["Records"][0]["s3"]["object"]["key"]
        print(key)