S3 bucket module code

In this module, we will take the image stored in the local storage of Raspberry Pi and upload it to the S3 bucket. On successful execution, it then calls the IndexFaces or SearchFacesByImage function of the IndexFaces.js and SearchFacesByImage.js modules respectively, based on which task needs to be performed. The image is taken using a camera and camera module code, which we will cover in the next section.

Create a file with the name S3put.js and add the following code lines, which will include the required modules and set the configurations right:

var AWS = require('aws-sdk')

fs = require('fs');

indexfaces = require('./indexFaces'),

searchFaces = require('./searchFacesByImage');

AWS.config.update({

accessKeyId: 'YOUR_ACCESS_KEY_ID',

secretAccessKey: ‘YOUR_SECRET_ACCESS_KEY'

});

AWS.config.region = 'ap-southeast-2';

var bucket = 'pibucketmaneesh';

var inputFilePathReferenceImage =
'Path of Reference Image stored on raspberry pi for IndexFaces operation ';

var inputFilePathTargetImage =
'Path of input Image stored on raspberry pi for SeacrchFacesByImage operation ';

var s3 = new AWS.S3();

Now, write a method that will upload the reference image to the S3 bucket and then call IndexFacesMethod of the IndexFaces.js module, which will extract the features of the face in the image and store them in the collection:

module.exports.uploadReferenceImageToS3 = function (callback) {

fs.readFile(inputFilePathReferenceImage, function (err, data) {

if (err) {console.log( err); }

var base64data = new Buffer(data, 'binary');

var params = {Bucket: bucket,

Key: "OrigReferenceS3.jpg",

Body: base64data};

s3.putObject(params,function (err, data) {

if(err){console.log(err.toString())
}
else{

console.log(data);

console.log('Successfully uploaded Image.');

indexfaces.IndexFacesMethod(callback)

}

});

});

}

Now, write a method that will upload the input image to the S3 bucket and then call the searchFaceMethod method of the serachFaceByImage.js module, which will compare the features of the face in the input image with the ones stored in the collection:

module.exports.uploadTarget_to_S3 = function (callback) {
fs.readFile(inputFilePathTargetImage, function (err, data) {

if (err) { throw err; }

var base64data = new Buffer(data, 'binary');

var params = {

Bucket: bucket,

Key: "TargetImageS3.jpg",

Body: base64data

};

s3.putObject(params,function (err, data) {

if(err){

console.log(err.toString())

}
else{

console.log(data);

console.log('Successfully uploaded Image.');

searchFaces.searchFaceMethod(callback);

}

});

});

}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.185.180