Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports.processFile = (event, context, callback) => {
- const csv = require('fast-csv');
- const s3 = new AWS.S3();
- const dynamodb = new AWS.DynamoDB();
- async.waterfall([
- (next) => {
- console.log('Waiting until the uploaded object becomes available',
- '[bucket = ', event.bucketName, ', key = ',
- event.objectKey, ' ]');
- s3.waitFor('objectExists', {
- Bucket: event.bucketName,
- Key: event.objectKey
- }, next);
- },
- (result, next) => {
- console.log('Downloading the CSV file from S3 [bucket = ',
- event.bucketName, ', key = ', event.objectKey, ' ]');
- const csvStream = s3.getObject({
- Bucket: event.bucketName,
- Key: event.objectKey
- }).createReadStream();
- csv.fromStream(csvStream).on('data', (data) => {
- dynamodb.putItem({
- Item: {
- 'sensor_id': {
- 'S': data[0]
- },
- 'timestamp': {
- 'N': data[1]
- },
- 'value': {
- 'N': data[2]
- }
- },
- TableName: "sensor_data"
- });
- });
- next(null);
- },
- ], (err, results) => {
- if (err) {
- console.log('Failed execution');
- return context.fail('Execution failed');
- } else {
- console.log('Successful execution');
- return context.succeed(event);
- }
- });
- };
Add Comment
Please, Sign In to add comment