-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-lambda.js
75 lines (64 loc) · 1.77 KB
/
aws-lambda.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
'use strict';
console.log('Loading function');
const aws = require('aws-sdk');
var ses = new aws.SES({
region: 'us-east-1'
});
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
exports.handler = (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
// Get the object from the event and show its content type
const bucket = event.Records[0].s3.bucket.name;
const fileName = event.Records[0].s3.object.key;
console.log("Got filename " + fileName)
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const params = {
Bucket: bucket,
Key: key,
};
// Send turn changed email
var eParams = {
Destination: {
BccAddresses: ["[email protected]"]
},
Message: {
Body: {
Text: {
Data: "Civ turn changed! Go and play! Find the server address from http://mywebsite.com/"
}
},
Subject: {
Data: "Civilization 5 Turn Notification: Game 'My Game'"
}
},
Source: "[email protected]"
};
if (fileName === "turn_changed.txt") {
console.log('===SENDING EMAIL===');
ses.sendEmail(eParams, function (err, data) {
if (err) console.log(err);
else {
console.log("===EMAIL SENT===");
//console.log(data);
console.log("EMAIL CODE END");
context.succeed(event);
}
});
}
// Get file from s3
if (false) {
s3.getObject(params, (err, data) => {
if (err) {
console.log(err);
const message = `Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.`;
console.log(message);
callback(message);
}
else {
//console.log('Data obj:' + JSON.stringify(data, null, 2))
console.log('CONTENT TYPE:', data.ContentType);
callback(null, data.ContentType);
}
});
}
};