Node.js File Upload to AWS S3 using Express and Multer
In this tutorial, we will be discussing how to upload files to an AWS S3 bucket using Node.js, Express, and Multer. AWS S3 is a cloud storage service provided by Amazon Web Services.
Prerequisites
- Node.js and NPM installed on your machine
- AWS S3 Account
- Basic knowledge of HTML, CSS, and JavaScript
- Basic understanding of Express framework for Node.js
Setting up the Project
First, let’s start by setting up a new Express project:
npm init
Then, install the necessary dependencies:
npm install express multer aws-sdk
Creating an AWS S3 Bucket and Credentials
Next, we need to create an AWS S3 bucket to store our uploaded files. Sign in to your AWS Management Console and navigate to the S3 service. Click on “Create bucket” and follow the instructions to configure your bucket.
After creating the bucket, we need to generate the AWS access key and secret access key that will be used to authenticate our Node.js application with S3. Go to the IAM service in your AWS Management Console, click on “Users”, and then click on “Add user”. Fill in the details and make sure to attach the “AmazonS3FullAccess” policy. After clicking “Create user”, you will be presented with the access key and secret access key. Store these carefully, as they will be needed in our Node.js code.
Creating the Express Application
Now, let’s create our Express application in a file named “app.js”.
<!DOCTYPE html>
<html>
<head>
<title>File Upload to AWS S3</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
In the above code, we have a basic HTML form with an input field of type “file” that allows the user to choose a file to upload. The form’s action attribute specifies the URL where the file will be POSTed when the form is submitted.
Implementing the File Upload Route
Back in our app.js file, we need to define a route for handling the file upload. We will be using the Multer middleware to handle the file upload process.
const express = require('express');
const app = express();
const multer = require('multer');
const AWS = require('aws-sdk');
const multerS3 = require('multer-s3');
const s3 = new AWS.S3({
accessKeyId: 'your_access_key',
secretAccessKey: 'your_secret_key',
region: 'your_s3_region'
});
const upload = multer({
storage: multerS3({
s3: s3,
bucket: 'your_bucket_name',
key: function (req, file, cb) {
cb(null, Date.now().toString())
}
})
});
app.post('/upload', upload.single('file'), (req, res) => {
res.send('File uploaded successfully.');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
In the above code, we first initialize the AWS S3 client object by providing the access key, secret access key, and the region where your S3 bucket is created.
We then configure the Multer middleware to use the Multer S3 storage engine, which allows us to store uploaded files directly to our AWS S3 bucket. We specify the S3 client object, the bucket name, and a function to generate a unique key for each uploaded file.
Finally, we define a POST route ‘/upload’ that uses the Multer middleware to handle the file upload process. The upload.single() function specifies that we are only expecting a single file to be uploaded with the field name ‘file’ from the form.
Testing the File Upload
Start your Node.js server by running the command:
node app.js
Now, open your web browser and navigate to http://localhost:3000/. You should see a simple form containing the file input field. Choose a file from your local system and click the “Upload” button. If everything is correctly configured, you should see a success message indicating that the file was uploaded successfully.
That’s it! You have successfully implemented file upload to AWS S3 using Express and Multer in Node.js. You can now integrate this functionality into your web applications and leverage the power of AWS S3 for file storage.
Conclusion
In this article, we have discussed how to upload files to AWS S3 using Express and Multer in Node.js. By following the steps mentioned above, you can easily implement file upload functionality in your own applications. AWS S3 provides a reliable and scalable solution for storing and managing files in the cloud.
However, remember to always follow best practices for securing your AWS access keys and configuring appropriate permissions to ensure the security of your data in the AWS cloud.
Wow! one of clearest and best tutorials I have ever watched. (I hope those weren't your real credentials 🤣). I was using knox-s3 which has some issues. Thanks so much.
Should you also use the package file-type to determine file type based on the magic number or signature and then compare that with the mimetype provided by multer and reject the request if they dont match?
Using the Javascript v3 how i could retrieve link to the image uploaded to s3? I want store the link in mongoDB
Thank you very much for this gold tutorial! It was exactly what i was looking for!
Hi there, am trying to use Multer and getting an error saying "Unexpected end of form", anyone have any idea why this would happen?
You are super awesome, you have solved my entire feature for my startup. Thankup
This tutorial is good! Explanation is clear and to the point. Thank you!!
Vs code theme?
what if file too small or empty ??
Clear explanations paced very well. You've found yourself a new subscriber. Hope to see more of you 🙂
Thank you for such a amazing video.
Thanks, I hope that you will get more subscribers and views in the future, because your tuts are pure gold!
while i am uploading a large video file to s3, that time i want to re-scale the actual video using ffmpeg and upload both the video files to s3 bucket. i don't have any idea how to implement that, please suggest.
Awesome video +++++++++ thank you 🙂
This is just perfect, thank you
really help full
good content , it helped me thanks
Thanks!
How to delete the s3 bucket
upload file ??
How to upload excel file ?