-
-
Save JoshHighland/0e458c2cce3ea803f37d8fc7dfca02b7 to your computer and use it in GitHub Desktop.
CSV to SQS throught AWS Lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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