Skip to main content

Amazon S3 File Upload using Node.js

In our previous Node.js tutorial, we have explained how to upload multiple file with Node.js. In this tutorial, you will learn how to upload files to Amazon S3 server with Node.js.

Amazon Simple Storage Service (AWS S3) is a popular web service that provides highly scalable, durable and secure file storage system on web. We can use AWS S3 web services to store files of different formats and can easily retrieve from anywhere and anytime on the web.

As mostly server side solutions are used to upload files to Amazon S3 server and its very time taking. But now we can use client-end solution as it is more faster than server side to upload large files to AWS S3 server with Node.js. The AWS S3 provides AWS SDK for Node.js to upload files from client side.

The tutorial explained in easy steps to implement Amazon S3 file upload with Node.js and can also download source code of running example.

Also, read:

So let’s start developing application step by step to upload files to Amazon S3 server with upload Form using Node.js. We will use following file structure for this application:

  • app.js
  • index.html

Steps1: Create Amazon S3 Account

First we need to create Amazon S3 account and create Bucket name and Access Keys to use for uploading files to Amazon S3 server. We will need Bucket, accessKeyId and secretAccessKey.

Steps2: Configure Node.js Application

Now we will configure Node.js application to upload files to Amazon S3 server. As we will upload files from Form and file uploaded directly to S3 server when Form submitted. So we will need to install required module express, aws-sdk, multer and multer-s3 to handle Form multipart/form-data to upload files from Form. We will create project folder amazon_s3_upload and then go to this folder using command line and run below command to install all required modules.

npm install express aws-sdk multer multer-s3 

Steps3: Create File Upload Form

We will create index.html file in project folder amazon_s3_upload and design a HTML Form to upload files with submit button and with action /upload and file input name uploadFile.

<form class="form-horizontal" method="post" enctype="multipart/form-data" action="/upload">
	<div class="form-group">
		<div class="col-sm-4"> 
			<input type="file" class="form-control"  name="uploadFile"/>
		</div>
	</div>
	<div class="form-group">
		<div class="col-sm-4"> 
			<input class="btn btn-primary" type="submit" value="Upload"/>
		</div>
	</div>
</form>

Steps4: Handle Amazon S3 File Upload

Now in project folder amazon_s3_upload, we will create file app.js and handle functionality to upload file to Amazon S3 server. We will need to define S3 server Bucket name, accessKeyId and secretAccessKey from our Amazon S3 account. Then we will handle file upload using post method on upload action using file input name uploadFile.

var express = require('express'),
    aws = require('aws-sdk'),
    bodyParser = require('body-parser'),
    multer = require('multer'),
    multerS3 = require('multer-s3');

aws.config.update({
	accessKeyId: 'Your Amazon S3 Access Key',
    secretAccessKey: 'Your Amazon S3 Secret Key',    
    region: 'us-east-1'
});

var app = express(),
    s3 = new aws.S3();

app.use(bodyParser.json());

var upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'Your Amazon S3 Bucket Name',
        key: function (req, file, cb) {
            cb(null, file.originalname);
        }
    })
});
app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});

app.post('/upload', upload.array('uploadFile',1), function (req, res, next) {
    res.send("File uploaded successfully to Amazon S3 Server!");
});

app.listen(3300, function () {
    console.log('Amazon s3 file upload app listening on port 3300');
});

Steps5: Run Amazon S3 File Application

Now we will start the server with app.js using below command.

node app.js

and then on browser, we will load URL localhost:3300 and load the file upload Form. Now we just need to browse file and submit Form to upload file to Amazon server. Now we have developed Node.js application to upload files to Amazon S3 server.

You can download the source code of running example from below link.
Download

You may also like: