Using ssh2 npm module for SFTP Implementation

Posted By : Ankit Uniyal | 29-Dec-2017

In this blog, we will discuss about the ssh2 npm module for SFTP implementation.We will cover up how to install ssh2 npm module in NodeJS and also usage of its different methods one by one.

Installation :

npm install ssh2

Below are the different methods of ssh2 npm module :

        var Client = require('ssh2').Client;
        var connSettings = {
           host: 'your_host_name',
           port: portNumber,
           username: 'your_user_name',
           password: 'your_password'
       };
        

1.Connect to SFTP server using ssh2 :

 var conn = new Client();
 conn.on('ready', function() {
     conn.sftp(function(err, sftp) {
         if (err) {
             console.log("Errror in connection", err);
             next(err);
         } else {
             console.log("Connection established", sftp);
         }
     });
 }).connect(connSettings);    
        

2.Listing directories via SFTP :

  sftp.readdir(remoteServerFilePath, function(err, list) {
    if (err) {
        console.log("Error on listing directory", err);
    } else {
        console.log("listing directory", list);
        conn.end();
    }
 }); 
   

3.Upload file on SFTP server :

        var options = Object.assign({}, {
            encoding: 'utf-8'
        }, true);
        var writeStream = sftp.createWriteStream(filePath, options);
        var data = writeStream.end(fileStreamContent);
        writeStream.on('close', function() {
            console.log("- file transferred succesfully");
            conn.end();
        });
        

4.Make directory on SFTP server :

        sftp.mkdir(remoteServerFilePath, 777, function(err) {
            if (err) {
                console.log("Error in directory creation", err);
                conn.end();
                next(err);
            } else {
                console.log("Directory created on first SFTP server");
                conn.end();
            }
        });
 });
        

5.Check if Directory exists on Server :

        sftp.exists(remoteServerFilePath, function(err) {
            if (err) {
                console.log("Error existing directory", err);
                conn.end();
            } else {
                console.log("Directory exists");
                conn.end();
            }
        })
        

6.Remove directory from Server :

        sftp.rmdir(remoteServerFilePath, function(err) {
            if (err) {
                console.log("Error in removing directory", err);
                conn.end();
            } else {
                console.log("Directory remove from server");
                conn.end();
            }
        });
        

 

Note : Always make sure that you end the sftp connection after the process is complete.

 

Thanks

About Author

Author Image
Ankit Uniyal

Ankit has knowledge in Javascript, NodeJS, AngularJS and MongoDB also have experience in using AWS Services.

Request for Proposal

Name is required

Comment is required

Sending message..