I manually uploaded an image to my aws account in an S3 bucket. Now I need inside a lambda to retrieve this image with the getObject method and extract the metadata from that image. I'm not getting it because what returns from S3 is an object and not an image. Can someone help me ?
'use strict';
const AWS = require ('aws-sdk'); // eslint-disable-line import / no-extraneous-dependencies
const dynamoDb = new AWS.DynamoDB.DocumentClient ();
var metadata = require ('im-metadata');
const s3 = new AWS.S3 ();
module.exports.extractMetadata = (event, context, callback) = > {
console.log (event); console.log ("S3", event.Records [0] .s3);
const bucket = event.Records [0] .s3.bucket.name; console.log ("bucket", bucket); const key = event.Records [0] .s3.object.key;
console.log ("key", key);
const params = {Bucket: bucket, Key: key};
var urlParams = {Bucket: bucket, Key: key}; s3.getSignedUrl ('getObject', urlParams, function (err, url) { console.log ('the url of the image is', url);
metadata (url, {exif: true}, function (error, metadata) { if (error) {console.error (error); } console.log (metadata); console.log (metadata.exif); }); })
s3.getObject (params, function (err, data) { if (err) console.log ("Error", err, err.stack); // an error occurred else console.log ("DDDD", date) var image = new Buffer (data.Body.toString (), 'binary'). toString ('base64'); console.log ("Image", image) dynamoPersist (data, key)
}); };
function dynamoPersist (data, key) { const timestamp = new Date (). getTime ();
console.log ("MErDa", data.LastModified); var params = { Key: { s3objectkey: key }, TableName: "serverless-challenge-dev", UpdateExpression: "set Size =: s, ContentType =: ct, LastModified =: lm", ExpressionAttributeValues: { ": s": data.ContentLength, ": ct": data.ContentType, ": lm": timestamp
}, ReturnValues: "UPDATED_NEW" };
dynamoDb.update (params, function (err, data) { if (err) console.log (err); else console.log ("Ok"); }); }
module.exports.getMetadata = (event, context, callback) = > { };