0

I'm using lambda nodejs to upload file to ftp server.

the source code for upload file:

const ftp = require('basic-ftp');
const fs = require('fs');

class FTPClient {
    constructor(host = 'localhost', port = 21, username = 'anonymous', password = 'guest', secure = false) {
        this.client = new ftp.Client();
        this.settings = {
            host: host,
            port: port,
            user: username,
            password: password,
            secure: secure
        };
    }

    upload(sourcePath, remotePath) {
        let self = this;
        (async () => {
            try {
                let access = await self.client.access(self.settings);
                let upload = await self.client.upload(fs.createReadStream(sourcePath), remotePath);
            } catch(err) {
                console.log(err);
            }
            self.client.close();
        })();
    }

    close() {
        this.client.close();
    }
}

module.exports = FTPClient;

the index.js file:

const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const ftp = require('./upload');
const request = require('request');

let doRequest = async (url) => {
    return new Promise(function (resolve, reject) {
        request(url, function (error, res, body) {
            if (!error && res.statusCode == 200) {
                resolve(body);
            } else {
                reject(error);
            }
        });
    });
}


let createFile = async (data) => {
    let fileName = 'fileName.csv';

    const csvWriter = createCsvWriter({
        path: '/tmp/fileName.csv',
        header: [
            { id: 'date', title: 'Date' },
            { id: 'mac_address', title: 'MAC_Address' },
            { id: 'enter', title: 'Enter' }
        ]
    });

    await csvWriter.writeRecords(data);

    const client = new ftp('ftphost', 21, 'ftp_username', 'ftp_password');

    await client.upload('/tmp/fileName.csv', fileName);
}

exports.handler = async (event, s3FileStreamContent) => {

    let dataExcel = [];
    let url = 'http://example.com/apiUrl';//only for example

    let res = await doRequest(url);
    let data = JSON.parse(res).data;


    dataExcel.push({
        date: data.date,
        mac_address: data.mac,
        enter: data.enter
    });

    createFile(data);
};

The log after run:

Response:

null

Request ID:

"9c8e1701-ad54-42eb-8dc6-bbed77bc9b41"

Function Logs:

START RequestId: 9c8e1701-ad54-42eb-8dc6-bbed77bc9b41 Version: $LATEST

END RequestId: 9c8e1701-ad54-42eb-8dc6-bbed77bc9b41

REPORT RequestId: 9c8e1701-ad54-42eb-8dc6-bbed77bc9b41 Duration: 517.98 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 93 MB

It can not upload file to ftp server. Please help me. Thank you.

2
  • Please share the logs, esp. those with errors. Also just to be sure: Are you providing different parameters to the FTPClient constructor on lambda or is that as well trying to access localhost? Commented Mar 11, 2020 at 9:43
  • @michaelbahr Thank you for response. I've updated the question.
    – Ki Ko
    Commented Mar 11, 2020 at 16:33

2 Answers 2

0

Please provide the error u come across. Posibilities as below:

const handler = async (event) => {
   }
  module.exports = { handler }

.AWS lambda starts with aws handler function , hope you have included it in your aws lambda code .how u r triggering the code. hope you provide value to event test configuration.

1
  • Manu Vargese it's only upload module file. My lambda function is run without any error. It can create csv file under 'tmp' folder but it can not upload.
    – Ki Ko
    Commented Mar 11, 2020 at 6:57
0

In short, FTP will not work with lambda since they use ephemeral ports.

Better to use SFTP or S3 will work nicely with lambda.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.