How to download multiple files with ssh2-sftp-client fastGet?

curiousgeorge

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.

Dhruv Shah

There seem to be multiple issues in your code:

  1. The loop through files should be executed in the first then block itself.
  2. sftp.fastGet returns a promise, hence its an asynchronous, operation, and executing asynchronous operations inside a forEach loop is not a good idea.

I would recommend updating your code with following changes:

sftp.connect(configs)
    .then(async () => {
        const files = await sftp.list('.');

        for(const file in files){
          if(file.name.match(/txt$/)){
            const remoteFile = // remote file dir path
            const localFile = // local file dir path
            try {
              await sftp.fastGet(remoteFile, localFile)
            }
            catch(err) { 
              console.log(err))
            };
          } 
        }
    })
    .catch(err => console.log(err))
    .finally(() => {
        sftp.end();
    });

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

ssh2-sftp-client fastGet으로 여러 파일을 다운로드하는 방법은 무엇입니까?

분류에서Dev

How to remove multiple files using sftp

분류에서Dev

how to download today's files based on filename using sftp in unix

분류에서Dev

NodeJS ssh2 fastGet이 압축 해제시 중단됨

분류에서Dev

How to download a single large file over sftp or http etc using multiple simultaneous connections

분류에서Dev

How to download part of a file over SFTP connection?

분류에서Dev

How to list files and only files via SFTP?

분류에서Dev

How to configure an SFTP client Filezila to a machine which is accessible by doing SSH second time?

분류에서Dev

curl download multiple files with brace syntax

분류에서Dev

Using Python, how to download multiple files from a subdirectory on FTP server into a desired directory on local machine?

분류에서Dev

PHP SSH2를 통해 로컬에서 SFTP로 파일 업로드

분류에서Dev

How do I allow SFTP with a password BUT not SSH?

분류에서Dev

How to Download files from the internet with powershell

분류에서Dev

Is it possible to add a cipher for an sftp client?

분류에서Dev

How to run hxnormalize on multiple files?

분류에서Dev

How can I allow access to both SSH and SFTP?

분류에서Dev

SFTP remove files with wildcard characters

분류에서Dev

간단한 ssh2_connect는 제공된 리소스가 유효한 SSH2 SFTP 리소스가 아닙니다.

분류에서Dev

Google Drive API:How to download files from google drive?

분류에서Dev

How to Download to XML files Previous Versions of Infopath form SharePoint

분류에서Dev

How to generate text files and download a zip file in Javascript?

분류에서Dev

How do you download files from Github via Batch?

분류에서Dev

SFTP GUI + SSH 키

분류에서Dev

youtube-dl how to download multiple playlists in individual folders

분류에서Dev

download files from heroku

분류에서Dev

Cannot download zip files

분류에서Dev

Download files by script or something

분류에서Dev

Download files by script or something

분류에서Dev

How to diff two folders to multiple patch files

Related 관련 기사

  1. 1

    ssh2-sftp-client fastGet으로 여러 파일을 다운로드하는 방법은 무엇입니까?

  2. 2

    How to remove multiple files using sftp

  3. 3

    how to download today's files based on filename using sftp in unix

  4. 4

    NodeJS ssh2 fastGet이 압축 해제시 중단됨

  5. 5

    How to download a single large file over sftp or http etc using multiple simultaneous connections

  6. 6

    How to download part of a file over SFTP connection?

  7. 7

    How to list files and only files via SFTP?

  8. 8

    How to configure an SFTP client Filezila to a machine which is accessible by doing SSH second time?

  9. 9

    curl download multiple files with brace syntax

  10. 10

    Using Python, how to download multiple files from a subdirectory on FTP server into a desired directory on local machine?

  11. 11

    PHP SSH2를 통해 로컬에서 SFTP로 파일 업로드

  12. 12

    How do I allow SFTP with a password BUT not SSH?

  13. 13

    How to Download files from the internet with powershell

  14. 14

    Is it possible to add a cipher for an sftp client?

  15. 15

    How to run hxnormalize on multiple files?

  16. 16

    How can I allow access to both SSH and SFTP?

  17. 17

    SFTP remove files with wildcard characters

  18. 18

    간단한 ssh2_connect는 제공된 리소스가 유효한 SSH2 SFTP 리소스가 아닙니다.

  19. 19

    Google Drive API:How to download files from google drive?

  20. 20

    How to Download to XML files Previous Versions of Infopath form SharePoint

  21. 21

    How to generate text files and download a zip file in Javascript?

  22. 22

    How do you download files from Github via Batch?

  23. 23

    SFTP GUI + SSH 키

  24. 24

    youtube-dl how to download multiple playlists in individual folders

  25. 25

    download files from heroku

  26. 26

    Cannot download zip files

  27. 27

    Download files by script or something

  28. 28

    Download files by script or something

  29. 29

    How to diff two folders to multiple patch files

뜨겁다태그

보관