Error when trying to read AWS SNS message

2019-06-01 19:36发布

I need to return the message sent by Rekognition to SNS but I get this error in CloudWatch:

'Records': KeyError Traceback (most recent call last): File "/var/task/AnalyzeVideo/lambda_function.py", line 34, in lambda_handler message = event["Records"][0]["Sns"]["Message"] KeyError: 'Records'

Code:

def detect_labels(bucket, key):
    response = rekognition.start_label_detection(
        Video = {
            "S3Object": {
                "Bucket": BUCKET,
                "Name": KEY
            }
        },
        NotificationChannel = {
            "SNSTopicArn": TOPIC_ARN,
            "RoleArn": ROLE_ARN
        }
    )

    return response

def lambda_handler(event, context):
    reko_response = detect_labels(BUCKET, KEY)
    message = event["Records"][0]["Sns"]["Message"]
    return message

And is this the correct way of implementing Rekognition stored video in AWS Lambda with python I didn't find any examples on it.

Update:

The steps my app needs to take are:

  1. In the frontend, the user triggers a lambda function with API gateway which sends a file to s3
  2. When the file arrives trigger the same lambda function to apply video recognition and send jobId to SNS
  3. when SNS receives a message trigger the same lambda function to get the label data and return the data back to the user with API gateway

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-01 20:05

Your function is calling rekognition.start_label_detection() (and presumably you have created the rekognition client in code not shown).

This API call starts label detection on a video. When it is finished, it will publish a message to the given SNS topic. You can connect a Lambda function to SNS to retrieve the details of the label detection when it is finished.

However, your code is getting the order of operations mixed up. Instead, you should be doing the following:

  • Something (probably not a Lambda function) should call start_label_detection() to begin the process of scanning the video. This can take several minutes.
  • A Lambda function should be configured to trigger when the SNS topic receives a message.
  • The Lambda function is then passed a copy of the message, which can be used to call get_label_detection() to retrieve the details of the scan.

So, your first step is to separate the initial start_label_detection() request from the code that retrieves the results. Then, modify the Lambda function to retrieve the results via get_label_detection() and process the results.

查看更多
登录 后发表回答