正如标题所说,我试图下载存储在 sftp 服务器中的所有文件夹和文件,所以我使用 ssh2-sftp-client 包来完成此操作,但目前我只是设法下载了指定文件夹中的单个文件控制器,这是代码
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');
});
};
我只想能够下载指定文件夹中的所有文件
另外,如果有任何建议可以为 FTP 服务器做同样的事情,我们将不胜感激