繁体   English   中英

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

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

我正在尝试编写一个程序,该程序将在上载到s3存储桶时读取.csv或.txt文件的内容,并将输出存储为变量,以便代码根据文件包含的内容进行交互。 我尝试使用fs,但是找不到其他可以完成任务的东西。 请让我知道该怎么做或在哪里可以找到答案,或者我的代码有什么问题。 谢谢!

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();
    });


};

如果我有足够的代表,这将是一个“评论” ...

您是否看过这些NPM软件包来替代fs

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM