+91 9404 340 614    gyaanibuddy@gmail.com

Like
1 Like

Using Node.js + S3 to create,delete,list buckets ,upload, list objects — Part 1

Last updated on Feb. 24, 2021, 2:31 p.m. by rugved62321

Using Node.js + S3 to create,delete,list buckets and upload, list objects

Introduction

Most of the websites require hosting to host images,videos etc. One obvious way to do it is to store it in hard drive. That seems alright but what if size of this storage required exceeds to hard drive storage limit. We will need to scale it accordingly which is a bit tedious task. So here hosting service like AWS S3 comes into picture which can store huge amount of media files and can scale accordingly.

This blog has two parts:

Part I — Learn how to setup aws s3 and manually use it.

Part II — Using node js programatically perform operations on S3.

What is AWS S3?

Amazon Simple Storage Service is storage for the Internet. It is designed to make web-scale computing easier for developers.

Amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.

source: https://docs.aws.amazon.com/AmazonS3/latest/dev/Welcome.html

What are we building?

In this blog we will see node js implementation to do the following:
(Code will be covered in Part II but before that we need to do some setup thus follow this blog till the end before moving to Part II)

  1. Create bucket on S3 (like a specific unique folder to store our media)
  2. List out all the buckets made by us.
  3. Upload images to the bucket.
  4. List out all the objects(images,videos etc) in the bucket
  5. Delete the bucket.

Lets begin

Setup

  1. Create an AWS account if don’t have one. Login into your AWS account.
  2. After login, on the top bar you will see your name with a dropdown sign. Click it.
  3. Select “My Security Credentials
    The following screen will appear.

 

Image for post

4. Click on “Create New Access Key” button.

 

Image for post

5. Click on “Download Key File” or “Show Access Key

6. In both cases you will get the following
— Access Key ID
— Secret Access Key
Note it down , we will require it further.

Accessing S3 console

In the search bar at top type out s3. Click on the first option. You will be directed to a page showing list of currently buckets. If you are using it for the first time ,it will be a empty list.

 

Image for post

Before moving to code lets do it manually and understand what S3 is.

  1. Click on create bucket button.

 

Image for post

2. Give a unique name to the bucket. Note that this should be a unique name.

 

Image for post

3. Untick “Block all public access”. This is not a recommended setting in production but for now just to learn how S3 works lets untick it.

 

Image for post

4. Scroll to bottom and click “Create bucket” button.

5. It will redirect you to page where all of your buckets will be displayed. Click on the bucket name you just created from the list.

 

Image for post

7. Click on upload button and upload a image. And now you will see uploaded image in the list.

 

Image for post

8. Click on the image item in the list and on the next page you will find “Object URL”. This will the image url by which you can access image uploaded by you globally.

In my case object url was https://my-unique-test-bucket-123.s3.us-east-2.amazonaws.com/daniel-norin-lBhhnhndpE0-unsplash.jpg

 

Image for post

Well did it work?? i guess no. Don’t worry.
We need to add “Bucket policy”.

9. Click on permissions.

 

Image for post

10. Under the “Block public access (bucket settings)” click on “Edit” button and untick “Block all public access”. Depending on your requirements you can tick/untick options but for now untick all as shown below and save.

 

Image for post

11. In the permissions tab, scroll down to “Bucket policy” click on“Edit” button and add the following code to it and click save.

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::my-unique-test-bucket-123/*"
        }
    ]
}

NOTE: Replace “my-unique-test-bucket” in the above snippet with your bucket name.

To understand how bucket policies are written read here. For now we will simply use the above code snippet.

10. Phew thats it! Now again try to access the image url. In my case it was

https://my-unique-test-bucket-123.s3.us-east-2.amazonaws.com/daniel-norin-lBhhnhndpE0-unsplash.jpg

 

Image for post

Yayy… Its working!

That’s it for this blog.

See you in the next blog where we will see how to use node js to upload file, create,list buckets and its contents etc.

Link for the Part II
https://medium.com/@rugved-bongale/using-node-js-s3-to-create-delete-list-buckets-and-upload-list-objects-part-2-fb1b76da36dc

Conclusion:

In this blog we have successfully completed the minimum setup required for using AWS S3.

...

by rugved62321
KJ Somaiya College of Engineering Mumbai

Gyaanibuddy
blog comments powered by Disqus