How to upload files (text/image/video) in amazon s3 using node.js


What are we going to learn ?

S3 or Simple Storage Service is a cloud based storage service offered by Amazon. We can use amazon s3 to store files of different formats and later on we can access them easily when needed. In this tutorial we'll learn how to use Node.js to upload a file to Amazon s3 service. Don't worry if you don't have a s3 bucket , we'll walk you through the steps of creating your s3 bucket. Let's get started!

Prerequisites

  • Node.js Installed
  • Basic knowledge of express
  • AWS account [ you can create it here ]
Let's Start by creating a Bucket!

Step - 1 : After signing in , go to the storage domain and click on s3 as shown in image below :



Step - 2 : In s3, click on the create bucket button to create a new bucket as shown below :



Step - 3 : Enter an appropriate bucket name and region . You can also copy the settings from an existing bucket , if you have any .



Step - 4 : After setting all the details , AWS will ask you to review the details. please check whether the details entered are correct or not and everything seems fine , click on the create bucket button as shown below :



Step - 5 : Your bucket is created :



Installing aws-sdk

Just like any other npm package we can install aws-sdk using the following command:

		        
npm install aws-sdk
                
	            

Also save the access credentials file in :
  • ~/home/.aws directory [ for linux ]
  • C:\Users\USERNAME\.aws\credentials [for windows ]
  • ~/.aws/credentials [for mac]

Uploading a text file

In this case , we are giving the data to the s3 in the form of content in the body as shown below :

		        
var AWS = require('aws-sdk');
var s3 = new AWS.S3();

// Bucket names must be unique across all S3 users

var myBucket = 'njera';

var myKey = 'textFile';


 params = {Bucket: myBucket, Key: myKey, Body: 'Valueeee or some data' };

     s3.putObject(params, function(err, data) {

         if (err) {

             console.log(err)

         } else {

             console.log("Successfully uploaded data to myBucket/myKey");

         }

      });
                
	            

Uploading a image/video/audio file

in this case , we can provide any type of file and fs will read and upload it on the s3.

		        
var AWS = require('aws-sdk');
var fs =  require('fs');

var s3 = new AWS.S3();

// Bucket names must be unique across all S3 users

var myBucket = 'njera';

var myKey = 'jpeg';
//for text file
//fs.readFile('demo.txt', function (err, data) {
//for Video file
//fs.readFile('demo.avi', function (err, data) {
//for image file				
fs.readFile('demo.jpg', function (err, data) {
  if (err) { throw err; }

  

     params = {Bucket: myBucket, Key: myKey, Body: data };

     s3.putObject(params, function(err, data) {

         if (err) {

             console.log(err)

         } else {

             console.log("Successfully uploaded data to myBucket/myKey");

         }

      });

});
                
	            

Summary
  1. What are we going to learn
  2. Prerequisites
  3. Let's Start by creating a Bucket!
  4. Installing aws-sdk
  5. Uploading a text file
  6. Uploading a image/video/audio file