user3323241
user3323241

Reputation: 479

code in node js lambda: get the lastmodified date of the s3 key from event data

I am using the node js code inside the lambda. A function
passes event data when an S3 object created

var record = event.Records[0];
var bucket = record.s3.bucket.name;
var key = record.s3.object.key;

How can i get the last modified date of the s3 key to create a folder and paste the key in it.

If the Last Modified date is: Mon Feb 22 14:46:23 GMT+530 2016,

then folder name must be: Bucketname/2016/02/22/

Upvotes: 1

Views: 6100

Answers (2)

user3913702
user3913702

Reputation: 127

You have to send an HTTP Head Request

var AWS = require('aws-sdk');
var s3 = new AWS.S3({apiVersion: '2006-03-01'})   

var params = {
   Bucket: record.s3.bucket.name,
   Key : record.s3.object.key
};

s3.headObject(params, function (error, response) {
   if(error) {
      context.fail();
   } else {
       var date = response.LastModified; //Last modified date
       context.done(null,date);
   }
});

Upvotes: 7

Michael - sqlbot
Michael - sqlbot

Reputation: 179054

The event structure doesn't actually contain the last-modified value for the object.

You could send an http HEAD request for the object, or -- probably -- use the value of Records[0].eventTime. The documentation isn't thoroughly clear that this will always be the same, saying only that it's "when S3 finished processing the request."

Upvotes: 3

Related Questions