Use AWS S3 bucket with API Gateway Proxy integration to return binary files

Rohan Hemnani
Geek Culture
Published in
4 min readJun 5, 2021

--

Let us see how we can use the S3 bucket with API Gateway to create a single REST API that will return all types of binary files based on the URI path passed.

We can add support for binary files in Rest API with API Gateway console and using YAML files also.

I have attached the tired and tested YAML code reference at the end which could be used as a reference if you want to develop it through SAM CLI Script.

But here, we will look into how we can achieve it through the AWS console.

Log in to the AWS Console, go to the API Gateway service, and click on create new Rest API.

After creating the API, create the resource path according to your requirement and then set Proxy resource as shown below

Create Get method on Proxy and select service as AWS Service. Select Simple Storage Device (S3) in AWS Service. HTTP Method as Get. In path override, we can add a base S3 bucket base path where our folders/files are present followed by {proxy} resource which will ensure if we will try to access files inside the folders of the base path given, it will be able to access it dynamically. We will see the example to get a better understanding.

Create the S3 bucket and you can store different files in your folders like json, jpeg, etc. I have created the base path with bucket my-test-bucket-rohan/s3test which has two folder properties and static. Inside properties, we have test.json file and inside static we have sample.jpeg file.

Go to the API’s Method Request and add ‘Proxy’ in the request path because we are going to use the proxy resource path. Also, add ‘Content-type’ in HTTP request headers which will use to match Content-type of the file requested.

Go to Integration Request and Map the request path and HTTP request headers as shown below

Go to Method Response and add ‘Content-Type’ in response headers as shown below

Go to Integration response, and Map the ‘Content-Type’ header value that will tell the browser to show files in response based on the content type of the file being accessed through the proxy.

Now, the final step would be to add Binary Media type in API as shown below which will ensure to support all the content-type based on the file being used through API. Some content types like JSON, image format, etc. will be rendered in the browser while others like doc, CSV, etc. will automatically get downloaded.

We can finally deploy the API and test it using the S3 bucket file path.

Copy the base API Path, and replace the {proxy+} with your S3 file location after the base S3 bucket path that we have given in Path override. As we earlier created the bucket and stored two different files let us try to call that files and look behavior

Example 1: static/sample.jpeg

Example 2: properties/test.json

So, as you can see, we can make a single REST API in AWS to fetch different file data through S3 which could be very helpful. We can also authenticate API using different services provided by AWS.

To do the configuration through YAML and deploy it through SAM CLI script refer the code: https://github.com/Rohan009/aws_s3_api_binary_files

--

--

Rohan Hemnani
Geek Culture

Full Stack Developer | Programmer | AWS | Enthusiast learner