AWS API Gateway and S3 Integration (Encouraging The Correct Way) - by Sayed Imran - Medium
AWS API Gateway and S3 Integration (Encouraging The Correct Way) - by Sayed Imran - Medium
Search
Get unlimited access to the best of Medium for less than $1/week. Become a member
Amazon S3
Amazon Simple Storage Service (Amazon S3), often referred S3, is a highly scalable,
secure, and flexible cloud storage service provided by Amazon Web Services (AWS).
It was introduced in March 2006 and has since become one of the most widely used
object storage services in the world.
S3 is designed to store and retrieve data of any type or size, making it a fundamental
building block for many cloud-based applications and services.
API Gateway
Amazon API Gateway is a fully managed service provided by Amazon Web Services
(AWS) that allows you to create, publish, secure, and manage APIs for your
applications. It serves a central entry point for your APIs, enabling you to connect to
backend services or AWS resources while handling essential tasks such as request
routing, authentication, authorization, monitoring, and more.
Requirement
There may be a requirement, where you want to upload images to S3 using APIs and
not any SDK or AWS CLI, rather directly from APIs!
Common Mistakes
I’ve seen people using AWS Lambda as the intermediate to receive files via API
Gateway and then upload to S3, which according to me isn’t the optimal solution,
even if the files are required to be processed before saving, it takes twice the effort
to upload the file. (API gateway -> Lambda -> S3), which may lead to unwanted
corrupted files in the destination.
Possible Solution
Instead the files can be directly uploaded to the S3 via API Gateway and then,
Lambda can perform processing over it and may store the result as and where/when
required.
Bucket Name
Filename
7. Create an usage plan, so as to attach API key to avoid unauthorized access to the
resources.
Getting started
1. Creating an S3 bucket.
1.1(Optional) Unchecking Block all public access (will allow public read access)
1.2 (Optional) Creating S3 Bucket Policy for the above created bucket to allow end
users to access it.
{
"Id": "Policy1696244459937",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1696244457380",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::api-gateway-s3-tutorial/*",
"Principal": "*"
}
]
}
2. Create an IAM role with permissions policy to PutObject to the above created S3
bucket
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::api-gateway-s3-tutorial/*"
}
]
}
4.3 Creating a PUT Method to the above created resource to upload files and
attaching the API Gateway Role created earlier.
4.3 Under Method Request for the above created method, check the API key required
field.
4.4 Under Integration Request for the above created method, edit the path
parameters as the following to accept bucket name and file name from the path
parameters.
5. Modifying API Gateway settings to accept any desired type of file content that we
want to upload. (.jpg and .png in our case).
6. Deploying the API with the above made configurations in a new stage.
7. Creating usage plan and API key to add the above created stage.
NOTE: Just to be sure, re-deploy the API gateway after making all the changes.
1.1 (Optional) To provide a destination folder path in s3 for a file, you need to use
“%2f” instead of “/”. For example if your path is:
api-gateway-s3-tutorial/folder1/wew.png, where
https://t8mhsswxx6.execute-api.ap-south-1.amazonaws.com/dev/api-gateway-s3-
tutorial%2ffolder1/wew.png
Hence marks the completion of this tutorial, hope you find this small tutorial
insightful and would have solved your use case.
Follow
Multi Cloud Certified Associate | AWS | Google Cloud | Azure | Cloud and DevOps Enthusiast |
122 3
Sayed Imran
85
Sayed Imran in Knowledge Lens: A Rockwell Automation Company
116
72 1
The Most Popular File Upload to AWS S3 Using AWS Api Gateway
Introduction
22
Akhilesh Mishra
2.4K 22
Lists
Should You Use Amazon API Gateway in 2024? Consider Function URLs
Instead!
Did you know that you can now invoke Lambda functions using an HTTP API via Function
URLs?
748 3
Artturi Jalli
9.6K 126
21 1
Christopher Adamson