【问题标题】:FTP in AWS Lambda - Issues Downloading Files (Async/Await)AWS Lambda 中的 FTP - 下载文件的问题(异步/等待)
【发布时间】: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


    【解决方案1】:

    问题是在处理程序中创建异步函数时您会迷失方向。由于example() 是异步的,它返回一个Promise。但是你没有在上面await,所以它的编码方式有点像火了就忘记了。此外,您的 Lambda 会在您的回调被触发之前被终止,因此即使下载它您也看不到它。

    我建议您将回调包装在 Promises 中,这样您就可以轻松地从处理函数中await 处理它们。

    我已经设法让它工作:我使用https://dlptest.com/ftp-test/ 进行测试,所以相应地更改它。此外,请参阅我自己上传了文件。因此,如果您想复制此示例,只需在项目的根目录上创建一个 readme.txt 并上传即可。如果您的 FTP 服务器上已经有这个 readme.txt 文件,只需删除它上传文件的那一行。

    这是一个工作示例:

    const ftp = require("basic-ftp");
    const fs = require("fs");
    const FileNameWithExtension = "readme.txt";
    
    module.exports.hello = async (event) => {
    
      const client = new ftp.Client();
      try {
        await client.access({
          host: 'ftp.dlptest.com',
          user: 'dlpuser@dlptest.com',
          password: 'puTeT3Yei1IJ4UYT7q0r'
        });
        console.log(await client.list());
        await client.upload(fs.createReadStream(FileNameWithExtension), FileNameWithExtension)
        await client.download(fs.createWriteStream('/tmp/' + FileNameWithExtension), FileNameWithExtension);
      }
      catch (err) {
        console.log('logging err')
        console.log(err);
      }
      client.close();
    
      console.log(await readdir('/tmp/'))
    
      console.log(await readfile('/tmp/', FileNameWithExtension))
    
      return {
        statusCode: 200,
        body: JSON.stringify({message: 'File downloaded successfully'})
      }
    
    };
    
    const readdir = dir => {
      return new Promise((res, rej) => {
        fs.readdir(dir, function (err, data) {
          if (err) {
            return rej(err);
          }
          return res(data)
        });
      })
    }
    
    const readfile = (dir, filename) => {
      return new Promise((res, rej) => {
        fs.readFile(dir + filename, 'utf8', function (err, data) {
          if (err) {
            return rej(err);
          }
          return res(data)
        })
      })
    }
    

    这是 Lambda 函数的输出:

    以下是完整的 CloudWatch 日志:

    我的文件中只包含一个“你好”。你可以在日志上看到它。

    请记住,在 Lambda 函数中,将任何内容下载到 /tmp 时有 512MB 的限制。你可以在docs看到限制

    【讨论】:

    • 谢谢,格雷西。很高兴我能帮上忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 2018-12-09
    • 2015-10-16
    • 1970-01-01
    • 2014-12-30
    • 2018-02-09
    相关资源
    最近更新 更多