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



我只使用一个节点包,basic-ftp尝试下载txt文件并将内容写入控制台。在此外,我将编辑文本,因此需要使用fs。只是在FTP程序中努力与createWriteStream的输出一起工作。

任何人都可以帮助我在AWS lambda中的/tmp/file编写一个TXT文件,然后在使用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;
}

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

如果要使用readdir(),则可以这样做,但是如您所见,在您的情况下它是多余的。要处理错误,我建议只在初始createWriteStream()中处理error事件,而不是添加此额外的开销(添加到上一个示例中) ...

       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数组是否是相对路径的绝对。

最新更新