Uploading an image through Amazon API gateway and lambda
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.
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.
Yonatan 2 months
I have a REST API with
Lambda. I wan't to create an endpoint for uploading a profile picture, that passes the file to a
Lambdafunction, 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).
AWS updated APIGATEWAY and know you can send binaries through an endpoint
Thanks to @blue and @Manzo for commenting it
Yonatan over 6 yearsThanks. I tried what you said. I'm getting
Could not parse request body into json: Unrecognized token. my
request temaplatehas a that prop:
payload: $input.body. It should return the body (file) as a string, but throws an error instead.
Yonatan over 6 yearsYou 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 over 6 yearsFixed it! add
$util.base64Encode(). The prop looks like this:
Yonatan over 6 yearsNow 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 almost 6 years@JoJoB. Have you been able to resolve the issue. I ran into the same issue.
Yonatan almost 6 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 almost 6 yearsYeah. hopefully API gateway supports binary data soon.