简体   繁体   English

使用Node.js读取上传到Amazon s3存储桶的文件的内容

[英]Read the contents of a file uploaded to an amazon s3 bucket with Node.js

I'm trying to write a program that will read the contents of a .csv or .txt file when it is uploaded to an s3 bucket and store the output as a variable so the code and interact with it depending on what the file contains. 我正在尝试编写一个程序,该程序将在上载到s3存储桶时读取.csv或.txt文件的内容,并将输出存储为变量,以便代码根据文件包含的内容进行交互。 I have tried using fs, but I could not find anything else that could do the task. 我尝试使用fs,但是找不到其他可以完成任务的东西。 Please let me know how to do it or where I can find the answer or what is wrong with my code. 请让我知道该怎么做或在哪里可以找到答案,或者我的代码有什么问题。 Thanks! 谢谢!

var sdk = require('aws-sdk');
var nodemailer = require('nodemailer');
var async = require('async');
var fs = require('fs');

exports.handler = function(event, context) {

    var transport = nodemailer.createTransport({
        service: 'Yahoo',
        auth: {
            user: 'pni.robot@yahoo.com',
            pass: 'automatic545Emails'
        }
    });

        var srcKey    =
    decodeURIComponent(event.Records[0].s3.object.key.replace(/\*/g, " "));
    var arr = srcKey.split('.');
    var obj = decodeURIComponent(event.Records[0].s3.object);

    console.log(arr);
    //console.log(event.Records[0]);

    var fileType = arr.pop();
    var fileName = arr.join('.');

    console.log("File detected with name: " + fileName);
    console.log("File detected with extension: " + fileType);

    //The following code does not work
    //It cannot read srcKey because srcKey is just a text value
    //I am just keeping it here until I find a better alternative
    if (fileType === 'csv') {
        console.log('.csv file detected.');
        fs.readFile(srcKey, function(err, data) {
            if (err) {
                return console.error(err);
            }
            var contents = data.toString;
            console.log("Email Robot read: " + data.tostring());
            console.log(contents);
        });
    }; 

    var mailOptions = {
        from: 'pni.robot@yahoo.com',
        to: 'teranai@reed.edu',
        subject: 'Email Program',
        text: 'A .' + fileType + ' file titled "' + fileName + '.' + fileType + '" has been uploaded to the S3 bucket "pnilambdabucket".'
    }

    transport.sendMail(mailOptions, function(error, info){
        if(error){
            return console.log(error);
        }
        console.log('Message Sent: ' + info.response);
        context.done();
    });


};

If I had enough rep, this would just be a "comment"... 如果我有足够的代表,这将是一个“评论” ...

Have you looked at these NPM packages as an alternative to fs ? 您是否看过这些NPM软件包来替代fs

https://www.npmjs.com/package/awssum-amazon-s3 https://www.npmjs.com/package/awssum-amazon-s3

https://www.npmjs.com/package/s3 https://www.npmjs.com/package/s3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Node.js中的Amazon S3存储桶同步下载文件 - How to download a file from Amazon S3 bucket in node.js synchronously 想从 Node.js 中的 S3 Bucket 下载文件 - Want to download file from S3 Bucket in Node.js Node.js上传到Amazon S3工作但文件损坏 - Node.js upload to Amazon S3 works but file corrupt 如何将 Zip 文件从 s3 存储桶下载到 node.js lambda 函数中的本地目录 - How to download Zip File from an s3 bucket to a local directory within a node.js lambda function 如何使用 s3 api 和 node.js 将文件上传到芥末桶? - How to upload file into Wasabi bucket with s3 api with node.js? 如何通过 Node.js 中的预签名 URL 将文件上传到 AWS S3 存储桶 - How to Upload File to AWS S3 Bucket via pre signed URL in Node.js 将远程文件数组流式传输到Node.js中的Amazon S3 - Stream array of remote files to amazon S3 in Node.js 我是否需要使用队列将文件从Node.js服务器上传到Amazon S3? - Do I need to use queue for uploading file to Amazon S3 from my Node.js Server? 使用Node.js和AWS Lambda将S3文件的内容记录到Postgres表中 - Logging contents of S3 file to postgres table with Node.js and AWS Lambda Node.js:获取上传文件的内容以创建读取流 - Node.js : get content of uploaded file for creating read stream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM