-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
157 lines (144 loc) · 3.85 KB
/
handler.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict'
const _ = require('lodash')
const axios = require('axios')
const { v4: uuidv4, validate: isUuid } = require('uuid')
const AWS = require('aws-sdk')
const s3 = new AWS.S3({
signatureVersion: 'v4'
})
const rekognizer = new AWS.Rekognition()
const blobsBucket = process.env.BUCKET_NAME
const { createBlob, updateBlob, readBlob } = require('./crud')
module.exports.getUploadUrl = async (event) => {
try {
let requestBody
if (!_.isEmpty(event.body)) {
requestBody = JSON.parse(event.body)
}
const callbackUrl = requestBody?.callback_url
const blobId = uuidv4()
const blob = await createBlob(blobId, callbackUrl)
if (!_.isEmpty(blob) && blob.error) {
console.log('Error creating blob ', blob.error)
return {
statusCode: blob.error.statusCode,
body: JSON.stringify({
success: false,
message: 'Error creating blob'
})
}
}
// generate signed url
const url = await s3.getSignedUrlPromise('putObject', {
Bucket: blobsBucket,
Key: blobId,
Expires: 3600 // One hour
})
return {
statusCode: 200,
body: JSON.stringify({
success: true,
message: 'Upload url generated successfully.',
url,
blob_id: blobId
})
}
} catch (err) {
console.log('Error generating upload url ', err)
return {
statusCode: err.statusCode || 502,
body: JSON.stringify({
success: false,
message: 'Error generating upload url'
})
}
}
}
module.exports.detectImageLabels = async (event) => {
try {
const lastUploadedImage = event.Records[0].s3.object.key
const params = {
Image: {
S3Object: {
Bucket: blobsBucket,
Name: lastUploadedImage
}
},
MaxLabels: 10
}
const response = await rekognizer.detectLabels(params).promise()
const labels = []
response.Labels.forEach(label => {
labels.push(_.pick(label, ['Name', 'Confidence']))
})
// Save labels to dynamoDB
await updateBlob(lastUploadedImage, labels)
} catch (err) {
console.log('Error saving labels ', err)
}
}
module.exports.getBlobInfoById = async (event) => {
try {
const blobId = event.pathParameters.id
if (_.isNil(blobId) || !isUuid(blobId)) {
return {
statusCode: 400,
body: JSON.stringify({
success: false,
message: 'Invalid blob id.'
})
}
}
const blobInfo = await readBlob(blobId)
if (!_.isEmpty(blobInfo)) {
return {
statusCode: 200,
body: JSON.stringify({
success: true,
message: 'Image labels retrieved successfully.',
labels: blobInfo.Item.labels
})
}
} else {
return {
statusCode: 404,
body: JSON.stringify({
success: false,
message: 'There are no labels for this blob'
})
}
}
} catch (err) {
console.log('Error getting blob labels', err)
return {
statusCode: err.statusCode || 502,
body: JSON.stringify({
success: false,
message: 'Error getting blob labels'
})
}
}
}
module.exports.sendBlobInfo = async (event) => {
const records = event.Records
for (const record of records) {
if (record.eventName === 'MODIFY') {
const callbackUrl = record.dynamodb.NewImage.callback_url.S
// check if there is a callback url
if (!_.isNil(callbackUrl)) {
const blobId = record.dynamodb.NewImage.blobId.S
const labels = record.dynamodb.NewImage.labels.L
const message = JSON.stringify({
blob_id: blobId,
labels: labels
})
// send labels to the callback url
await axios.post(callbackUrl,
message,
{
headers: { 'content-type': 'application/json' }
})
}
}
}
}