Api gateway as s3 proxy

Vijay Sebastian
3 min readApr 15, 2021

Introduction

In any web application file upload is an essential feature. In most cases even though the upload is only to S3, we tend to create an API for file upload so that we can provide the same authentication mechanism as other APIs. Instead of creating a separate backend API to handle the file upload, AWS API Gateway can be used as a proxy to provide restful endpoints to S3 along with the same auth mechanism as other APIs

Create S3 bucket

Step1: Create an S3 bucket to store the uploaded files.

Go to AWS console > Services > S3 > create bucket

Step 2: S3 permission

Go to Console > Services > S3 > uploaded-images-test > permissions > edit

{

“Version”: “2012–10–17”,

“Statement”: [

{

“Sid”: “Allow-Public-Access-To-Bucket”,

“Effect”: “Allow”,

“Principal”: “*”,

“Action”: “s3:GetObject”,

“Resource”: “arn:aws:s3:::uploaded-images-test/*”

}

]

}

Step 3: Cors configuration

Edit the Cross-origin resource sharing (CORS) configuration

[

{

“AllowedHeaders”: [

“*”

],

“AllowedMethods”: [

“PUT”,

“POST”,

“DELETE”

],

“AllowedOrigins”: [

“*”

],

“ExposeHeaders”: []

}

]

API Gateway

Step 1: Create API Gateway Resource

Go to Console > Services > API Gateway > Create API > REST API > Build.

Create a resource

Step 2: Create another resource in /{bucket} resource

Step3: Create a PUT method in /{item} resource

Step 4: Configure the URL Path Parameter and Mapping Templates

Step 5: Configure the HTTP Request Headers in method request

Step 6: Configure the HTTP Headers in Request integration

Step 7: Enable CORS for PUT method

Step 8: Test the API to verify if the file upload is happening correctly

Step 9: Change the setting to accept the binary files

Step 10: Run the cors enabling command in the shell

aws apigateway update-integration — rest-api-id qjijks2vcc — resource-id crskn8 — http-method OPTIONS — patch-operations “op=’replace’ ,path=’/contentHandling’,value=’CONVERT_TO_TEXT’” — region us-west-2

Finally, Deploy your API and verify in the browser

--

--