javascript - How correctly use fastGet method of ssh2-sftp-client library in Node.js? - Stack Overflow

I am tring to load file from remote SFTP server in Node.js application. I use ssh2-sftp-client library

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
Add a ment  | 

1 Answer 1

Reset to default 3

Here 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');
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745565993a4633397.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信