Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing and Copying Files from SFTP server using aws lambda nodejs

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();
    });
}; 
like image 577
Becca Avatar asked Nov 30 '25 01:11

Becca


1 Answers

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);
    });
}; 
like image 90
Anders Avatar answered Dec 02 '25 19:12

Anders