D
D
dmitriyivvv2020-01-11 12:53:03
JavaScript
dmitriyivvv, 2020-01-11 12:53:03

How to get rid of error when downloading multiple files via sftp?

I am trying to download some files from my ftp server. For small files everything works fine, but if the files are "large" (4MB in my case) then the following error occurs:

grabFile was called
connected
get finished
grabFile was called
connected
(node:7112) UnhandledPromiseRejectionWarning: Error: sftp.get: Received more data than requested /test/file2.csv
    at Object.formatError (/home/zendor/untitled_1/node_modules/ssh2-sftp-client/src/utils.js:57:18)
    at ReadStream.<anonymous> (/home/zendor/untitled_1/node_modules/ssh2-sftp-client/src/index.js:310:20)
    at ReadStream.emit (events.js:209:13)
    at /home/zendor/untitled_1/node_modules/ssh2-streams/lib/sftp.js:2843:12
    at cb (/home/zendor/untitled_1/node_modules/ssh2-streams/lib/sftp.js:901:16)
    at SFTPStream._transform (/home/zendor/untitled_1/node_modules/ssh2-streams/lib/sftp.js:444:17)
    at SFTPStream.Transform._read (_stream_transform.js:189:10)
    at SFTPStream._read (/home/zendor/untitled_1/node_modules/ssh2-streams/lib/sftp.js:183:15)
    at SFTPStream.Transform._write (_stream_transform.js:177:12)
    at doWrite (_stream_writable.js:428:12)
(node:7112) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7112) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Even in the simplified implementation before this one, the same error still occurs:
import SftpClient from 'ssh2-sftp-client';

const grabFile = (path) => {
  console.log('grabFile was called');
  const client = new SftpClient();
  return client.connect({
    username: 'login',
    password: 'pass',
    host: 'ftp',
  }).then(() => {
    console.log('connected');
    return client.get(path, `${Math.random()}.txt`);
  }).then(res => {
    console.log('get finished');
    return client.end();
  });
};

grabFile('/test/file1.csv').then(_ => grabFile('/test/file2.csv')).then(_ => console.log('done!'));

The first file is successfully created on my computer and downloaded completely. The second one is created but this error occurs at ~5 percent. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karpion, 2020-01-11
@Karpion

I did not understand anything. It feels like you need to re-initialize something for each file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question