Function URL returns null but Lambda function is working

0

i have a function URL linked to a lambda function that is returning 'null' when referenced. The function calls AWS rekognition and the json response is formatted like so:

Test Event Name test1

Response null

Function Logs START RequestId: b62907a6-2d92-46f2-a1a8-815e4016e2f6 Version: $LATEST { "BoundingBox": { "Width": 0.2756405174732208, "Height": 0.21851715445518494, "Left": 0.3744633197784424, "Top": 0.5871027112007141 }, "AgeRange": { "Low": 38, "High": 46 }, "Smile": { "Value": false, "Confidence": 94.42446899414062 }, "Eyeglasses": { "Value": false, "Confidence": 91.79085540771484 }, "Sunglasses": { "Value": false, "Confidence": 99.99473571777344

So when the function URL is invoked from the browser it is referencing the JSON response (which is NULL). The relevant informaton is contained under "Functional Logs" How can this be rectified? I am using the DetectFaces operation (https://docs.aws.amazon.com/rekognition/latest/dg/faces-detect-images.html)

已提問 9 個月前檢視次數 891 次
1 個回答
0
已接受的答案

As you can see here, the response format should be like this:

{
   "statusCode": 201,
    "headers": {
        "Content-Type": "application/json",
        "My-Custom-Header": "Custom Value"
    },
    "body": "{ \"message\": \"Hello, world!\" }",
    "cookies": [
        "Cookie_1=Value1; Expires=21 Oct 2021 07:48 GMT",
        "Cookie_2=Value2; Max-Age=78000"
    ],
    "isBase64Encoded": false
}

So make sure to construct your response correctly, embedding the Rekognition response in the body attribute and setting the statusCode.

profile pictureAWS
專家
Uri
已回答 9 個月前
profile picture
專家
已審閱 1 個月前
  • Hi Uri, thanks for the reply. Do you mean something like this?

    import json import boto3 import requests

    client = boto3.client('rekognition', region_name='ap-southeast-2')

    def lambda_handler(event, context): # TODO implement bucket_name = "intelli1a" file_name = "msic.jpg"

    response = client.detect_faces(Image={'S3Object':{'Bucket':bucket_name,'Name':file_name}},
                                   Attributes=['ALL'])
    
    #Process response
    for faceDetail in response['FaceDetails']:
        print(json.dumps(faceDetail, indent=2))
    
    #Insert JSON response format
    expected_response = {
        "statusCode": 201,
        "headers": {
            "Content-Type": "application/json",
            "My-Custom-Header": "Custom Value"
        },
        "body": "{ \"message\": \"Hello, world!\" }",
        "cookies": [
            "Cookie_1=Value1; Expires=21 Oct 2021 07:48 GMT",
            "Cookie_2=Value2; Max-Age=78000"
        ],
        "isBase64Encoded": False
    }
    
  • got it Uri, just a small change and it worls. Thanks so much for your input, really appreciate it

    #Process response
    for faceDetail in response['FaceDetails']:
        return {  'statusCode' : 200,
        'body': json.dumps(faceDetail, indent=2)
    
  • Happy to hear that it works, however, looking at your code, I see that you return inside a loop. This means that your function will operate correctly only if there is a single face in the image.

    You should construct an array of all faces in the loop and then, after the loop, return the array as part of the body.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南