Reputation: 387
my task is (or better, should be) simple: to fetch the content of a directory in SFTP from a Node application, then download all the files in there and finally delete them remotely.
Now, I've checked different modules. Very clean, in my opinion, is ssh2-sftp-client. I can use it like this:
let Client = require('ssh2-sftp-client');
let sftp = new Client();
sftp.connect({
host: myhost,
port: '22',
username: myusername,
password: mypassword
}).then(() => {
return sftp.list('/path/to/my/files');
}).then((data) => {
for(var i = 0 ; i < data.length; i++) {
sftp.get('/path/to/my/files/'+data[i].name);
}
}).catch((err) => {
console.log(err, 'catch error');
});
I can list all files and see the resulting jSon in console, but the .get command seems to do nothing and I have no idea what is wrong. From the documentation (https://www.npmjs.com/package/ssh2-sftp-client) you can get files simply like I'm doing. But:
1) it doesn't seem to work 2) how can I debug what's going on? 3) if I have no chance to choose the local directory, will these files most probably be downloaded where the js is launched by node, right?
So, how could I get the files actually downloaded and possibly deleted remotely if they were successfully downloaded with this module (or others if any)?
Thanks, Fabio
Upvotes: 3
Views: 12826
Reputation: 360
Download and save file using get() method
sftp.get(remote_file_path, local_file_path).then((stream) => {
console.log('File download Success')
});
It requires two param: src and dest file path
Upvotes: 0
Reputation: 155
sftp.get(remoteFileNameWithPath, true).then(stream => {
stream.pipe(fs.createWriteStream(localFilePath)).on('close', () => console.log('completely passed file'))
})
the get method returns a promise with a stream result, so, In order to save this you need to pipe to a writeStream Check docs on nodejs: https://nodejs.org/api/stream.html
Upvotes: 1
Reputation: 1
I'm not sure what happens but when I ran a code like this it only download one file.
for (var i = fileList.length - 1; i >= 0; i--) {
remoteFilename = process.env.FTP_DEFAULT_DIR + '/' + fileList[i].name
localFilename = fileList[i].name
sftp.get(remoteFilename).then((stream) => {
stream.pipe(fs.createWriteStream(localFilename));
console.log(stream);
console.log('processing: ', remoteFilename);
}).catch((err)=> {
console.log(err)
});
}
Upvotes: -1
Reputation: 106698
sftp.get()
returns a Promise that returns a stream, so if you want to save the stream to disk, you will need to pipe it. For example:
var fs = require('fs');
// ...
for(var i = 0; i < data.length; i++) {
const remoteFilename = '/path/to/remote/files/' + data[i].name;
const localFilename = '/path/to/local/files/' + data[i].name;
sftp.get(remoteFilename).then((stream) => {
stream.pipe(fs.createWriteStream(localFilename));
});
}
Also, it appears that ssh2-sftp-client
is not using the latest version branch of the ssh2
module (v0.5.x as of this writing), so keep that in mind if you run into issues.
Upvotes: 4