Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js AWS Lambda nodejs将文件上载到ftp_Node.js_Aws Lambda - Fatal编程技术网

Node.js AWS Lambda nodejs将文件上载到ftp

Node.js AWS Lambda nodejs将文件上载到ftp,node.js,aws-lambda,Node.js,Aws Lambda,我正在使用lambda nodejs将文件上传到ftp服务器 上载文件的源代码: const ftp = require('basic-ftp'); const fs = require('fs'); class FTPClient { constructor(host = 'localhost', port = 21, username = 'anonymous', password = 'guest', secure = false) { this.client =

我正在使用lambda nodejs将文件上传到ftp服务器

上载文件的源代码:

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;
index.js文件:

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);
};
运行后的日志:

答复:

空的

请求ID:

“9c8e1701-ad54-42eb-8dc6-bbed77bc9b41”

功能日志:

启动请求ID:9c8e1701-ad54-42eb-8dc6-bbed77bc9b41版本:$LATEST

结束请求ID:9c8e1701-ad54-42eb-8dc6-bbed77bc9b41

报告请求ID:9c8e1701-ad54-42eb-8dc6-bbed77bc9b41持续时间: 517.98毫秒计费持续时间:600毫秒内存大小:128 MB最大使用内存:93 MB


无法将文件上载到ftp服务器。请帮帮我。谢谢。

请提供您遇到的错误。 可能性如下:

const handler = async (event) => {
   }
  module.exports = { handler }
.AWS lambda以AWS处理程序函数开始,希望您已将其包含在AWS lambda代码中
.你是如何触发代码的。希望您为事件测试配置提供价值。

Manu Vargese这是唯一的上传模块文件。我的lambda函数运行时没有任何错误。它可以在“tmp”文件夹下创建csv文件,但无法上载。请共享日志,尤其是有错误的日志。另外,请确定:您是否为lambda上的FTPClient构造函数提供了不同的参数,还是尝试访问
localhost
?@michaelbahr感谢您的回复。我已经更新了这个问题。