Uploading Files to AWS S3 with Multer and the Node.js AWS SDK
Node.js is a popular platform for building server-side applications. One common use case for Node.js applications is to store files on Amazon Web Services (AWS) Simple Storage Service (S3). In this article, we will look at how to use the Node.js AWS SDK and the multer middleware to upload files to an S3 bucket.
Multer is a middleware for handling multipart/form-data
, which is typically used for file uploads. It can be used with Node.js and supports different storage options, including in-memory storage, disk storage, and cloud storage. In our case, we will use it to receive a file upload from a client and store the file in an S3 bucket.
Prerequisites
Before we begin, you need to have the following installed:
- Node.js (v10 or higher)
- AWS account
- AWS IAM user with S3 access
- Multer package
- AWS SDK package
Step 1: Set up the AWS SDK
To use the AWS SDK in your Node.js application, you need to install the aws-sdk
package. You can do this by running the following command in your terminal:
npm install aws-sdk
Next, you need to set up your AWS credentials. You can do this by creating an AWS IAM user and assigning it the necessary permissions to access your S3 bucket. Once you have your credentials, you can create a new AWS.S3 object and pass your credentials to it:
const AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: 'your_access_key_id',
secretAccessKey: 'your_secret_access_key',
region: 'your_bucket_region',
});
const s3 = new AWS.S3();
Step 2: Set up Multer middleware
To use Multer in your Node.js application, you need to install the multer
package. You can do this by running the following command in your terminal:
npm install multer
Next, you can set up Multer as middleware in your application. In this example, we will use the memoryStorage
option to store the uploaded file in memory. You can also use the diskStorage
option to store the file on disk or the s3
option to store the file directly in your S3 bucket:
const multer = require('multer');
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024, // limit file size to 5MB
},
});
Step 3: Handle the file upload
Now that we have our AWS SDK and Multer set up, we can handle the file upload. In this example, we will create a new S3 object and pass the uploaded file to it using the upload
method:
app.post('/upload', upload.single('file'), (req, res) => {
const params = {
Bucket: 'your_bucket_name',
Key: req.file.originalname,
Body: req.file.buffer,
};
s3.upload(params, (err, data) => {
if (err) {
console.error(err);
return res.status(500).send('Error uploading file');
}
res.send('File uploaded successfully');
});
});
In this example, we are handling a POST
request to the /upload
endpoint. We are using Multer middleware to receive the file upload and store it in memory. We then create a new S3 object and pass the uploaded file to it using the upload
method. Finally, we send a response back to the client indicating whether.