I m trying to read files from SFTP location using the following class:
SFTPClient.js:
let Client = require('ssh2-sftp-client');
class SFTPClient {
constructor() {
this.client = new Client();
}
async connect(options) {
console.log(`Connecting to ${options.host}:${options.port}`);
try {
await this.client.connect(options);
} catch (err) {
console.log('Failed to connect:', err);
}
}
async disconnect() {
await this.client.end();
}
async listFiles(remoteDir, fileGlob, res) {
console.log(`Listing ${remoteDir} ...`);
let fileObjects;
try {
fileObjects = await this.client.list(remoteDir, fileGlob);
} catch (err) {
console.log('Listing failed:', err);
res.write('Listing failed:'+ err);
}
return fileObjects;
}
Connection to public demo SFTP is initiated from the following code: Helper.js:
const SFTPClient =require('./SFTPClient');
async function connectToDemoSFTP(){
const parsedURL = new URL("sftp://test.rebex.net:22");
const port = parsedURL.port;
const { host, username, password } = {"host":"test.rebex.net", "username":"demo", "password":"password"};
//* Open the connection
const client = new SFTPClient();
await client.connect({ host, port, username, password });
return client;
}
module.exports = {connectToDemoSFTP};
index.js:
const { connectToDemoSFTP } = require('./utils/Helper');
let app = new express();
console.log(`Env port is ${process.env.PORT}`);
let port = process.env.PORT || 3000;
app.listen(port);
app.get('/nodejsapi/get/files', async (req, res) => {
{
let client = await connectToDemoSFTP();
client.listFiles("./pub/example", undefined, res);
....
}
When I ran this locally from vscode, I am able to connect to the SFTP using expressjs get call and it lists down the files from the SFTP server (from pub/example folder). It also works fine from trial BTP account as well. I did not do any extra config in my trial account to make it functioning though...
However, when I deploy and test this in a productive BTP account, the connection is not succeeding. It fails at line: let client = await connectToDemoSFTP();
I am wondering how it is working in my trial BTP account and what is wrong here w.r.to productive SAP BTP account!! Can someone please help..
I could see the following log in BTP for the nodejs application:
STDOUT [APP/PROC/WEB/0] Failed to connect: Error: connect: getConnection: Timed out while waiting for handshake [APP/PROC/WEB/0] at SftpClient.fmtError (/home/vcap/app/node_modules/ssh2-sftp-client/src/index.js:111:22)
STDOUT [APP/PROC/WEB/0] at SftpClient.connect (/home/vcap/app/node_modules/ssh2-sftp-client/src/index.js:249:37)
STDOUT [APP/PROC/WEB/0] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
STDOUT [APP/PROC/WEB/0] at async SFTPClient.connect (/home/vcap/app/utils/SFTPClient.js:10:9)
STDOUT [APP/PROC/WEB/0] at async connectToDemoSFTP (/home/vcap/app/utils/Helper.js:8:5)
STDOUT [APP/PROC/WEB/0] at async /home/vcap/app/index.js:64:22 {
STDOUT [APP/PROC/WEB/0] code: 'ERR_GENERIC_CLIENT',
STDOUT [APP/PROC/WEB/0] custom: true
STDOUT [APP/PROC/WEB/0] }
STDOUT [APP/PROC/WEB/0] Listing ./pub/example ...
STDOUT [APP/PROC/WEB/0] Connecting to test.rebex.net:22
Regards,
Faddy..