2

我正在尝试使用 aws lambda 函数访问 sftp 服务器,但它一直返回 null,我不知道为什么。主机、用户名和密码是正确的,因为我使用 WinSCP 测试了连接。我也尝试连接到其他免费的 sftp 服务器,但响应一直返回 null。我不确定我在脚本中做错了什么。我确实安装了 npm 包,然后压缩文件并将压缩文件上传到 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();
    });
}; 
4

1 回答 1

2

已经 9 个月了,所以我希望它得到解决,但如果其他人需要解决方案:

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);
    });
}; 
于 2020-04-17T07:19:02.373 回答