Uploading an image through Amazon API gateway and lambda

10,019

Solution 1

Uploading a file directly to S3 doesn't necessarily require IAM permissions. You would create an API endpoint that returns a pre-signed S3 URL, which could then be used to upload the file directly to S3. The Lambda function behind the API endpoint would be the only thing that needed the correct IAM permissions for the S3 bucket.

Solution 2

Since API Gateway and Lambda don't support natively currently, you can pass the file to a picture in based64 encoded to API Gateway then pass to Lambda function. Your Lambda function can based64 decoded, then resized, registers it to the database and returning the url path of the new image.

Share:
10,019
Yonatan
Author by

Yonatan

Updated on July 20, 2022

Comments

  • Yonatan
    Yonatan over 1 year

    I have a REST API with API gateway and Lambda. I wan't to create an endpoint for uploading a profile picture, that passes the file to a Lambda function, where it is been resized, registers it to the database and returning the url path of the new image.

    Is there any way to do so with those services? Couldn't find anything online (the only suggestion I found is uploading directly to S3, which requires IAM permissions, and having an event triggering a Lambda function that resizing the picture).

    Thanks

    UPDATE

    AWS updated APIGATEWAY and know you can send binaries through an endpoint
    Thanks to @blue and @Manzo for commenting it

  • Yonatan
    Yonatan over 7 years
    Thanks. I tried what you said. I'm getting Could not parse request body into json: Unrecognized token. my request temaplate has a that prop: payload: $input.body. It should return the body (file) as a string, but throws an error instead.
  • Yonatan
    Yonatan over 7 years
    You are right, but I have my own lambda function that authorizes the user, and according to his id saves the file to the s3 bucket. I don't think letting the user getting a s3 url and then upload the file is a good idea, the point of an API is to abstract the logic and infrastructure, the user shouldn't handle all those things or even know we use s3 or aws
  • Yonatan
    Yonatan over 7 years
    Fixed it! add $util.base64Encode(). The prop looks like this: "payload": "$util.base64Encode($input.body)".
  • Yonatan
    Yonatan over 7 years
    Now I get a corrupted file. It's on application/octet-stream, which I can't manipulate or determine the image type. Any solution for that?
  • advncd
    advncd about 7 years
    @JoJoB. Have you been able to resolve the issue. I ran into the same issue.
  • Yonatan
    Yonatan about 7 years
    @advncd I used the suggestion below. I have an endpoint that gives me a signed url to the s3 bucket, and then I'm uploading it directly. You can play with the permissions, file type and file size of this sign url. currently API gateway encodes every input to JSON, so the img data will be corrupted (or at least I don't know how to decode it). I think they are planing on adding more functionalities to API gateway, and one of them is file uploading
  • advncd
    advncd about 7 years
    Yeah. hopefully API gateway supports binary data soon.