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.
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 } })
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 } })
how to run the above code?
You need to have Node.JS and NPM installed in your computer. You can check install nodejs blog for more information about installing and using Node.JS and NPM.
You also need to have AWS Bucket Read Only User with ID and Secret Key
Thanks
Where are you storing your aws secret and access key? Are they in some json file or yaml file or in a js file?
I am storing in a js file, you can save it any file as per your convenience
i got CORS, how to resolve ?