SFTP upload not working in lambda function in Node.js

534 views Asked by At

I am trying to upload single or multiple files from local folder to sftp folder using ssh2-sftp-client in Node.js.

I am trying to upload files using Promise.all. The code works fine when tested locally, but when I upload the code to the AWS lambda. Messages show files uploaded successfully but when I check the FTP folder I can't see any files uploaded. Here is the code I am using.

exports.handler= async (event)=>{    
 var Files = await Promise.all(FilesLIst.map(async (file) =>UploadToFtp(_customer,dir,file)));
}
function UploadToFtp(_customer,dir,file){
       // let data = fs.createReadStream(dir+'/'+file);
        let remote = _customer.FTPPath + '/' + file;
        let client = new Client;
        const config = {
            host: _customer.FTPHost,
            port:_customer.FTPPort,
            username:_customer.FTPUserName,
            password: _customer.FTPPassword,
          };
        return new Promise((res,rej)=>{
                client.connect(config)
                .then(() => {
                    let data=fs.readFileSync(dir+'/'+file);
                    return client.put(data, remote);
                })
                .then(() => {
                    return client.end();                   
                })//.then(()=>{ res()})
                .catch(err => {
                    console.error(err.message);
                    //rej()
                });
        })
}

What can I try next?

0

There are 0 answers