Yonatan
Yonatan

Reputation: 1377

Uploading an image through Amazon API gateway and lambda

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

Upvotes: 10

Views: 11290

Answers (3)

Deepjyot Singh Kapoor
Deepjyot Singh Kapoor

Reputation: 103

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

Upvotes: 0

Ka Hou Ieong
Ka Hou Ieong

Reputation: 6515

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.

Upvotes: 1

Mark B
Mark B

Reputation: 200562

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.

Upvotes: 4

Related Questions