Ad

Stream File Through SFTP Connection To Client

- 1 answer

I'm writing a Node Express server which connects via sftp to a file store. I'm using the ssh2-sftp-client package.

To retrieve files it has a get function with the following signature:

get(srcPath, dst, options)

The dst argument should either be a string or a writable stream, which will be used as the destination for a stream pipe.

I would like to avoid creating a file object at my server and instead transfer the file onto my client to save memory consumption as described in this article. I tried to accomplish this with the following code:

const get = (writeStream) => {
    sftp.connect(config).then(() => {
        return sftp.get('path/to/file.zip', writeStream)
    });
};

app.get('/thefile', (req, res) => {
    get(res); // pass the res writable stream to sftp.get   
});

However, this causes my node server to crash due to a unhandled promise rejection. Is what I am attempting possible? Should I store the file on my server machine first before sending to the client? I've checked the documentation/examples for the sftp package in question, but cannot find an example of what I am looking for.

Ad

Answer

I found the error, and it's a dumb one on my part. I was forgetting to end the sftp connection. When this method was called a second time it was throwing the exception when it tried to connect again. If anyone finds themselves in the same situation remember to end the connection once you're finished with it like this:

const get = (writeStream) => {
    sftp.connect(config).then(() => {
        return sftp.get('path/to/file.zip', writeStream);
    }).then(response => {
      sftp.end();
      resolve(response);
    });

};
Ad
source: stackoverflow.com
Ad