Coder Social home page Coder Social logo

Comments (3)

mscdex avatar mscdex commented on May 30, 2024

If you want to copy multiple files, don't call sftp.end();. You don't even have to wait for a file copy to finish before starting the next one.

Also: calling ws.end(); in your ReadableStream's 'end' handler is unnecessary since pipe() already takes care of all that.

Here's all you should need if you want to go the stream piping route:

c.sftp(function(err, sftp) {
  if (err) throw err;
  var files = ['.bashrc', 'test.sh',
               'remotefile2', 'localfile2',
               'remotefile3', 'localfile3'],
      left = files.length / 2;
  for (var i = 0; i < files.length; i += 2) {
    var rs = sftp.createReadStream(files[i]),
        ws = fs.createWriteStream(files[i + 1]);
    ws.on('finish', function() {
      if (--left === 0) {
        console.log('SFTP :: All files transferred!');
        c.end();
      }
    });
    rs.on('error', function(err) { console.log('rs: ' + err); });
    rs.pipe(ws);
  }
});

If you have large files, you might benefit from parallel reads. In those cases you might want to use fastGet():

c.sftp(function(err, sftp) {
  if (err) throw err;
  var files = ['.bashrc', 'test.sh',
               'remotefile2', 'localfile2',
               'remotefile3', 'localfile3'],
      left = files.length / 2;
  for (var i = 0; i < files.length; i += 2) {
    sftp.fastGet(files[i], files[i + 1], function(err) {
      if (err) throw err;
      if (--left === 0) {
        console.log('SFTP :: All files transferred!');
        c.end();
      }
    });
  }
});

from ssh2.

bnoon avatar bnoon commented on May 30, 2024

Great. Thanks for the help. I have it working now. Is there any case that you would call sftp.end()?

I will generally need to stream the files serially, but from multiple hosts (in parallel).

from ssh2.

mscdex avatar mscdex commented on May 30, 2024

Usually there is no real reason to end the sftp subsystem, you can always re-use it later.

from ssh2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.