问题
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:
- In the frontend, the user triggers a lambda function with API gateway which sends a file to s3
- When the file arrives trigger the same lambda function to apply video recognition and send jobId to SNS
- 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:
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.
来源:https://stackoverflow.com/questions/53123056/error-when-trying-to-read-aws-sns-message