Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning images through AWS API Gateway

I'm trying to use AWS API Gateway as a proxy in front of an image service. I'm able to get the image to come through but it gets displayed as a big chunk of ASCII because Content-Type is getting set to "application/json".

Is there a way to tell the gateway NOT to change the source Content-Type at all?

I just want "image/jpeg", "image/png", etc. to come through.

like image 422
kjs3 Avatar asked Jul 24 '15 16:07

kjs3


3 Answers

I was trying to format a string to be returned w/o quotes and discovered the Integration Response functionality. I haven't tried this fix myself, but something along these lines should work:

  • Go to the Method Execution page of your Resource,
  • click on Integration Response,
  • expand Method Response Status 200,
  • expand Mapping Templates,
  • click "application/json",
  • click the pencil next to Output Passthrough,
  • change "application/json" to "image/png"

Hope it works!

like image 73
tgig Avatar answered Nov 11 '22 22:11

tgig


I apologize, in advance, for giving an answer that does not directly answer the question, and instead suggests you adopt a different approach... but based in the question and comments, and my own experience with what I believe to be a similar application, it seems like you may be using the the wrong tool for the problem, or at least a tool that is not the optimal choice within the AWS ecosystem.

If your image service was running inside Amazon Lambda, the need for API Gateway would be more apparent. Absent that, I don't see it.

Amazon CloudFront provides fetching of content from a back-end server, caching of content (at over 50 "edge" locations globally), no charge for the storage of cached content, and you can configure up to 100 distinct hostnames pointing to a single Cloudfront distribution, in addition to the default xxxxxxxx.cloudfront.net hostname. It also supports SSL. This seems like what you are trying to do, and then some.

I use it, quite successfully for exactly the scenario you describe: "a proxy in front of an image service." Exactly what my image service and your image service do may be different (mine is a resizer that can look up the source URL of missing/never before requested images, fetch, and resize) but fundamentally it seems like we're accomplishing a similar purpose.

Curiously, the pricing structure of CloudFront in some regions (such as us-east-1 and us-west-2) is such that it's not only cost-effective, but in fact using CloudFront can be almost $0.005 cheaper than not using it per gigabyte downloaded.

In my case, in addition to the back-end image service, I also have an S3 bucket with a single file in it, attached to a single path in the CloudFront distribution (as a second "custom origin"), for the sole purpose of serving up /robots.txt, to control direct access to my images by well-behaved crawlers. This allows the robots.txt file to be managed separately from the image service itself.

If this doesn't seem to address your need, feel free to comment and I will clarify or withdraw this answer.

like image 4
Michael - sqlbot Avatar answered Nov 11 '22 21:11

Michael - sqlbot


@kjsc: we finally figured out how to get this working on an alternate question with base64 encoded image data which you may find helpful in your solution:

AWS Gateway API base64Decode produces garbled binary?

To answer your question, to get the Content-Type to come through as a hard-coded value, you would first go into the method response screen and add a Content-Type header and whatever Content type you want.

api gateway method response

Then you'd go into the Integration Response screen and set the Content type to your desired value (image/png in this example). Wrap 'image/png' in single quotes.

enter image description here

like image 3
Dave Maple Avatar answered Nov 11 '22 20:11

Dave Maple