TrainingByPackt / Serverless-Architectures-with-AWS

Discover how you can migrate from traditional deployments to serverless architectures with AWS
MIT License
12 stars 10 forks source link

Where is lesson 3? #2

Open ryanslabxyz opened 5 years ago

ryanslabxyz commented 5 years ago

Where is the code for the image-analiser-lambda?

migueleiva commented 5 years ago

The code of lesson 3 has some small errors in the names of the variables.

var AWS = require('aws-sdk');

var rek = new AWS.Rekognition();

var sns = new AWS.SNS();

AWS.config.update({region: 'us-east-1'});

exports.handler = (event, context, callback) => {

console.log('Hello, this is nodejs!');

// Get the object from the event

var bucket = event['Records'][0]['s3']['bucket']['name'];

var imageName = event['Records'][0]['s3']['object']['key'];

detectLabels(bucket, imageName)

.then(function(response){

var params = {

Message: JSON.stringify(response['Labels']), / required /

Subject: imageName,

TopicArn: 'arn:aws:sns:us-east-1:829788106713:extract-image-label-sns'

};

sns.publish(params, function(err, data) {

if (err) console.log(err, err.stack); // an error occurred

else console.log(data); // successful response

});

});

callback(null, 'Hello from Lambda');

};

function detectLabels(bucket, key) {

let params = { Image: { S3Object: { Bucket: bucket, Name: key } } }; return rek.detectLabels(params).promise(); }

ryanslabxyz commented 5 years ago

The code of lesson 3 has some small errors in the names of the variables.

var AWS = require('aws-sdk');

var rek = new AWS.Rekognition();

var sns = new AWS.SNS();

AWS.config.update({region: 'us-east-1'});

exports.handler = (event, context, callback) => {

console.log('Hello, this is nodejs!');

// Get the object from the event

var bucket = event['Records'][0]['s3']['bucket']['name'];

var imageName = event['Records'][0]['s3']['object']['key'];

detectLabels(bucket, imageName)

.then(function(response){

var params = {

Message: JSON.stringify(response['Labels']), / required /

Subject: imageName,

TopicArn: 'arn:aws:sns:us-east-1:829788106713:extract-image-label-sns'

};

sns.publish(params, function(err, data) {

if (err) console.log(err, err.stack); // an error occurred

else console.log(data); // successful response

});

});

callback(null, 'Hello from Lambda');

};

function detectLabels(bucket, key) {

let params = { Image: { S3Object: { Bucket: bucket, Name: key } } }; return rek.detectLabels(params).promise(); }

Does this code work?

migueleiva commented 5 years ago

Yes image