-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
142 lines (127 loc) · 3.39 KB
/
main.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
const https = require('https')
const querystring = require('querystring')
const BUCKET = process.env.BUCKET;
if (BUCKET) {
AWS = require('aws-sdk');
s3 = new AWS.S3();
}
function route(event, context, keyword_handler_map) {
for (var keyword in keyword_handler_map) {
if (event.path.indexOf(`/${keyword}/`) === 0) {
var key = event.path.substring(keyword.length + 2);
return keyword_handler_map[keyword](event, context, key)
}
}
console.log("unknown route, event:", event);
context.fail('unknown route')
}
function setValue(event, context, key) {
var value = (event.queryStringParameters || {}).v || event.body
s3.putObject({
Bucket: BUCKET,
Key: key,
Body: value,
ContentType: 'text/plain'
}, function (err) {
if (err) {
console.log("Error SET object " + key + " from bucket " + BUCKET);
context.fail('fail')
} else {
returnSucceed(context, 'ok')
}
});
}
function getValue(event, context, key) {
s3.getObject({ Bucket: BUCKET, Key: key }, function (err, data) {
if (err) {
context.succeed({
statusCode: 404,
headers: { 'Content-Type': 'text/plain' },
body: ''
})
} else {
returnSucceed(context, data.Body.toString())
}
});
}
function deleteObj(event, context, key) {
s3.deleteObject({ Bucket: BUCKET, Key: key }, function (err, data) {
if (err) {
context.fail('fail')
} else {
returnSucceed(context, 'ok')
}
});
}
function addValue(event, context, key) {
var value = (event.queryStringParameters || {}).v || event.body
var itemKey = key + '/' + (new Date()).getTime()
s3.putObject({
Bucket: BUCKET,
Key: itemKey,
Body: value,
ContentType: 'text/plain'
}, function (err) {
if (err) {
console.log("Error SET object " + key + " from bucket " + BUCKET);
context.fail('fail')
} else {
returnSucceed(context, 'ok')
}
});
}
function getListKeys(event, context, key) {
s3.listObjectsV2({ Bucket: BUCKET, Prefix: key }, function (err, data) {
if (err) {
context.fail(err)
} else {
var keys = data.Contents.map(i => i.Key)
returnSucceed(context, JSON.stringify(keys))
}
})
}
function getList(event, context, key) {
s3.listObjectsV2({ Bucket: BUCKET, Prefix: key }, function (err, data) {
if (err) {
context.fail(err)
} else {
var resultHash = {}
var left = data.Contents.length
if( left === 0 ){
return returnSucceed(context, JSON.stringify([]))
}
data.Contents.forEach(function (obj) {
s3.getObject({ Bucket: BUCKET, Key: obj.Key }, function (err, data) {
left--;
if (!err) {
resultHash[obj.Key] = data.Body.toString()
if (left === 0) {
var result = []
for (var kk in resultHash) {
result.push({ "k": kk, "v": resultHash[kk] })
}
returnSucceed(context, JSON.stringify(result))
}
}
})
})
}
})
}
function returnSucceed(context, body, code = 200) {
context.succeed({
statusCode: code,
headers: { 'Content-Type': 'text/plain' },
body: body
})
}
exports.handler = function (event, context) {
var err, body = route(event, context, {
'SET': setValue,
'GET': getValue,
'DEL': deleteObj,
'ADD': addValue,
'KEYS': getListKeys,
'LIST': getList
})
};