-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (43 loc) · 1.61 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
const formatter = require('./dateFormatter');
const {Storage} = require('@google-cloud/storage');
const fs = require('fs');
const uuid = require('uuid');
const projectId = 'cloud-functions-230120';
const storage = new Storage({
projectId: projectId
});
const bucketName = 'gs://atm_events/';
exports.nodeHTTP = function entryHTTP(req, resp) {
console.log(`HTTP Request from: ${req.ip}`);
const fileName = `${uuid.v4()}.json`;
const localFile = `/tmp/${fileName}`;
fs.writeFile(localFile, JSON.stringify(req.body), (err)=>{
if (err) {
console.error(`Something went wrong writing ${localFile}`);
console.error(err);
}
});
console.log(`File to populate: ${localFile}`);
storage.bucket(bucketName).upload(localFile, {
gzip: false,
metadata: {
cacheControl: 'public, max-age=31536000',
},
});
console.log(`Body: ${JSON.stringify(req.body)}`);
const replyString = `${formatter.dateFormatter()}: File Created: ${localFile}`;
resp.status(200).send(replyString);
};
exports.nodePubsub = function entryPubsub(event, callback) {
console.log(`Hello PUBSUB! ${formatter.dateFormatter()}`);
callback();
};
exports.nodeStorage = function entryStorage(event, callback) {
console.log(`Hello STORAGE! ${formatter.dateFormatter()}`);
console.log(`Event ID: ${event.eventId}`);
console.log(`Event Type: ${event.eventType}`);
console.log(`Bucket: ${event.data.bucket}`);
console.log(`File: ${event.data.name}`);
callback();
};
// TODO: read https://cloud.google.com/nodejs/getting-started/using-cloud-storage