The article provides a step-by-step guide on how to upload files to AWS S3 directly from a React application using two different methods.
Abstract
The article titled "How to Upload Files to AWS S3 in React" outlines two distinct methods for uploading files from a React application to an AWS S3 bucket. It begins by emphasizing the ease of file uploads with modern serverless cloud solutions like AWS and Firebase. The guide is structured into six steps, starting with the creation of an S3 bucket and configuring its policies for public access and CORS. It then moves to setting up a React project and details two approaches for file uploads: using the native AWS SDK and using the react-s3 library, which is recommended for projects not utilizing other AWS services to avoid unnecessary bundle size. The article concludes by encouraging readers to explore further customization based on their needs and provides a link to an updated article on securing S3 bucket access.
Opinions
The author suggests that uploading files to AWS S3 is straightforward due to serverless cloud solutions.
It is implied that using the native AWS SDK is suitable for projects that integrate other AWS services, while react-s3 is preferred for simplicity and efficiency in projects without such integration.
The author emphasizes the importance of configuring bucket policies and CORS settings to ensure successful file uploads from the front-end application.
The article promotes best practices by advising readers to replace example values with their own credentials and to explore documentation for further customization.
A recommendation is made to read an additional article for learning how to securely access private S3 buckets using pre-signed URLs.
The author encourages engagement by inviting readers to connect via LinkedIn or their personal website and to check out other related articles on React programming.
How to Upload Files to AWS S3 in React
In 2 different ways
Credit: Google
Today, with some awesome serverless cloud solutions like AWS and Firebase, uploading a file has become a piece of cake.
Today we will see how we can quickly create an S3 bucket and upload files directly from our front-end React application.
Pre Requisites
An AWS Account
Basic Understanding of ReactJS
Let’s get started!
Step 1. Create S3 Bucket
Log in to your aws console. Search for Amazon S3 and click on Create bucket.
aws-console
Then give it a name and select the proper region. Then uncheck the Block all public access just for now (You have to keep it unchecked in production).
create-s3-bucket
Hit Create Bucket and you will see your new bucket on the list.
Step 2. Edit Bucket Policy
Now we will set the bucket policy. Click on the bucket name and go to the properties tab.
Scroll down a bit, and you will see a section named Bucket Policy
bucket-policy
Click on the Edit button. and add the following json.
This will allow public access to the folder's content for now.
Step 3. Edit CORS Policy
Scroll down a bit further, and you will get a section where you can edit the CORS policy. As we will be uploading files from another endpoint, we need to ensure that our bucket does not block that for cors
cors-policy
Then add the following .json file to the policy
Now we are ready to upload files to our AWS S3 bucket. Now moving on with the frontend part.
Step 4. Set Up React Project
To follow along, you will need a basic React project setup. You can do it easily by
npxcreate-react-apps3-upload
It will scaffold a basic application for you.
Step 5. Upload File Via Native SDK
If we use other aws services in our project, then we have a special npm package for that named aws-sdk. We can get it by
yarn add aws-sdk
Then create a new component named UploadImageToS3WithNativeSdk and add the following code there
Here is a simple component where there is a file input and upload the file using native aws-sdk .
Replace BUCKET_NAME , REGION_NAME, SECRET_KEY, ACCESS_KEY with your own value
Step 6. Upload File Via React S3
If you are not using other AWS services, then you don’t need the full aws-sdk which will unnecessarily increase your bundle size.
We will use another library named react-s3. First, install it
yarnadd react-s3
Then create a new component named UploadImageToS3WithReactS3 and add the following code
This component will do the same as the previous component. It will take a file as input and upload it to the S3 bucket.
And also, don’t forget to change the parameter values.
Conclusion
And there you go! Here is how you can upload any file to an s3 bucket. Explore the documentation for more customization according to your need.
UPDATE:
Read the below article to learn how to do it securely.