forked from Ralphive/b1ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.js
94 lines (77 loc) · 2.64 KB
/
bucket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
functions to manage files and bucket storages
on AWS S3
*/
var fs = require('fs')
var global = require('./global')
var ml = require('./ml')
var uuid = require('node-uuid');
var request = require('request').defaults({ encoding: null });
var user = '';
module.exports = {
//Creates a bucket and stores a file if its required
//It also sned the files to a Rekognition Collection
create :function (s3, user, bucket, files, rek){
return createBucket(s3, user, bucket, files, rek);
},
//Puts file in a given bucket
put : function (s3, bucket, user, files,rek, callback){
putObjects(s3, bucket, user, files,rek, callback);
}
}
function createBucket(s3, user, bucket, files, rek){
//Bucket example b1ml-C99998-12dj91dj-192jd129dj1-2d1jd
bucket = bucket.toLowerCase();
var params = {Bucket: bucket};
s3.createBucket(params, function(err, data) {
if (err){
console.log(err, err.stack); // an error occurred
return;
}
console.log("Bucket '"+bucket+"' successfully created.");
if (files){
putObjects(s3, params.Bucket, user, files,rek)
}
});
}
function putObjects(s3, bucket, user, files,rek,callback){
for(var i in files ) {
if (files[i].url!=""){
getFileData(files[i].url,function(body){
putObject(s3, bucket, user, body,rek, function(data){
if (callback){
return callback(data);
}
})
});
}
}
}
function getFileData(path,callback){
request.get(path, function (err, res, body) {
console.log("Content retrieved from "+ path);
return(callback(body));
})
}
function putObject(s3,bucket, user, body,rek,callback){
//Upload an object to a given S3 bucket
//Add the object to a Rekognition collection if required
//Callback recommended for a single file only (to retrieve fileID)
var params = {Bucket: bucket, Key: user+"-"+uuid.v4(), Body: body};
console.log("Putting file "+params.Key+" to bucket "+bucket);
s3.putObject(params, function(err, data) {
if (err){
console.log(err)
return(callback(err))
}else{
console.log("Successfully uploaded files to " + bucket + "/" + params.Key);
if(rek){
ml.indexFace(rek,bucket,params.Key )
}
params.data = data;
if(callback){
return(callback(params));
}
}
});
}