[英]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.