10 minutes read

How to Upload Files from Node JS to AWS S3?

Summary:It is important to separate static files from your codebase. This is a simple tutorial to upload static files from Node JS to Amazon S3 using the AWS SDK.

AWS S3 is a very popular cloud service to host static files. The usefulness of a cloud is also determined by the developer’s experience. Amazon S3 is very easy to integrate with different programming languages.

In this post, we will explore how we can upload files from Node JS to AWS S3 easily.

Preparing the AWS S3 for Uploads

The first thing is that you need to create an account on AWS S3 and activate billing by adding a credit card. Once you have completed this step you can get started with AWS S3 service.

Create Security Access Credentials

Before we can actually interact with AWS S3, we need to create security access keys.

The step is very simple.

1.) Click on your username and click ‘My Security Credentials’.

2.) Now under the access keys click on ‘Create New Access Key’.

3.) Download the access keys and make sure to keep them protected.
All done for access keys, now we can move ahead.

Create an S3 Bucket

Buckets, as the name suggests, holds your files. This is a critical step and it is important to follow correctly because certain settings cannot be changed once the bucket is initialized. You need to create a new bucket to change those settings.

There are two ways to create buckets in AWS S3. The first one is using the GUI and the other one using Node JS.

The first option will do the task most of the times. It is a very rare situation to create buckets programmatically, used only when you have a very complex application.

1.) Click on ‘Services’ from the navbar and then click on S3 under storage.

2.) Now click on ‘Create Bucket’ button.

3.) Enter the bucket name and region.

Choose the region which is close to most of your website visitors. If you are not sure then leave the default settings.

You can configure other settings or leave them at default.

Hit the Create Bucket at bottom of the page to create the bucket.

Done. Now the bucket is ready and we can jump to coding section to get started with Node JS to upload files to AWS S3.

Using Node JS to Upload Files to AWS S3

The easiest way to get started with Node JS and AWS S3 to upload files is through AWS SDK.

Install the AWS SDK

Install the AWS SDK using any package manager. I prefer using npm for my projects.

npm i aws-sdk

Upload files from Node JS to Amazon S3

From here, the real coding begins.

  • The first step is to import the AWS SDK and fs module in our project.
const AWS = require("aws-sdk");
const fs = require("fs");

AWS SDK has inbuilt API’s to interact with the AWS and the fs module is to read the files that we need to upload on the cloud.

  • The second step will be to initialize the S3 client on our server.
const s3 = new AWS.S3({
  accessKeyId: YOURACCESSKEYID,
  secretAccessKey: SECRETACCESSKEY,
});

Fill the placeholders with your credentials.

It is good practice to store sensitive details in environment variables. They are relatively safe because hardcoding keys in code have risk when sharing code with others.

  • The third step will be to upload the required file.
const fileData = fs.readFileSync('./holyCoders.png');

const params = {
	Bucket: YOURBUCKETNAME', //Enter the bucket name that you created
	Key: 'holycoders.png', //filename to use on the cloud
	Body: fileData,
    ContentType: 'image/png',
    CacheControl: "max-age=172800",
	ACL: "public-read", //To make file publicly accessible through URL
};

s3.upload(params, function(error, data) {
	if (error) {
		throw error;
	}
	console.log(`File was Uploaded Successfully. ${data.Location}`);
});

Replace the bucket parameter with your own bucket name.

The parameter ContentType is useful only if you want browsers to open that specific file. In this above example browser will know this is a PNG image file and display it as same.

You can adjust the CacheControl header as per your need. It saves some bandwidth on frequent resources request.

Conclusion

This was the easiest way to upload file to AWS S3 with Node JS. There are many configuration settings which you need to explore. I tried to keep this tutorial it simple and beginners friendly.

If you are using React JS to upload files, then you can use multer-s3 along with AWS SDK to upload files to S3 from React JS → Node JS → AWS S3.

If you faced any problem then please share them in the comments section.