This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (45 loc) · 1.52 KB
/
index.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
let s3Api = require('./s3Api.js');
let snsApi = require('./snsApi.js');
const TOPIC_ARN = process.env['TOPIC_ARN'];
const MANDATORY_META_DATA = (process.env['MANDATORY_META_DATA'] || '').split(',').map(s => s.trim());
exports.handler = async (event) => {
let {
Records: [{
s3: {
bucket: {name: bucket},
object: {key}
}
}]
} = event;
console.log(`Incoming file: ${bucket}/${key}`);
let base = [{bucket}, {key}];
let {
Metadata = {}
} = await s3Api.headObject({
Bucket: bucket,
Key: key
});
console.log(`Collected Metadata: ${JSON.stringify(Metadata)}`);
let allMeta = [...base,
...Object.entries(Metadata).map(e => Object.assign({[e[0]]: e[1]}))
];
let missingMandatoryFields = MANDATORY_META_DATA.filter(m => !allMeta.map(e=>Object.keys(e)[0]).includes(m));
if(missingMandatoryFields.length>0){
throw `missing mandatory fields: ${missingMandatoryFields}`;
}
let result = allMeta.reduce((acc, cur) => {
let [key, StringValue] = Object.entries(cur)[0];
return Object.assign(acc, {
[key]: {
DataType: 'String',
StringValue
}
});
}, {});
console.log(`Send: ${JSON.stringify(result)} to ${TOPIC_ARN}`);
return snsApi.publish({
Message: 'placeholder',
MessageAttributes: result,
TopicArn: TOPIC_ARN
});
};