Skip to content

Instantly share code, notes, and snippets.

@JoshHighland
Forked from cristianmedeiros/index.js
Created May 6, 2017 00:03
Show Gist options
  • Select an option

  • Save JoshHighland/0e458c2cce3ea803f37d8fc7dfca02b7 to your computer and use it in GitHub Desktop.

Select an option

Save JoshHighland/0e458c2cce3ea803f37d8fc7dfca02b7 to your computer and use it in GitHub Desktop.
CSV to SQS throught AWS Lambda
var aws = require('aws-sdk');
aws.config.update({
region: 'us-east-1'
});
var s3 = new aws.S3({apiVersion: '2006-03-01'});
var sqs = new aws.SQS();
var Converter = require('csvtojson').core.Converter;
exports.handler = function(event, context) {
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
s3.getObject({Bucket: bucket, Key: key}, function(err, data) {
if (err) {
console.log("Error getting object " + key + " from bucket " + bucket +
". Make sure they exist and your bucket is in the same region as this function.");
context.fail ("Error getting file: " + err)
} else {
console.log('CONTENT TYPE:', data.ContentType);
//content from CSV
var content = String(data.Body);
var csvConverter = new Converter({constructResult:true});
//end_parsed will be emitted once parsing finished
csvConverter.on("end_parsed", function(jsonObj) {
//final result poped here as normal.
});
csvConverter.fromString(content,function(err,jsonObj){
if (err){
//err handle
console.log(err);
}
for(var i = 0; i < jsonObj.length; i++){
var params = {
MessageBody: JSON.stringify(jsonObj[i]),
QueueUrl: 'https://QUEUE_URL/QUEUE_NAME',
DelaySeconds: 0
};
sqs.sendMessage(params, function (err, data) {
if (err) {
console.log(err, err.stack);
};
});
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment