![](/img/trans.png)
[英]How to download a file from Amazon S3 bucket in node.js synchronously
[英]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();
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.