Search code examples
node.jsmongodbhapi.jsimport-csv

How to upload and process csv file with nodejs and [email protected]


I started working with an existing project which is build with Hapi's older version ([email protected]) and node version is also older on server. And i got a task to upload CSV file which includes Email address of users and i need to process users data with the help of email provided in the CSV file.
List of task is as follow:-

  • Task 1. Upload and save CSV on server inside a directory "root/uploaded_csv".
  • Task 2. Read and process CSV data to search users in the database with the help of email mentioned in the CSV at Column 2.
  • Task 3. Save imported CSV file on AWS s3 server.

As i am dealing with older node and hapi's versions and at that stage i can't even update all the versions and dependency.


Solution

  • Step 1. Receive CSV as API parameter as follow

          payload: {
              maxBytes: 20715200,
              output: 'stream',
              parse: true,
              allow: 'multipart/form-data'
          },
    

    Step 2. Validate imported CSV file as follow:

    validate: {
    importedCsv: Joi.any()
                        .meta({swaggerType: 'file'})
                        .required()
                        .allow('')
                        .description('CSV file')
    
              },
    

    Step 3. To Upload and save CSV on server inside a directory "root/XXX".

    csvFileName = ""+moment().utc().format('XXXX-XX-XX')+".csv";
              csvFilePath = Path.resolve(".") + "/XXX/" + csvFileName ;
              var file = fs.createWriteStream(csvFilePath);
              file.on('error', function (err) {
                 console.log(err.message);
              });
              payload.importedCsv.pipe(file);
              payload.importedCsv.on('end', function (err) {
                  if(err){
                    cb(ERROR);
                  }else{
                    cb(null);
                  }
              });
    

    Step 4. To Read and process CSV data to search users in the database with the help of email mentioned in the CSV at Column 2.

    var obj = csv();
    obj.from.path(csvFilePath).to.array(function (data) {            
    async.forEach(data, function (item, callback){
        /** Data processing and query part **/
        console.log(item[1]); /* will print email from the second cloumn in the CSV */
    }, function(err) {    
          cb(null);
       });
    });
    

    Step 5. To save imported CSV file on AWS s3 server.

    fs.readFile(csvFilePath, function (error, fileBuffer) {
                var accessKeyId = XXXXXX;
                var secretAccessKeyId = XXXXXX;
                AWS.config.update({accessKeyId: accessKeyId, secretAccessKey: secretAccessKeyId});
                var s3bucket = new AWS.S3();
                var params = {
                    Bucket: XXXXXXX,
                    Key: 'XXX' + '/' + csvFileName,
                    Body: fileBuffer,
                    ACL: 'public-read',
                    ContentType: payload.importedCsv.hapi.headers['content-type']
                };
    
                s3bucket.putObject(params, function (err, data) {
                    if (err) {
                      // cb(ERROR);
                    }else{
                      // cb(null);
                    }
                });
    });