Uploading an image through Amazon API gateway and lambda
Asked Answered
P

3

10

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

Prophecy answered 7/5, 2016 at 15:0 Comment(2)
API gateway supports binary data now...Scoot
Yes, example here: aws.amazon.com/blogs/compute/…Chaldean
B
4

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.

Brother answered 7/5, 2016 at 16:5 Comment(1)
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 awsProphecy
F
1

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.

Fi answered 7/5, 2016 at 17:30 Comment(6)
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.Prophecy
Fixed it! add $util.base64Encode(). The prop looks like this: "payload": "$util.base64Encode($input.body)".Prophecy
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?Prophecy
@JoJoB. Have you been able to resolve the issue. I ran into the same issue.Treasonable
@Treasonable 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 uploadingProphecy
Yeah. hopefully API gateway supports binary data soon.Treasonable
P
0

To make a put request and pass image/file in the body of the request to the API Gateway url in Python (Flask), you could use:

file = request.files['file']

headers = {'Content-Type': file.content_type, 'x-amazon-apigateway-binary-media-types': 'image/jpeg' }

api_url = '<url_endpoint>'

file.seek(0)
file_data = file.read()

response = requests.put(api_url,file_data, headers=headers)

Postman Request

Padus answered 16/4 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.