I'm trying to access sftp server using an aws lambda function but it keeps returning null and I'm not sure why. The host, username and password are correct because I tested the connection using WinSCP. I also tried connecting to other free sftp servers but the response kept returning null. I'm not sure what I'm doing wrong in the script. I did install the npm packages, then zipped the file and uploaded the zip file to aws lambda.
exports.handler = async (event) => {
let Client = require('ssh2-sftp-client');
let Path = '/path';
let sftp = new Client();
sftp.connect({
host: 'host',
port: 22,
username: 'username',
password: 'password'
}).then(() => {
return sftp.list(Path);
}).then(() => {
context.done();
}).catch((err) => {
console.log('Catch Error: ', err);
context.fail();
});
};
It's been 9 months so I hope it is solved but if anyone else needs the solution:
exports.handler = (event) => {
let Client = require('ssh2-sftp-client');
let Path = '/path';
let sftp = new Client();
return sftp.connect({
host: 'host',
port: 22,
username: 'username',
password: 'password'
}).then(() => {
return sftp.list(Path);
}).then((list) => {
// If you want to do something with the list, do it here
// else just return the list from the function above and remove this
return list;
}).catch((err) => {
console.log('Catch Error: ', err);
throw new Error(err);
});
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.