【发布时间】:2019-03-02 12:38:00
【问题描述】:
我一直在努力使用各种 FTP 节点模块来尝试让任何东西在 AWS Lambda 中运行。最好和最受欢迎的似乎是也支持异步/等待的“基本 FTP”。但是当在 FTP 功能下添加任何代码时,我无法让它下载文件。
我不想在 FTP 异步函数中添加 fs 函数,因为我需要解决在添加以下任何代码时导致中断的原因,并且我还需要添加其他代码位并使用下载的文件后面的内容:
FTP 成功 - 仅使用 async 函数且其下没有 fs 代码时
FTP FAILURE - 在下面添加 fs readdir/readFile 函数或任何其他代码
错误 Error: ENOENT: no such file or directory, open '/tmp/document.txt'
https://github.com/patrickjuchli/basic-ftp
const ftp = require("basic-ftp");
const fs = require("fs");
var FileNameWithExtension = "document.txt";
var ftpTXT;
exports.handler = async (event, context, callback) => {
example();
async function example() {
const client = new ftp.Client();
//client.ftp.verbose = true;
try {
await client.access({
host: host,
user: user,
password: password,
//secure: true
});
console.log(await client.list());
await client.download(fs.createWriteStream('/tmp/' + FileNameWithExtension), FileNameWithExtension);
}
catch (err) {
console.log(err);
}
client.close();
}
// Read the content from the /tmp/ directory to check FTP was succesful
fs.readdir("/tmp/", function (err, data) {
if (err) {
return console.error("There was an error listing the /tmp/ contents.");
}
console.log('Contents of AWS Lambda /tmp/ directory: ', data);
});
// Read TXT file and convert into string format
fs.readFile('/tmp/' + FileNameWithExtension, 'utf8', function (err, data) {
if (err) throw err;
ftpTXT = data;
console.log(ftpTXT);
});
// Do other Node.js coding with the downloaded txt file and it's contents
};
【问题讨论】:
标签: javascript node.js amazon-web-services ftp aws-lambda