Simple usage of AWS-S3 with NodeJS application

İlhan Sönmez
3 min readMar 28, 2021

The main purpose of this article is to understand the basics of uploading data to AWS-S3 buckets. Before start, Don’t forget to create an s3 bucket and an IAM User with the required permissions to your bucket on your AWS Console. You can find guides about these steps at the end of this article :)

I’ve taken educations about computer science. After a while, I started to interest in web technologies. I’m new to web development and I wanted to share a little part of my last NodeJS project with you.

I want to start with the awsService.js module. First, we are going to import the AWS Library to use. Then we are going to update our configuration as needed. This project was about storing some data in S3 buckets, so we will use the upload function of the AWS Library, then we will export and use it in our main javascript file.

// 1- Import the AWS Library
var AWS = require('aws-sdk');
// 2- Update region in AWS.config
AWS.config.update({region: 'Your buckets region here'});
// 3- We will define a few constants with necessary information
const bucket = 'Your buckets name here';
const IAM_USER_KEY = 'Your IAM user key here';
const IAM_USER_SECRET = 'Your IAM user secret here';
// 4- Then we are going to use AWS.S3 and those constants to define
// an s3 service interface object.

const s3 = new AWS.S3({
accessKeyId: IAM_USER_KEY,
secretAccessKey: IAM_USER_SECRET,
Bucket: bucket
});
// 5- Now we can define our own upload function by using s3
// object's upload function. This function is async because s3
// object's upload function is async.

const upload = async (data) => {
// 6- Define an object to keep our data
const uploadParams = {Bucket: bucket, Key: '', Body: ''};
// 7- Convert data into string and put it into that object
uploadParams.Body = JSON.stringify(data);
// 8- Define the key, you can assume it is the filename :)
uploadParams.Key = 'filename.txt';
// 9- Now, let's call the s3 object's upload function and
// convert it to a Promise. So we can wait for the process,
// then/catch return whatever we want

await s3.upload(uploadParams).promise()
.then((data) => {
console.log(`File uploaded successfully. ${data.Location}`);
})
.catch((err) => {
console.error("Upload failed", err);
});
};
// 10- Let's export our upload function to use it in main javascript
// file

module.exports = { upload };

Now, all we need to do is, import our upload function into the main javascript file and use it!

// Import our awsService module
const awsService = require('./awsService');
// Call an async function directly to keep it short :)(async() => { // Get and process your data, then pass it to our upload
// function which is in the awsService module
await awsService.upload(data);
})();

This is a very simple way to upload your data to AWS-S3 buckets. There are many different functions in AWS Library for different purposes and operations. I will leave a few links down below, which I believe you will find very useful informations :)

--

--