I am tring to load file from remote SFTP server in Node.js application. I use ssh2-sftp-client library for this task. Unfortunatly I have error. What I did wrong?
When I use such code it return me list of all files in the path of remote SFTP server correctly.
router.get('/', (req, res) => {
sftp.connect(config.sftpServer).then(() => {
return sftp.list('/reports')
}).then((data) => {
console.log(data)
}).catch((error) => {
console.log(error)
})
})
In console I see such result:
[ { type: '-',
name: '1548244803285.csv',
size: 74589,
modifyTime: 1548278757000,
accessTime: 1548513471000,
rights: { user: 'rwx', group: 'rwx', other: 'rwx' },
owner: 1030,
group: 1022 } ]
If I try to download the specific file with fastGet
method, Node.js raise error.
return sftp.fastGet('/reports/1548244803285.csv', path.join(process.env.HOME || process.env.USERPROFILE, 'downloads/'))
ERROR:
Error: Failed to get /reports/NNogerbek1548244803285.csv: EISDIR: illegal operation on a directory, open 'C:\Users\NNogerbek\downloads'
at C:\Users\NNogerbek\WebstormProject\web-app\node_modules\ssh2-sftp-client\src\index.js:192:16
at cbfinal (C:\Users\NNogerbek\WebstormProject\web-app\node_modules\ssh2-streams\lib\sftp.js:1038:11)
at SFTPStream._transform (C:\Users\NNogerbek\WebstormProject\web-app\node_modules\ssh2-streams\lib\sftp.js:389:17)
at SFTPStream.Transform._read (_stream_transform.js:190:10)
at SFTPStream._read (C:\Users\NNogerbek\WebstormProject\web-app\node_modules\ssh2-streams\lib\sftp.js:183:15)
at SFTPStream.Transform._write (_stream_transform.js:178:12)
at doWrite (_stream_writable.js:410:12)
at writeOrBuffer (_stream_writable.js:394:5)
at SFTPStream.Writable.write (_stream_writable.js:294:11)
at Channel.ondata (_stream_readable.js:666:20)
The user which I use to connection to remore SFTP server has rights to read, write. Don't understand whats the problem.
Well, finally I found the reason of the problem. EISDIR
means that application is trying to do something to a file but it is a directory.
The correct code is that:
return sftp.fastGet('/reports/1548244803285.csv', path.join(process.env.HOME || process.env.USERPROFILE, 'downloads/1548244803285.csv'))
Now I want to return file in browser as response. How to make it correctly?