Integrating API Gateway with S3 for Secure File Uploads
Introduction
Hello, I'm Hemanth from the Alliance Department. In this blog, I will demonstrate how to use AWS API Gateway to upload files to an S3 bucket. This tutorial will guide you through the setup process, from creating the necessary AWS resources to configuring the API Gateway, enabling you to efficiently manage file uploads to S3 using HTTP requests.
AWS
Amazon Web Services, or AWS, is a cloud service platform that provides content distribution, database storage, processing capacity, and other features to support corporate expansion. AWS has offered a broad range of services in many different categories, including Compute, Storage, Networking, Database, Management Tools, and Security.
S3
Simple and popular AWS Service for storage. Replicates data by default across multiple facilities. It charges per usage. It is deeply integrated with AWS Services. Buckets are logical storage units. Objects are data added to the bucket. S3 has a storage class on object level which can save money by moving less frequently accessed objects to a colder storage class.
API Gateway
AWS API Gateway is a fully managed service that enables developers to create, publish, maintain, monitor, and secure APIs at any scale. It acts as an entry point for applications to access back-end services, such as S3, securely and efficiently.
Demo
In the AWS Management Console, search for "S3" and select it. Click on "Create bucket".
Provide a unique bucket name, keep other settings as default, and click "Create bucket". The bucket is successfully created.
Go to the IAM service, select "Roles," and click on "Create role".
choose "API Gateway" as the service and click "Next".
Click "Next" to proceed.
Provide a role name and click "Create role".
The role is successfully created.
Go to the role you've just created, navigate to the "Permissions" tab, and click on "Create inline policy".
Choose the JSON editor, enter the following code, replacing the Resource section with your S3 bucket ARN, and click "Next":
Provide a policy name and click "Create policy".
The policy is successfully created and attached to the role.
In the AWS Management Console, search for "API Gateway" and click on it. Click on "Create API"
select "REST API" to build.
Provide an API name and click "Create API".
Click on "Create Resource".
In the resource {bucket} and click "Create Resource".
Name the resource {filename} and click "Create Resource".
In the {filename} resource, create a method.
Selecting type "PUT", set the Integration type to "AWS Service", select your preferred region, choose "S3" as the service, and select "PUT" as the HTTP method.
Enter the path override for your S3 bucket, provide the ARN of the role created earlier in the execution role field, and click "Create Method".
Go to the PUT method, click on "Integration Request" and then "Edit".
Add the following mappings:
Name: bucket, Mapped from: method.request.path.bucket
Name: filename, Mapped from: method.request.path.filename
and click "Save".
Click on "Deploy API".
Go to API settings, and in binary media types, manage the settings to allow any file types to be uploaded.
Right now changing the settings such way that you can upload any objects.
Go to the "Stages" section, select the PUT method, and copy the "Invoke URL".
Open Postman, select "PUT" as the method, paste the URL, and replace {bucket} with your S3 bucket name and {filename} with the file you want to upload. In Postman, select "Body", choose "Binary", and upload the file.
Check your S3 bucket to confirm that the file was successfully uploaded.
Conclusion
Uploading files to S3 with Amazon API Gateway provides a quick, safe, and adaptable solution to upload data to a variety of applications. You can easily set up your S3 bucket and API Gateway to handle file uploads by following this step-by-step tutorial.