Uploading files to AWS S3 Bucket using Node.js

Akila De Silva
4 min readAug 20, 2021
Uploading files to AWS S3 bucket using Node.js

Intro

When developing web and mobile applications developers may have to provide user the ability to upload images, photos, avatars etc. So as a developer you should choose the best way to store these files. There are several approaches to store files,

  • storing files directly on the server
  • storing files in the database as a binary file
  • using some cloud storages

For cloud storages AWS S3 is a good option, here is why:

  • Access Management with S3 which allows you to control the access of a bucket.
  • S3 is easy to integrate with other services available on AWS.
  • AWS supports a lot of programming languages.
  • S3 provides an interface to manage files in the bucket.

Step 01- Create an AWS account.

  • Open the Amazon Web Services (AWS) home page.
  • Choose Create an AWS Account.
    Note: If you signed in to AWS recently, choose Sign in to the Console. If Create a new AWS account isn’t visible, first choose Sign in to a different account, and then choose Create a new AWS account.
  • Enter your account information, and then choose Continue.

Step 02 — Create a bucket in AWS S3 that will store my static files.

To start using S3 Bucket you need to create an account on Amazon website. (you will not be charged for creating an account) After account creation we need to create s3 bucket. Go to Services -> S3

Then press ‘Create bucket’ button.

Enter your bucket name (should be unique) and choose region that is closest to you. Press ‘Create’ button.

Step 03 — Create an IAM Group and attach policies.

Now your bucket is created but we need to give permission for users to access this bucket. It is not secured to give the access keys of your root user to your developer team or someone else. We need to create new IAM user and give him permission to use only S3 Bucket. AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources.

Step 04 — Add IAM Users to the Created Group.

Let’s create such user. Go to Services -> IAM. In the navigation pane, choose Users and then choose Add user.

Enter user’s name and check Access typeProgramatic access’. Press next button. We need to add permissions to this user. Press ‘Attach existing policy directly’, in the search field enter ‘s3’ and among found permissions choose AmazonS3FullAccess.

Then press next and ‘Create User’. If you did everything right then you should see Access key ID and Secret access key for your user. There is also ‘Download .csv’ button for downloading these keys, so please click on it in order not to loose keys.

Node.js application

Step 05 - Save in the .env file in your node.js application

const accessKeyId = process.env.AWS_ACCESS_KEY;const secretAccessKey = process.env.AWS_SECRET_KEY;const bucketname = process.env.AWS_BUCKET_NAME;const bucketname1 = process.env.AWS_BUCKET_NAME1;

Step 06 - use multer s3 to upload files and shortid to create an id and naming the uploaded file.

const multer = require('multer');const multerS3 = require('multer-s3');const aws = require('aws-sdk');const shortid = require('shortid');require('dotenv').config();

Step 07 - create aws.s3 object

const s3 = new aws.S3({accessKeyId,secretAccessKey,});

Step 08 -Finally the function to upload files to the s3 bucket

exports.upload = multer();exports.uploadS3 = multer({storage: multerS3({s3,bucket: bucketname,// acl: 'public-read',metadata(req, file, cb) {cb(null, { fieldName: file.fieldname });},key(req, file, cb) {cb(null, `${shortid.generate()}-${file.originalname}`);},}),});

--

--

Akila De Silva
0 Followers

Full Stack developer, Cloud Enthusiast, Undergraduate at UCSC