Guest User

Untitled

a guest
Dec 13th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. module.exports.processFile = (event, context, callback) => {
  2. const csv = require('fast-csv');
  3. const s3 = new AWS.S3();
  4. const dynamodb = new AWS.DynamoDB();
  5.  
  6. async.waterfall([
  7. (next) => {
  8. console.log('Waiting until the uploaded object becomes available',
  9. '[bucket = ', event.bucketName, ', key = ',
  10. event.objectKey, ' ]');
  11. s3.waitFor('objectExists', {
  12. Bucket: event.bucketName,
  13. Key: event.objectKey
  14. }, next);
  15. },
  16. (result, next) => {
  17. console.log('Downloading the CSV file from S3 [bucket = ',
  18. event.bucketName, ', key = ', event.objectKey, ' ]');
  19.  
  20. const csvStream = s3.getObject({
  21. Bucket: event.bucketName,
  22. Key: event.objectKey
  23. }).createReadStream();
  24.  
  25. csv.fromStream(csvStream).on('data', (data) => {
  26. dynamodb.putItem({
  27. Item: {
  28. 'sensor_id': {
  29. 'S': data[0]
  30. },
  31. 'timestamp': {
  32. 'N': data[1]
  33. },
  34. 'value': {
  35. 'N': data[2]
  36. }
  37. },
  38. TableName: "sensor_data"
  39. });
  40. });
  41.  
  42. next(null);
  43. },
  44. ], (err, results) => {
  45. if (err) {
  46. console.log('Failed execution');
  47. return context.fail('Execution failed');
  48. } else {
  49. console.log('Successful execution');
  50. return context.succeed(event);
  51. }
  52. });
  53. };
Add Comment
Please, Sign In to add comment