I am relatively new to AWS Lambda so maybe this is trivial but I haven't figured it out so far. I would like to return JSON data the same was that I can return it as a callback in response to an API Gateway request. The problem that I have is that I would like to process an image that a user uploads and return the result of that processing to the user in JSON format. I know that I can create Lambda functions that respond to S3 uploads (triggers), but I can't return data to the user that way.
Return JSON in response to S3 Upload
2.3k views Asked by lafayette At
2
There are 2 answers
0
On
I would recommend an alternate approach:
- let the browser do the upload to S3 directly using javascript
- subscribe your lambda function to S3 events in that bucket
- let your Lambda function thumbnail the image and save the resulting thumbnail back into S3 based on a naming convention, for example if your image is
someimage.jpg
you could save it assomeimage-thumb.jpg
. - after the upload completes let the javascript client poll the S3 bucket in a loop until the thumbnail exists and is displayed.
This method will result is faster upload, better user experience and should be extremely reliable.
=================================================
If you really really do want to use API gateway, this is possible as binary data is now supported. You can receive the data in your function either as binary or base64 encoded. You could reply with a response including the base64 encoded processed image and some meta data, for example:
{
"thumbnail": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==",
"metadataKey1": "some data",
"metadataKey2": "some other data"
}
The disadvantages here:
- Time to upload will be slower as we're introducing a bottleneck.
- Much more processing is required, especially if large images must be converted to/from base64.
- The browser will work harder to render the processed image.
You may be able to implement this as a single, synchronous operation. If the images are less then 10MB, then you should be able to create a Lambda function to accept the image, upload it to S3, process it, and return the data to the user. This also assume that you can upload to S3 and process the image within 29 seconds, which is the maximum time before API Gateway will timeout the request.
If that won't work for you, then you'll have switch to asynchronous processing. You can have a Lambda function which response to the S3 upload event and does the processing and writes the results to a table in DynamoDB, RDS, or Aurora. You can then expose an API Gateway method to check for completed processing and get the results. The client would then have to poll this API Gateway method.