Linux Tutorials, Guides & Latest Tech Stuffs

How to read data from AWS S3 in Node.JS

AWS S3 is a popular solution for storing and retrieving data.If your application runs on Node.JS and required  to read or fetch data from AWS  S3 Bucket then you are at right place. This post will demonstrate how to use AWS SDK to fetch data from S3 Bucket.

Requirements

  • AWS Bucket Read Only User with ID and Secret Key

Install AWS SDK NPM Module

For fetching data from AWS S3 you have to install AWS SDK npm module, go to your project directory and run

npm install aws-sdk

after installing as a npm module now you would require to include in your js file

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

Configure SDK to use AWS S3 Bucket Read Only User’s AWS ID and AWS KEY

You can construct new S3 Object  to include aws_id and aws_key. I have stored AWS ID and AWS KEY for s3-read-only-user in a config file as AWS_ID and AWS_KEY. You can add your own user ID and Key using a config file as shown below or you can hard code values as a string.

var config = require(‘../../config/environment/’ + process.env.NODE_ENV);
var s3 = new aws.S3({ accessKeyId: config.s3.AWS_ID, secretAccessKey: config.s3.AWS_KEY });

Create a request param

Create a request param object and pass in AWS S3 Bucket Name and File Location path (key ) as shown below. For example if there is a bucket called example-bucket and there is a folder inside it called data then there is a file called data.json then you can construct getParams as following

//construct getParam
var getParams = {
    Bucket: 'example-bucket', //replace example bucket with your s3 bucket name
    Key: 'data/data.json' // replace file location with your s3 file location
}

Use s3.getObject to fetch data from AWS S3 using getParam

You can call s3.getObject function to fetch data from S3 as following

//Fetch or read data from aws s3
s3.getObject(getParams, function (err, data) {

    if (err) {
        console.log(err);
    } else {
        console.log(data.Body.toString()); //this will log data to console
    }

})
By using this code you will receive data from S3, you can also parse string data in JSON using JSON.parse() function.

Complete Code to fetch data from AWS S3 using aws-sdk in Node.JS

'use strict';

var aws = require('aws-sdk'); //require aws-sdk

var config = require('../../config/environment/' + process.env.NODE_ENV); // require you config file

var s3 = new aws.S3({ accessKeyId: config.s3.AWS_ID, secretAccessKey: config.s3.AWS_KEY }); //create a s3 Object with s3 User ID and Key

//construct getParam
var getParams = {
    Bucket: 'example-bucket',
    Key: 'data/data.json'
}

//Fetch or read data from aws s3
s3.getObject(getParams, function (err, data) {

    if (err) {
        console.log(err);
    } else {
        console.log(data.Body.toString()); //this will log data to console
    }

})
 Replace example values with your own and data should be logged to your console. If  you are facing any problem or please ask using comment form provide below, We will try to reply you as soon as we get your message.

Pranav K

Pranav K is a software engineer and all-round computer geek. His interests include AWS, Ubuntu and Wordpress

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.