首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ssh2-sftp-client中使用npm cli-progress

如何在ssh2-sftp-client中使用npm cli-progress
EN

Stack Overflow用户
提问于 2021-04-01 20:12:26
回答 2查看 331关注 0票数 0

我有一个用npm ssh2-sftp-client从远程服务器下载文件的项目,所以我想在console.Downloading文件中显示下载进度,但我不知道如何在文件下载时使用cli-progress来显示下载进度。

代码语言:javascript
复制
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);
   }
  }); 
EN

回答 2

Stack Overflow用户

发布于 2021-04-01 21:02:17

我已经修改了,希望它会更好。

代码语言:javascript
复制
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];
            //=================================================
            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();
            }
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-04-02 08:28:19

我遵循了@Abbas Agus Basari的建议,如下:

代码语言:javascript
复制
 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:https://i.stack.imgur.com/97sRi.png我从远程服务器下载了两个文件,但控制台只能看到一个文件100%,另一个停止在59%

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66904283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档