YavarTechWorks
Published in

YavarTechWorks

Amazon s3 bucket image upload using node.js

In this article, I will explain about storing the image in S3 bucket and save URL in your local storage.

google image

Before we move to write we have to create an S3 bucket. The link helps you to create a bucket. Click here

Once you create a bucket then move to write coding.

In AWS account, you have these things

  1. AWS_ACCESS_KEY_ID,

2. AWS_SECRET_ACCESS_KEY

3. Bucket name

4. Region

Various ways we can store images in the S3 bucket. In this example, we store images using base64 format.

Convert base64 to image buffer.

https://carbon.now.sh

AWS-SDK

Install plugin

npm install aws-sdk

Once you installed successfully then move to set up AWS-SDK.

https://carbon.now.sh

Set your ‘AWS_ACCESS_KEY_ID’ and ‘AWS_SECRET_ACCESS_KEY’ in your .env file.

Set your region and bucket name from your AWS account.

PutObject function

https://carbon.now.sh

In the above picture we define these things

key: Path of images (‘/user/1.jpg’).

buffer: Image buffer.

ContentEncoding: 'base64',
ContentType: 'image/jpeg',
ACL: 'public-read'

S3Url:

We can set our uploaded image URL

You can access your image by using:

https://s3.amazonaws.com/bucketname/foldername/imagename.jpg

or if there are no folders, you can do:

https://s3.amazonaws.com/bucketname/imagename.jpg

Get uploaded image URL:

https://carbon.now.sh

The function return image URL.

Once you get image URL then store in your local storage.

Once I get the image URL then I store the image URL in my PostgreSQL DB.

--

--

--

Today a Reader, Tomorrow a Leader

Recommended from Medium

6 Differences Every JavaScript Developer Should Know

Selection Sort Algorithm 101

Expo Detached App: From ‘npm install’ to Store: Part 1 — Managing JS bundle for different…

六 (Japanese Kanji) — six

How I was able to achieve goal of using reactjs as frontend and django as backend api server for…

Learn GatsbyJS by creating a tourism site -11

Understand closures in javascript

Refusing to convert from directory to symlink with Ansible

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Edison Devadoss

Edison Devadoss

Software developer / JavaScript / React / React Native / Firebase / Node.js / C Programming / Book Reader / Story Teller

More from Medium

How to Build an Authentication API with JWT Token in Node.js

An opiniated skeleton for dockerized node js apps with postgreSQL and sequelize

Connect to a MongoDB Database Using Node.js with mongoose

Node Js With MongoDB