Introduction to AWS S3 and its operations
Posted By : Pulkit Chadha | 16-Jun-2017
Introduction:
Amazon Simple Storage Service (Amazon S3) is one of the service to store and retrieve any amount of data from anywhere on the web.Customers can use AWS S3 as primary storage for their web applications.Storage pricing varies by region and class of storage.
Before, we proceed we need the following things:
AccessKeyId: '***********'
SecretAccessKey: '***********'
Bucket Name: 'TEST-BUCKET'
Region: '' // Region of Your S3 Bucket
Step 1: First, install AWS SDK package in your application using the command:
npm install aws - sdk
or
npm install aws - sdk --save //save to package.json
Step 2: Write the code given below:
var AWS = require('aws-sdk'); var fs = require('fs'); //Constants var ACCESS_KEY_ID = '***********'; var SECRET_ACCESS_KEY = '***********'; var BUBKET_NAME = 'TEST-BUCKET'; var REGION = 'us-east-1'; //Initialize AWS.config.update({ accessKeyId: ACCESS_KEY_ID, secretAccessKey: SECRET_ACCESS_KEY, region: BUBKET_NAME, signatureVersion: "v4" }); //Upload To AWS S3 var uploadToS3 = function (fileName, PathToFile, callback) { s3 = new AWS.S3({ apiVersion: '2017-01-31' }); fs.readFile(PathToFile, function (err, file_buffer) { var params = { Bucket: BUBKET_NAME, Key: '/Images/' + fileName, // If folder not exists,it is auto. created on S3 bucket. ContentDisposition: 'attachment', Body: file_buffer }; s3.upload(params, function (err, data) { callback(err, data) }); }); } //Get file var getFileFromBucket = function (fileName, callback) { var s3 = new AWS.S3(); var params = { Bucket: configurationHolder.config.bucketname, Key: '/Images/' + fileName }; s3.getObject(params, function (err, data) { if (err) { callback(err, null); } else { console.log("downloaded successfully", data.Body); callback(null, data.Body); } }); } // List files var listFilesOnAWS = function (callback) { var s3 = new AWS.S3({ apiVersion: '2017-01-31', params: { Bucket: BUBKET_NAME }, region: REGION }); var params = { Bucket: BUBKET_NAME } s3.listObject(params, function (err, success) { callback(err, success); }); } // Delete files from AWS S3 var deleteFromAWS = function (FilePath, callback) { var s3 = new AWS.S3({ apiVersion: '2017-01-31', params: { Bucket: BUBKET_NAME }, region: REGION }); var params = { Bucket: BUBKET_NAME, Key: FilePath // File path stored on S3 } s3.deleteObject(params, function (err, success) { callback(err, success); }); } THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Pulkit Chadha
Pulkit is an expert web app developer and has good knowledge of JQuery, MongoDb, NodeJs, AngularJS, kaltura, Akamai. etc.