How to use s3 bucket with node.js

Hello guys, in this tutorial I will show you how we can use amazon s3 bucket with nodejs to store your files and fetch them whenever required. Before we start let me explain what exactly is s3 Bucket.

Basically, amazon s3 is a cloud storage where you can store your data (pictures, videos, documents etc). S3 buckets are very simple to configure and have high availability and high security which makes it very popular. In s3 buckets you can easily do some important stuff like restricting access to some users, scaling as much as you want and also automatically determining the life of the items which are getting stored.


Now, in order to use s3 bucket to store data you first need to create a bucket and for this, you need an AWS account. You can follow my previous post for creating a new AWS account if you need any help. You can have a maximum of 100 buckets linked to each of your AWS accounts. In order to create a bucket, you need to provide a name for your bucket and AWS region where you want to have your bucket. Its good to have a bucket in a region geographically close to you which helps in reducing the cost and also accessing time of objects stored in the bucket.

Now once your bucket is created you can use s3's RESt APIs to access your bucket. Although Amazon also has s3 console which can also be used to access buckets and do operations on it. But as in this post our goal is to use s3 bucket with nodejs so we will be focussing on programmatically ways to access buckets and do operations on them using RESt APIs.

As we have gathered some basic info about the s3 bucket so we can start with our nodejs application. In this tutorial, we will be creating a single page node application having a button to select pictures from your device and one Submit button which will upload the selected picture to the s3 bucket. Also in the bottom section of the page, we will be displaying all the uploaded pictures by fetching them from the s3 bucket.



So in order to start with this application first thing we need is to install aws-sdk for nodejs, which we will be using to connect with amazon s3 and make our api calls. After that, we need to initialize our aws s3 sdk for which we need to give our aws credentials. Its always recommended to create a new IAM user for each specific purpose in amazon and use their credentials instead of giving our root credentials. For initializing store your credentials in a config.json file and give the path of this file to initialize sdk like this:-

AWS.config.loadFromPath('config.json');

Here is how your config.json file should look


Since this file contains your secret credentials make sure not to expose it somewhere. After your aws-sdk is initialized you need to create a bucket where you can store your files(images). For creating the bucket we will be using createBucket API of s3, this requires just one parameter i.e. bucketName. After your bucket got created successfully we can then upload images to this bucket using putObject API.

Here are the code snippets for creating and uploading buckets in s3:

In createBucket before creating bucket we need to check if bucket is already created or not so or this we need headBucket, this api is used to determine if a bucket exists and you have permission to access it. If its present with appropriate permissions then this will return 200 status and hence no need to create a new bucket otherwise this will give 404 or forbidden and hence we will have to create a new bucket using createBucket api.

You can see the entire source code of the project here on my Github. Just give your correct AWS credentials in the config.json file and you can test the entire app. That's all I have in this post, feel free to comment below if you have any queries.

Reference:
aws s3 docs

Comments

Popular posts from this blog

All about NIT Agartala

How to integrate a payment gateway into your WordPress website for donations without using woocommerce

Implementing a Self Organizing List in Go