How to download all folders and files with ssh2-sftp-client in node.js

1.6k Views Asked by At

So as the title says, im trying to download all folders and files stored in a sftp server, so im using ssh2-sftp-client package to accomplish this, but at the moment ive just managed to download single files in the folder specified in the controller, here is the code

    checkCtrl.checkSftp = async (req, res) => {
  console.log(req.body);
  console.log('wtf men');
  const {url,username,password,port} = req.body

  const host = url;
  
  sftp.connect({
    host,
    port,
    username,
    password,
    keepaliveInterval :2000,
    keepaliveCountMax :20
  }).then(() => {
    return sftp.list('/');
  }).then(async (data) => {
    console.log(`Remote working directory is ${data}`);
    console.log(data);

    len = data.length;
    // x is one element of the array
    await data.forEach(x => {
        let remoteFilePath = '/' + x.name;
        sftp.get(remoteFilePath).then((stream) => {
            // save to local folder ftp
            let file = './ftp/' + x.name;
            fs.writeFile(file, stream, (err) => {
                if (err) console.log(err);
            });
        });
    });

    // res.status(200).send(`Success`)

    // return sftp.end();
  }).catch(err => {
    res.status(400).send('error')
    console.log(err, 'catch error');
  });

};

i just want to be able to download all files in the folder specified

also if there is any recomendation to do the same as for a FTP server would be appreciated

1

There are 1 best solutions below

1
On

I was able to download all of the .csv files in a given folder using this example:

https://github.com/theophilusx/ssh2-sftp-client/blob/master/example/downloadDir.js

I only wanted .csv files, so I added:

const filter = '(\\.csv$)'

let rslt = await client.downloadDir(src, dst, filter);