最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

通过FTP下载文件,使用AWS Lambda将 tmp 和.txt内容写入控制台

运维笔记admin13浏览0评论

通过FTP下载文件,使用AWS Lambda将/ tmp /和.txt内容写入控制台

通过FTP下载文件,使用AWS Lambda将/ tmp /和.txt内容写入控制台

我只使用一个Node包,basic-ftp尝试下载TXT文件并将内容写入控制台。我将继续编辑文本,因此需要使用fs。只是努力使用来自FTP程序的createWriteStream的输出。

任何人都可以帮我写一个TXT文件到AWS Lambda中的/ tmp /文件,然后使用正确的语法在createWriteStream被使用后打开并编辑文件吗?

var fs = require('fs');
const ftp = require("basic-ftp")
var path = require('path');

exports.handler = (event, context, callback) => {

var fullPath = "/home/example/public_html/_uploads/15_1_5c653e6f6780f.txt"; //  File Name FULL PATH -------
const extension = path.extname(fullPath); // Used to calculate filenames below
const wooFileName = path.basename(fullPath, extension); // Uploaded filename  with no path or extension eg. filename
const myFileNameWithExtension = path.basename(fullPath); // Uploaded filename  with the file extension eg. filename.txt
const FileNameWithExtension = path.basename(fullPath); // Uploaded filename  with the file extension eg. filename.txt

example()

async function example() {
    const client = new ftp.Client()
    client.ftp.verbose = true
    try {
        await client.access({
            host: "XXXX",
            user: "XXXX",
            password: "XXXX",
            //secure: true
        })
        await client.download(fs.createWriteStream('./tmp/' + myFileNameWithExtension), myFileNameWithExtension)
    }
    catch(err) {
        console.log(err)
    }
    client.close()
}

//Read the content from the /tmp directory to check it's empty
fs.readdir("/tmp/", function (err, data) {
    console.log(data);
    console.log('Contents of AWS Lambda /tmp/ directory');
});

/*
downloadedFile = fs.readFile('./tmp/' + myFileNameWithExtension)
console.log(downloadedFile)
console.log("Raw text:\n" + downloadedFile.Body.toString('ascii'));
*/

}
回答如下:

很确定你的fs.createWriteStream()必须使用Lambdas中/tmp的绝对路径。你的实际工作目录是var/task而不是/

此外,如果你正在使用fs.createWriteStream(),你需要等待finish事件才能从文件中读取。有点像这样...

async function example() {
    var finalData = '';

    const client = new ftp.Client()
    client.ftp.verbose = true
    try {
        await client.access({
            host: "XXXX",
            user: "XXXX",
            password: "XXXX",
            //secure: true
        })

        let writeStream = fs.createWriteStream('/tmp/' + myFileNameWithExtension);
        await client.download(writeStream, myFileNameWithExtension)

      await finalData = (()=>{
       return new Promise((resolve, reject)=> {

        writeStream
          .on('finish', ()=>{
           fs.readFile("/tmp/"+myFileNameWithExtension, function (err, data) {
             if (err) { 
               reject(err) 
             } else {
               console.log('Contents of AWS Lambda /tmp/ directory', data);
               resolve(data);
             }
           });
          })
          .on('error', (err)=> {
             console.log(err);
             reject(err);
          })
        })
       })();
    }
    catch(err) {
        console.log(err)
    }
    client.close();
    return finalData;
}

您还需要使用file访问fs.readFile()。您使用的fs.readdir()为您提供了目录中的文件列表,而不是文件的内容。

如果你想使用readdir(),你可以这样做,但正如你所看到的,在你的情况下它是多余的。为了处理错误,我建议只在最初的error中处理createWriteStream()事件,而不是添加额外的开销(添加到前面的示例中)...

       writeStream
          .on('finish', ()=>{
           fs.readdir('/tmp',(err, files)=> {
               let saved = files.find(file => file === myFileNameWithExtension);
               fs.readFile("/tmp/"+saved, function (err, data) {
                 if (err) throw new Error();
                 console.log(data);
                 console.log('Contents of AWS Lambda /tmp/ directory');
               });
           })
          })
          .on('error', (err)=> {
             console.log(err);
             throw new Error();
          })

注意:请注销saved的结果,我不记得files数组是否是相对路径的绝对值。

发布评论

评论列表(0)

  1. 暂无评论