Advertisement
Guest User

Lambda Function Code

a guest
Apr 26th, 2018
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. const aws = require('aws-sdk');
  3. const DynamoDB = new aws.DynamoDB.DocumentClient({region: 'us-west-2'});
  4. const s3 = new aws.S3({ apiVersion: '2006-03-01' });
  5. var m = require('./methods.js');
  6.  
  7.  
  8. exports.handler = (event, context) => {
  9.   // Get the object from the event and show its content type
  10.     const bucket = event.Records[0].s3.bucket.name;
  11.     const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
  12.     const fileInfo = {
  13.         Bucket: bucket,
  14.         Key: key,
  15.     };
  16.    
  17.     s3.getObject(fileInfo, (err, data) => {
  18.         if (err) {
  19.             console.log(err);
  20.             const message = `Error getting object from bucket. Make sure they exist and your bucket is in the same region as this function.`;
  21.             console.log(message);
  22.         } else {
  23.             var filesize = m.formatBytes(data.ContentLength);
  24.             var params = {
  25.                   Item: {
  26.                    "FileName": {
  27.                      S: key
  28.                     },
  29.                    "FileSize": {
  30.                      S: filesize
  31.                     },
  32.                    "UploadedBy": {
  33.                      S: data.Metadata.uploadedby
  34.                     }
  35.                   },
  36.                   TableName: "FileMetaData"
  37.                  };
  38.             DynamoDB.putItem(params, function(err, data) {
  39.                    if (err) console.log(err, err.stack); // an error occurred
  40.                    else     console.log(data);           // successful response
  41.             });
  42.  
  43.         }
  44.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement