Sunday, February 6, 2022

File handling in AWS S3 by Node Js

 Today, I am going to discuss about file handling operations like upload a file, get list of all the files, delete a file and download files from S3 bucket in AWS. To perform all these operation, we should follow the below steps:

  1. Create an user in IAM service in AWS
  2. Create a bucket in S3 service in AWS
  3. Save user credential in a shared file in your local
  4. Create a NodeJS application
  5. Install aws-sdk package
  6. Create routes for handling files
Now, lets discuss about each steps here.
1) Create an user in IAM service in AWS: We should go to the IAM service in AWS and click on "Add users button".


Add the user name and select the check box for "Access key - Programmatic access" and then click on "Next: Permissions"

Click on the "Create group" button and then add a group name and then click on Create group button.

Now, on group list page, click on your newly created group and then click on Permissions tab. Now, Add Permissions dropdown box and click on attach Policy.


Now, click on Create Policy and select service as S3. Further, select check boxes of Read, List and Write


So, up to here, your user and required group and policy is created. Now, note down the user access key id and secret access key. These two keys are going to be used in NodeJS programming.

2) Create a bucket in S3 service in AWS: Now, we should go to the S3 service of AWS and create a bucket with unique name in AWS.




3) Save user credential in a shared file in your local: In this steps, we should save the user credential so that node js application can read it while establishing connection with AWS. We can refer below AWS documentation for this step:


4) Create a NodeJS application: Here, create a very simple node js application with Express. Code will be given in the below.

5) Install aws-sdk package: In your node js application, install aws-sdk package by using command like npm install aws-sdk

6) Create routes for handling files: Below is the code for different routes like upload a file in S3, get list of all the files from the S3 bucket, delete a file, download a file.

const express = require('express');
const dotenv = require('dotenv');
const path = require('path');
const multer = require('multer');
const AWS = require('aws-sdk');

const s3 = new AWS.S3();
const app = express();
const upload = multer();
dotenv.config({
  path: path.join(__dirname, './.env')
});

app.listen(8000);

//Creating routes
//uploading file to s3
app.post('/upload', upload.single('file'), async (req, res) => {
  try {
    const result = await s3.putObject({
      Body: req.file.buffer,
      Bucket: process.env.BUCKET_NAME,
      Key: req.file.originalname
    }).promise();
    res.send(result);
  } catch (error) {
    console.log(error);
    throw error;
  }  
});

//Listing uploaded files
app.get('/fileList', async(req, res) => {
  try {
    const result = await s3.listObjectsV2({
      Bucket: process.env.BUCKET_NAME
    }).promise();
    res.send(result);
  } catch (error) {
    console.log(error);
    throw error;
  }
});

//Deleting a file
app.delete('/deleteFile/:fileName', async(req, res) => {
  try {
    const fileName = req.params.fileName;
    const result = await s3.deleteObject({
      Bucket: process.env.BUCKET_NAME,
      Key: fileName
    }).promise();
    res.send(result);
  } catch (error) {
    console.log(error);
    throw error;
  }
})

//Download a file
app.get('/downloadFile/:fileName', async(req, res) => {
  try {
    const fileName = req.params.fileName;
    const result = await s3.getObject({
      Bucket: process.env.BUCKET_NAME,
      Key: fileName
    }).promise();
    res.send(result.Body);
  } catch (error) {
    console.log(error);
    throw error;
  }
})














No comments:

Post a Comment

Please provide your precious comments and suggestion