Building a Serverless File Upload API to AWS S3 Bucket with API Gateway

Amazon S3 (Simple Storage Service) is a cloud-based object storage service provided by Amazon Web Services (AWS). It allows users to store and retrieve files, including images, videos, documents, and other types of data, in a highly scalable and reliable manner.

Amazon S3 Use cases:

  • Backup and storage
  • Disaster Recovery
  • Archive
  • Hybrid Cloud storage
  • Application hosting
  • Media hosting
  • Data lakes & big data analytics
  • Software delivery
  • Static website

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs (Application Programming Interfaces) at any scale. With API Gateway, you can create RESTful APIs that can integrate with AWS services or any HTTP endpoint.

To build a file upload API to AWS S3 bucket, you can follow these general steps:

Step 1:

Open the AWS Management Console and navigate to the Amazon S3

s3

Create an AWS S3 bucket where the uploaded files will be stored. You can do this using the AWS Management Console or by using an AWS SDK or AWS CLI.

bucket name
bucket versioning
default encryption

Step 2:

Create an IAM role for the API Gateway with the required permissions to access the S3 bucket. The role should have access to write objects to the S3 bucket.

Open the AWS Management Console and navigate to the IAM

iam

Go to Roles and click Create new role

Trusted entity type

Select API Gateway and click Next

Use case

Review permissions and click Next

Add permissions

Enter a Role name and click Create Role

Role name

Step 3:

After the role has been created, we have to attach s3 PutObject policy to it. In your role, go to permissions and click add permissions to attack policy to it.

attach policies

After that, click Create policy to create a new policy

create policy

Select Service S3 and in actions select PutObject and add your bucket name in the arn and click Next.

s3 bucket

Give your policy a name

policy name

After that attach that policy to your newly created IAM role.

attach policy

Step 4:

Open the AWS Management Console and navigate to the API Gateway and choose REST API

api gateway
REST API

Step 5:

Create a New API and choose a friendly name for your API and click Create api

new api

Step 6:

Under Actions, click Create Resource

create resource

For Resource Name, enter folder.

For Resource Path, enter {folder}.

Then, click Create Resource

Choose Actions, and then click Create Resource.

For Resource Name, enter object.

For Resource Path, enter {object}.

Click Create Resource.

Create a PUT method for your API for uploading image or PDF

So under Actions, click Create Method

create method

From the dropdown list, choose PUT, and then click the check mark icon. Under the Integration type, choose AWS Service.

For AWS Region, choose us-east-1 or the AWS Region you see on the Bucket properties page.

For AWS Service, choose Simple Storage Service (S3).

Leave AWS Subdomain empty.

For HTTP method, choose PUT.

For Action Type, choose Use path override.

put

For Path override (optional), enter {bucket}/{key}

For Execution role, enter the Amazon Resource Name (ARN) for the IAM role that you created earlier.

For Content Handling, choose Passthrough and click Save.

put

Step 7:

Click Integration Request.

api gateway

Expand URL Path Parameters and click Add path

For Name, enter bucket and for Mapped from, enter method.request.path.folder

Repeat and set Name to key and set Mapped from to method.request.path.object

URL Path Parameters

Step 8:

Now we will set up binary media types for the API

In the navigation pane of your API page, go to Settings and in the Binary Media Types, click Add Binary Media Type

Binary Media Types

Note: choose image/jpeg to have API Gateway treat JPEG images as binary media types. If you add */*, then API Gateway treats all media types as binary media types.

binary

Step 9:

Go back to Resources &  choose Actions, and then click Deploy API

deploy api

In the Deploy API window, for Deployment stage, choose [New Stage]. For Stage name, enter dev and click Deploy

deploy api

Step 10:

In the navigation pane, choose Stages and the invoke URL for making requests to the deployed API snapshot appears. Copy the invoke URL

invoke url

Install Insomnia/Postman (both are popular API testing tools)

Choose PUT method and enter the URL and in File, choose binary and upload your file

URL: Invoke_URL/bucket_name/image_name_with_format

insomnia

Click Send and you can see 200 OK response

200 ok

To confirm your file upload, check your s3 bucket

s3 bucket

To follow official documentation: Click here

Leave a Reply

Your email address will not be published. Required fields are marked *