admin管理员组文章数量:1431727
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?
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?
Share Improve this question edited May 20, 2019 at 3:42 Nurzhan Nogerbek asked Jan 26, 2019 at 16:15 Nurzhan NogerbekNurzhan Nogerbek 5,25620 gold badges99 silver badges201 bronze badges 2- 1 Already explained on that link stackoverflow./questions/10046039/… – DeadMan Commented May 16, 2019 at 12:10
- I had to update my localePath to point to a file instead of a directory – Gabe Gates Commented Jul 23, 2021 at 15:37
1 Answer
Reset to default 3Here is my final working code:
router.post('/', (req, res) => {
const fileName = req.body.file_name;
const remotePath = '/put_your_path_here/' + fileName;
const localePath = path.join(process.env.HOME || process.env.USERPROFILE, 'Downloads/' + fileName);
sftp.connect(config.sftpServer, 'once').then(() => {
sftp.fastGet(remotePath, localePath, {}).then(() => {
res.header('Content-type', 'text/csv; charset=windows-1251');
res.sendFile(localePath);
sftp.end();
}).catch((err) => {
sftp.end();
console.log(err, 'fastGet method error');
})
}).catch((err) => {
sftp.end();
console.log(err, 'connect method error');
});
});
本文标签: javascriptHow correctly use fastGet method of ssh2sftpclient library in NodejsStack Overflow
版权声明:本文标题:javascript - How correctly use fastGet method of ssh2-sftp-client library in Node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745565992a2663763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论