Reputation: 41
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. I have tried using fs, but I could not find anything else that could do the task. 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: '[email protected]',
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: '[email protected]',
to: '[email protected]',
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();
});
};
Upvotes: 0
Views: 4437
Reputation: 940
If I had enough rep, this would just be a "comment"...
Have you looked at these NPM packages as an alternative to fs
?
https://www.npmjs.com/package/awssum-amazon-s3
https://www.npmjs.com/package/s3
Upvotes: 1