googleads
How To Upload Images/Files To AWS S3 Using Node.js
preloder
Node.js Tips & Tricks Web Development

How To Upload Images/Files To AWS S3 Using Node.js

 

File uploading is very common in the world of web development. When a web client or browser uploads a file to a backend server, it is generally submitted through an HTTP form and encoded as multipart/form-data.

Here’s where Multer acts as a middleware for Express and Node.js that makes it easy to handle this multipart/form-data when the users upload files.  In this tutorial,  we will give you the complete steps to use the Multer library to upload file in Node.js.

 

What Is Multer?

Multer is a middleware used for express and Node.js to handle the file uploads. In our case, Multer acts as a middleware helper when uploading files which makes easy to handle the multipart/form-data when we try to upload files.

 

Setting Up A Node.js Application

We will use Node Express framework to listen to the server.  Next, install other dependencies which are required for the application.

 

 

 

Create a server.js file and initialize all the dependency node modules, then create an Express app and finally make sure that the server listens to the receiving request from the web client.

 

Using config.js & config.json, initialize our s3 credentials like below. 

 

config.json will look like below:

 

 

Upload Files To Amazon s3:

Now we need to create an endpoint in the Express application to handle uploading. Next, add the upload.js file inside routes folder and add the following code: 

 

Installing NPM Dependency Packages:

Use the below comment to install NPM dependency packages.

 

Create service.js file inside the service folder and add the following code to handle the single upload.

 

 

Steps To Upload Files Into Amazon s3

  • First, we are initializing AWS Object using AWS credentials.
  • Next, validating file format with help of fileFilter using mime types node module
  • We are going to use multer to handle file uploads and the multer used here will allow 3 parameters: one for validating file format, next one to handle file size an the third one is storage which is going to upload files to s3 using multer S3

 

To achieve the single upload, we are using the multer method as ‘upload.single’ and for multiple uploads, you can use upload.array.

 

Why We Should Use Multer s3 To Upload File

Actually, Multer buffering the multipart uploads file into the actual filesystem which is very difficult to scale. In order to overcome these difficulties, we need to stream the uploaded file. Now you know why we are preferring to use Multer S3 package. Multer S3 is used integration piece for existing code samples from the Multer’s storage engine documentation.

 

In the above snippet, we are using different parameters like acl, s3, key inside the Multer S3. For more details about this, please check here (https://www.npmjs.com/package/multer-s3)

 

Love to read more from the Node.js experts? Post us your favorite & user-centric topics here, we would love to work on the right topics you love that remains helpful for node programmers.

 

Like What You're Reading ?

 

The following two tabs change content below.

Bharanidharan Arumugam

Technical Architect | Tech Lead | Mentor | - An enthusiastic & vibrant Full stack developer has 7 plus years of experience in Web development arena. Owns legitimate knowledge in Ruby, Ruby On Rails, AngularJs, NodeJs, MEAN Stack, CMS Services. Apart all, A Modest human who strictly says "No To Harming Humans".

Leave a Reply

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

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call