Amazon S3 Image Upload using Nodejs – AWS SDK

Amazon S3 Image Upload using Nodejs – AWS SDK

In this Post We Will Explain About is Amazon S3 Image Upload using Nodejs – AWS SDK With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Upload Files to Amazon S3 using Nodejs Example

In this post we will show you Best way to implement Upload an Object Using the AWS SDK for Nodejs, hear for Multiple Images Upload to Amazon S3 using Nodejs and API with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Phase 1: Create Amazon S3 Account

We need to create simple First of all Amazon S3 account and then get all the amazon get aws s3 bucket name as well as access keys to use for uploading on s3 server images.

Also Read :   Angular 4 Grid with CRUD operations | Angular 4 insert update delete

Phase 2: Install Amazon AWS SDK

You can install step by step aws sdk using Like as a npm command ‘npm i aws-sdk’ or second Put entry Like “aws-sdk”: “^2.175.0” into this package.json file and then simple run ‘npm update’ (CMD) command.

Phase 3: Configure Amazon AWS S3

I have All the Full Amazon S3 account Information, I will define simple Amazon S3 server account Information in app.js using server access key as well as get secret key.

//const AWS
const AWS = require('aws-sdk')

//config update accessKeyId and secretAccessKey
AWS.config.update({ accessKeyId: process.env.ACCESS_KEY_ID, secretAccessKey: process.env.SECRET_ACCESS_KEY });

I have stored web-application Key of the secrets as a set all the config env variable in the web server,You can use get the direct secret key as well as used to access key.

Phase 4: Upload Image using S3

I am planning to store All the data in Image base64 string into amazon AWS server, Therefor I shall convert all the image data into string base64 using nodejs web-app and send to store the all image amazon S3 server.I shall do following Phases to image upload files using amazon server aws s3 sdk.

Also Read :   NodeJS With MySQL Database Connection Tutorial Example

Make an S3 instance using amazon s3 aws-sdk class.

//declare const
const s3 = new AWS.S3();

We will All the convert image data into simple base64 format string using some Functions.

//const variable declare
const base64Data = new Buffer(base64.replace(/^data:image\/\w+;base64,/, ""), 'base64')

We will simple get image type Like as a: jpeg Image, png Image or gif Image

//const variable declare
const type = base64.split(';')[0].split('/')[1]

We will make final upload s3 server image params ,

//init All the Params
const params = {
  Bucket: process.env.S3_BUCKET,
  Key: `avatar`,
  Body: base64Data, //Body data
  ACL: 'public-read',
  ContentEncoding: 'base64', //It's required
  ContentType: `image/${type}` //type
}

After that We send some curl based request to simple S3 server to All the upload image with all the Data params.

Also Read :   Top 10 Most Popular programming languages

s3.upload(params, (err, data) => {
//check conditions
  if (err) { return console.log(err) }
  
  //Live24u Continue if no any error
  // Save data.Location in your Simple database
  console.log('Your Image successfully uploaded Good Luck.');
});

We have used to upload() functions but you can some also use other Data functions which is putObject().

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example





Example

I hope you have Got What is Upload an Object Using the AWS SDK for Nodejs And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.