Streamlining File Transfers with Node.js, SSH2, and SFTP

May 15, 2024

Introduction

In the realm of server-side development, efficient file management and transfer are paramount. Whether it's deploying applications, synchronizing data, or maintaining backups, having robust tools at your disposal is crucial. In this article, we'll delve into advanced file transfer techniques using Node.js, SSH2, and SFTP (SSH File Transfer Protocol). By harnessing the power of these technologies, you can streamline your file management processes and enhance the security of your data transfers.

Prerequisites

To follow along with the examples in this article, you'll need:

Setting up SSH2 and SFTP

Before we dive into the code, let's set up our development environment by installing the necessary Node.js modules:

npm install ssh2 ssh2-sftp-client

The ssh2 module provides a high-level API for SSH interactions, while ssh2-sftp-client offers convenient methods for working with SFTP.

Uploading a Folder to a Remote Location

Our first task is to upload a folder from our local machine to a remote Linux server using SFTP. Here's how we can accomplish this programmatically:

const { SftpClient } = require('ssh2-sftp-client');

// SFTP Configuration
const sftpConfig = {
    host: 'your_remote_host',
    port: 22,
    username: 'your_username',
    password: 'your_password'
};

// Local Folder to Upload
const localFolderPath = '/path/to/local/folder';

// Remote Location
const remoteUploadLocation = '/path/to/remote/upload/location';

// Connect to SFTP server and upload folder
async function uploadFolder() {
    const sftp = new SftpClient();
    try {
        await sftp.connect(sftpConfig);
        await sftp.uploadDir(localFolderPath, remoteUploadLocation);
        console.log('Folder uploaded successfully.');
    } catch (err) {
        console.error('Error uploading folder:', err);
    } finally {
        await sftp.end();
    }
}

// Execute the upload function
uploadFolder();


Copying the Uploaded Folder to Another Location

Once the folder is uploaded, we might need to duplicate it to another location on the same server. We can achieve this using SSH2 low-level access to SSH functionality. Here's how:

const { Client } = require('ssh2');

// SSH Configuration
const sshConfig = {
    host: 'your_remote_host',
    port: 22,
    username: 'your_username',
    password: 'your_password'
};

// Remote Locations
const remoteUploadLocation = '/path/to/remote/upload/location';
const remoteCopyLocation = '/path/to/remote/copy/location';

// Copy uploaded folder to another location using SSH2
async function copyFolder() {
    const conn = new Client();
    try {
        conn.on('ready', () => {
            console.log('SSH Connection established.');
            const remoteCommand = `cp -r ${remoteUploadLocation} ${remoteCopyLocation}`;
            conn.exec(remoteCommand, (err, stream) => {
                if (err) throw err;
                stream
                    .on('close', (code, signal) => {
                        console.log(`Copy completed with code ${code}`);
                        conn.end();
                    })
                    .stderr.on('data', data => {
                        console.error('STDERR:', data);
                        conn.end();
                    });
            });
        }).connect(sshConfig);
    } catch (err) {
        console.error('Error copying folder:', err);
    }
}

// Execute the copy function
copyFolder();


Conclusion

In this article, we've explored advanced file transfer techniques using Node.js, SSH2, and SFTP. By combining the high-level API of SSH2-SFTP-Client with the low-level capabilities of SSH2, we can seamlessly upload folders to remote locations and copy them within the server environment. This approach not only enhances the efficiency of file management but also ensures the security and integrity of your data transfers. Whether you're deploying applications, managing backups, or synchronizing data, mastering these techniques will empower you to handle file transfers with confidence in your Node.js projects.

Related:

← Back to home

Raunak