Node.js 如何在ssh2 sftp客户端中使用npm cli进程
我有一个npm ssh2 sftp客户端项目,用于从远程服务器下载文件,因此我想在控制台中显示下载进度。下载文件可以正常工作,但我不知道如何在下载文件时使用cli进度显示下载进度Node.js 如何在ssh2 sftp客户端中使用npm cli进程,node.js,ssh2-sftp-client,Node.js,Ssh2 Sftp Client,我有一个npm ssh2 sftp客户端项目,用于从远程服务器下载文件,因此我想在控制台中显示下载进度。下载文件可以正常工作,但我不知道如何在下载文件时使用cli进度显示下载进度 function getConnect(ip, name, pwd, remotepath, localpath) { const sftp = new SftpClient(); sftp.connect({ host: ip, port: 22, username: name,
function getConnect(ip, name, pwd, remotepath, localpath) {
const sftp = new SftpClient();
sftp.connect({
host: ip,
port: 22,
username: name,
password: pwd
}).then(async () => {
const files = await sftp.list(remotepath, '.');
for (var j = 0; j < files.length; j++) {
var e =files[j];
await sftp.fastGet(remotepath + "/" + e.name, localpath + "\\" + e.name);
}
});
函数getConnect(ip、名称、pwd、远程路径、本地路径){
const sftp=新的SftpClient();
连接({
主持人:ip,,
港口:22,
用户名:name,
密码:pwd
})。然后(异步()=>{
const files=wait sftp.list(远程路径“.”);
对于(var j=0;jfunction getConnect(ip, name, pwd, remotepath, localpath) {
const sftp = new SftpClient();
sftp.connect({
host: ip,
port: 22,
username: name,
password: pwd
}).then(async () => {
const files = await sftp.list(remotepath, '.');
for (var j = 0; j < files.length; j++) {
var e =files[j];
//=================================================
const Throttle = require('throttle');
const progress = require('progress-stream');
const throttleStream = new Throttle(1); // create a "Throttle " instance that reads at 1 bps
const progressStream = progress({
length: e.size,
time: 100, // ms
});
progressStream.on('progress', (progress) => {
process.stdout.write("\r" + " [" +e.name+"] downloaded ["+progress.percentage.toFixed(2)+"%]");
});
const outStream = createWriteStream(localpath);
throttleStream.pipe(progressStream).pipe(outStream);
try {
await sftp.get(remotepath + "/" + e.name, throttleStream, { autoClose: false });
} catch {
console.log('sftp error', e);
} finally {
await sftp.end();
}
}
}
}
函数getConnect(ip、名称、pwd、远程路径、本地路径){
const sftp=新的SftpClient();
连接({
主持人:ip,,
港口:22,
用户名:name,
密码:pwd
})。然后(异步()=>{
const files=wait sftp.list(远程路径“.”);
对于(var j=0;j{
process.stdout.write(“\r”+“[“+e.name+”]下载[“+progress.percentage.toFixed(2)+“%]”);
});
const outStream=createWriteStream(本地路径);
节流蒸汽管道(前进流)。管道(扩流);
试一试{
等待sftp.get(remotepath+“/”+e.name,throttleStream,{autoClose:false});
}抓住{
console.log('sftp error',e);
}最后{
等待sftp.end();
}
}
}
}
我遵循@Abbas Agus Basari的建议,比如:
await sftp.fastGet(secondPath + "/" + e.name, localPath + "\\" + e.name, {
step: step=> {
const percent = Math.floor((step / e.size) * 100);
process.stdout.write("\r" + "【"+e.name+"】downloaded【"+percent+'%】');
}
});
然后像这样跑:
[1]:
我从远程服务器下载了两个文件,但控制台只能100%看到一个文件,另一个文件在59%时停止