Get object from S3 in AWS Lambda function and send to Api Gateway

前提是你 提交于 2019-11-28 13:46:12
lenaten

You can achieve Get Image <-> API Gateway <-> Lambda <-> S3 integration with ease.

In lambda, instead of json, return base64 string representation of image (buffer.toString('base64')), force API Gateway to convert the string to binary and add specific Content-Type (so you don't need to use their limited binary support that enforce you to send a specific Accept header).

In AWS console, go to API Gateway, then go to the relevant method and update the settings:

  • Integration Request

    • Uncheck: Use Lambda Proxy integration
  • Method response

    • Add Response -> HTTP Status: 200
    • Add Header: Content-Type
  • Integration Response -> Header Mapping -> Response header -> Content-Type

    • Mapping value: 'image/jpeg' (single quote matter)

From command line, run the command below to force convert string to binary. First, retrieve the rest-api-id and the resource-id from API Gateway. Then, run in CLI (replace rest-api-id and resource-id with your own):

aws apigateway put-integration-response --rest-api-id <rest-api-id> --resource-id <resource-id> --http-method GET --status-code 200 --content-handling CONVERT_TO_BINARY

You shouldn't use API Gateway to pass back binary content when you use it with Lambda. API Gateway with Lambda are setup to respond with XML/JSON data. Read more about why and how here.

Try changing your callback chain so it uploads the modified image back to S3. After successful upload, send back URI of the target object and redirect your client to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!