Open In App

How To Read File From Amazon S3 Bucket Using Node.Js ?

The AWS Simple Storage Service (S3) is a cloud service provided by Amazon Web Services (AWS) to store your data securely. There are different approaches to storing and retrieving data from AWS S3; one of them is by using aws-sdk provided by Amazon Web Services. In this article, we will provide you with step-by-step instructions on how to use aws-sdk to upload and retrieve files from Amazon S3 securely.

What Is Amazon S3?

Amazon S3 is a simple storage service provided by Amazon Web Services. It is highly durable and provides security and scalability to any kind of data. This data is stored as objects within resources called buckets.

There are several approaches to storing and retrieving data from the S3 bucket. One of them is using the aws-sdk provided by AWS. This can be installed on any node application to communicate with the S3 bucket.



Reading A File From Amazon S3 Bucket Using Node Js

Create an AWS account and an IAM Role. Identity and Access Management (IAM) helps you secure the services you want to use by providing authentication and authorization as per your team’s requirements. Use the IAM role for accessing the S3 bucket data instead of directly using your main or root account.



Step 1: Create An Amazon S3 Bucket

Step 2: Configure The Amazon S3 Bucket

Now, your bucket is ready to be integrated into the node application.

Setting Up AWS SDK On Your Node Server

Step 4: Installation And Set Up

npm install aws-sdk fs

Step 5: Connect Node Service To The Amazon S3 Bucket

Upload A File On The Amazon S3 bucket

Step 6: Prepare The File To Be Uploaded

Step 7: Write The Function To Upload File To Amazon S3 Bucket

Step 8: Execution

node index.js

Step 9: Verify if file is visible on the S3 bucket.

There is one more file Node_to_aws_GFG.drawio.png available in the list. Let’s check how can we download that file from our Node js application.

Retrieve The File From Amazon S3 bucket

Step 10: Create a Write Stream to download the file.

Step 11: Create The Parameter Object

Step 12: Execution

node index.js

In this way we can retrieve a file from the AWS S3 Bucket.

AWS S3 Bucket With Node Js – FAQ’s

Is The Data Uploaded On Amazon S3 Accessible To Everyone?

You can add permissions to your file/data which you want to upload and provide access to specific users. You can also keep it public, available to everyone.

How Much Data Can You Store On Amazon S3?

As per the AWS document, the total volume of data and number of objects you can store in Amazon S3 are unlimited. Individual Amazon S3 objects can range in size from a minimum of 0 bytes to a maximum of 5 TB.

Can You Rename A Amazon S3 Bucket?

No, you will need to create a new bucket with the new name.

What Are The Other Ways To Upload And Download File On Amazon S3?

Some of the common ways are using AWS CLI and operating directly from the Amazon console.


Article Tags :