How do you download multiple txt files with fastGet? My code is as follows:
const Client = require('ssh2-sftp-client');
const sftp = new Client();
sftp.connect(configs)
.then(() => {
return sftp.list('.');
})
.then(files => {
files.forEach(file => {
if(file.name.match(/txt$/)){
const remoteFile = // remote file dir path
const localFile = // local file dir path
sftp.fastGet(remoteFile, localFile).catch(err => console.log(err));
}
});
})
.catch(err => console.log(err))
.finally(() => {
sftp.end();
});
I keep getting a no sftp connection available error. I'm pretty sure I'm doing a few things wrong here with sftp.fastGet but don't know exactly what or where to start.
There seem to be multiple issues in your code:
loop
through files should be executed in the firstthen
block itself.sftp.fastGet
returns a promise, hence its anasynchronous
, operation, and executingasynchronous
operations inside aforEach
loop is not a good idea.I would recommend updating your code with following changes: