for sftp we are using latest version of ssh2-sftp-client module(5.1.3)
service file(sftpConnectionService) with sftp connection
{Error: get->get: No SFTP connection available}
class SftpConnectionService {
constructor() {
this.sftp = null;
}
async getSftpConnection(randomDigit) {
try {
this.sftp = new Client(`connection-${randomDigit}`);
await this.sftp.connect({
host: sftpconfig.host,
port: sftpconfig.port,
username: sftpconfig.username,
password: sftpconfig.password,
});
return this.sftp;
} catch (exception) {
throw exception;
}
}
}
js file getting the connection and download the file
downloadFile(){
let sftp;
const randomDigit = generateRandomCodeSrvc.generateCode(4);
sftp = await sftpConnectionService.getSftpConnection(randomDigit);
const remoteDir = `month/statement/filename.pdf`
let bufferStream = await sftp.get(remoteDir);
if (sftp) await sftp.end();
}