-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
72 lines (67 loc) · 2.27 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
const { getProcessedImages, getImageSigned, jpegifyImage, uploadImage } = require('./src/processImage')
module.exports.processImages = async (event, context, callback) => {
for(const record of event.Records) {
console.log(record)
console.log('Processing ', record.s3.object.key)
try {
const res = await jpegifyImage(record.s3.object.key)
callback(null, { res, msg: 'done' })
} catch (err) {
callback(err, null)
}
}
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};
module.exports.getProcessedImages = async (event, context, callback) => {
try {
const imageList = await getProcessedImages()
callback(null, {
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
statusCode: 200,
body: JSON.stringify(imageList)
})
} catch (err) {
callback(err, null)
}
}
module.exports.getImageSigned = async (event, context, callback) => {
try {
const url = await getImageSigned(event.pathParameters.name)
console.log('url ', url)
const response = {
body: null,
statusCode: 302,
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true, // Required for cookies, authorization headers with HTTPS
Location: url
}
}
callback(null, response)
} catch (err) {
callback(err, null)
}
}
module.exports.uploadImage = async (event, context, callback) => {
try {
const body = JSON.parse(event.body)
console.log('name ', body.name)
console.log('event ', body)
const res = await uploadImage(body.name, body.image, body.type)
const response = {
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify({ res }),
statusCode: 200
}
callback(null, response)
} catch (err) {
callback(err, null)
}
}