简体   繁体   中英

Error while parsing event.body at AWS API Gateway Lambda nodejs handler

Is there any standard way to parse the AWS API event content in AWS Lambda?

I have the lambda nodejs (handler) function like this. This function is invoked by AWS API Gateway Proxy integration. In short, the proxy integration passes the http request as is to the lambda.

exports.handler = async(event, context, callback) => {
...
}

My event would contain images or documents. In other words, for now using Postman, I upload content through HTTP Post method to the AWS API Url. Then, I would like to call the AWS S3 upload with the content.

As is, the event object if we JSON.stringify, it contains these things

{
    "resource": "/UploadToS3",
    "path": "/UploadToS3",
    "httpMethod": "POST",
    "headers": {
        "Accept": "*/*",
        "accept-encoding": "gzip, deflate",
        "Cache-Control": "no-cache",
        "Content-Type": "multipart/form-data; boundary=--------------------------286169661413777803563037",
        "Host": "rzm85vjr13.execute-api.us-east-1.amazonaws.com",
        "Postman-Token": "84182d22-7193-480f-8642-0fede39b4458",
        "User-Agent": "PostmanRuntime/7.11.0",
        "X-Amz-Date": "20190506T191534Z",
        "X-Amzn-Trace-Id": "Root=1-5cd087d7-73a9f50e5b2c20a2628852fb",
        "X-Forwarded-For": "100.18.45.29",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https"
    },
    "multiValueHeaders": {
        "Accept": [
            "*/*"
        ],
        "accept-encoding": [
            "gzip, deflate"
        ],
        "Cache-Control": [
            "no-cache"
        ],
        "Content-Type": [
            "multipart/form-data; boundary=--------------------------286169661413777803563037"
        ],
        "Host": [
            "rzm85vjr13.execute-api.us-east-1.amazonaws.com"
        ],
        "Postman-Token": [
            "84182d22-7193-480f-8642-0fede39b4458"
        ],
        "User-Agent": [
            "PostmanRuntime/7.11.0"
        ],
        "X-Amz-Date": [
            "20190506T191534Z"
        ],
        "X-Amzn-Trace-Id": [
            "Root=1-5cd087d7-73a9f50e5b2c20a2628852fb"
        ],
        "X-Forwarded-For": [
            "100.18.45.29"
        ],
        "X-Forwarded-Port": [
            "443"
        ],
        "X-Forwarded-Proto": [
            "https"
        ]
    },
    "queryStringParameters": null,
    "multiValueQueryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "resourceId": "f80gn4",
        "resourcePath": "/UploadToS3",
        "httpMethod": "POST",
        "extendedRequestId": "ZRopnE9AIAMF2ig=",
        "requestTime": "06/May/2019:19:15:35 +0000",
        "path": "/test/UploadToS3",
        "accountId": "364714536632",
        "protocol": "HTTP/1.1",
        "stage": "test",
        "domainPrefix": "rzm85vjr13",
        "requestTimeEpoch": 1557170135135,
        "requestId": "5319988d-7033-11e9-a592-f9fb3565e98f",
        "identity": {
            "cognitoIdentityPoolId": null,
            "accountId": null,
            "cognitoIdentityId": null,
            "caller": null,
            "sourceIp": "100.18.45.29",
            "principalOrgId": null,
            "accessKey": null,
            "cognitoAuthenticationType": null,
            "cognitoAuthenticationProvider": null,
            "userArn": null,
            "userAgent": "PostmanRuntime/7.11.0",
            "user": null
        },
        "domainName": "rzm85vjr13.execute-api.us-east-1.amazonaws.com",
        "apiId": "rzm85vjr13"
    },
    "body": "----------------------------286169661413777803563037\r\nContent-Disposition: form-data; name=\"test\"; filename=\"b002.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n**CONTENT GOES HERE**\r\n",
    "isBase64Encoded": false
}

If I say event.body , this is what we get. I have cut down so much of image data for brevity (all the image data is between image/jpeg and ---- ).

----------------------------286169661413777803563037
Content-Disposition: form-data; name="test"; filename="b002.jpg"
Content-Type: image/jpeg

����JFIF��C         

----------------------------286169661413777803563037--

I have to parse this in some way to get the content of the image, and possibly the type. This content looks like it is an extract of the HTTP request. If I use this content as-is and pass it to S3.upload, it is of not a good format to open later.

Some examples online I saw has some libraries. Can someone please explain why I would need the libraries to extract the content as needed by S3 upload or for some other functions.

In the case of an image upload to an S3 bucket, you need to consider pass the image as Base64 (either with upload or PutObject methods). So, there are some considerations can I share as a result of my experience:

  • First, remove the headers before image data. (You can find the /(forward-slash) or ".../jpeg" dividing this) Ex.
...\r\nContent-Type: image/jpeg\r\n\r\n����\u0000\u0010JFIF\u0000\u0001\u00...
  • When you send image via Postman is NOT the right encoding in your event.body, so you have to convert it to Base64 before upload to your bucket. If you don't do it, your image will be successfully uploaded but corrupted and won't be possible to see any content (you'll see a weird blank box). In Node.js you can perform it with Buffer.from(yourstring, 'base64') .

  • Don't forget to include "ContentType": "image/jpeg" in your parameters for s3 upload method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM