s3 object metadata lambda function

Viewed 3150

I am new to javascript and I am trying to write a lambda function that would get triggered by PUT event in the bucket, the function would write the file name and some metadata field that's on the s3 object to the dynamodb table. I have most of it worked out but I'm stuck at grabbing the x-amz-meta header info and pass the variable to dynamo.put parameter. Can anyone tell me what I am doing wrong in my code? thanks!

var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
var s3 = new AWS.S3();
//specify the parameters from event to write to specified db table
exports.handler = function(event, context, callback) {
  var srcKey = unescape(event.Records[0].s3.object.key);
  var srcEtag = unescape(event.Records[0].s3.object.eTag);
  var scrUploadTime = unescape(event.Records[0].eventTime);
  var bucket= unescape(event.Records[0].s3.bucket.name);
  var checksum =

      s3.headObject(
        {
            Bucket: bucket,
            Key: srcKey
        },
        function(err, data)
        {
            if (err)
            {
                console.log(err);
                context.done('Error', 'Error getting s3 object: ' + err);
            }
            else
            {
                 return console.log(this.httpResponse.headers['x-amz-meta-checksum']);
            }
        });

    var params = {  
        Item: {
            filename: srcKey,
            uploadtime: scrUploadTime,
            client_checksum : checksum
        },
        TableName: 'S3_log'
        };

//write to dynammodb
dynamo.put(params, function(err, data){
        if (err) {
            callback(err, null);
        }else{
            callback(null, data);
        }
    });
};
1 Answers
Related