how to upload file to s3 bucket using node js code example

Example 1: how to upload file to s3 nodejs

const fs = require('fs');
const AWS = require('aws-sdk');

const s3 = new AWS.S3({
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});

const fileName = 'contacts.csv';

const uploadFile = () => {
  fs.readFile(fileName, (err, data) => {
     if (err) throw err;
     const params = {
         Bucket: 'testBucket', // pass your bucket name
         Key: 'contacts.csv', // file will be saved as testBucket/contacts.csv
         Body: JSON.stringify(data, null, 2)
     };
     s3.upload(params, function(s3Err, data) {
         if (s3Err) throw s3Err
         console.log(`File uploaded successfully at ${data.Location}`)
     });
  });
};

uploadFile();

Example 2: node js read file from s3 bucket

# This function for read/download file from s3 bucketconst s3download = function (params) {    return new Promise((resolve, reject) => {        s3.createBucket({            Bucket: BUCKET_NAME        /* Put your bucket name */        }, function () {            s3.getObject(params, function (err, data) {                if (err) {                    reject(err);                } else {                    console.log("Successfully dowloaded data from  bucket");                    resolve(data);                }            });        });    });}