forked from jasonpolites/gcf-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (39 loc) · 1.19 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
var gcloud = require('gcloud');
var readline = require('readline');
module.exports = {
wordCount: function(context, data) {
var bucketName = data['bucket'];
var fileName = data['file'];
if (!bucketName) {
context.failure(
'Bucket not provided. Make sure you have a \'bucket\' property in ' +
'your request');
return;
}
if (!fileName) {
context.failure(
'Filename not provided. Make sure you have a \'file\' property in ' +
'your request');
return;
}
// Create a gcs client.
var gcs = gcloud.storage({
// We're using the API from the same project as the Cloud Function.
projectId: process.env.GCP_PROJECT,
});
var bucket = gcs.bucket(bucketName);
var file = bucket.file(fileName);
var count = 0;
// Use the readLine module to read the stream line-by line.
var lineReader = readline.createInterface({
input: file.createReadStream(),
});
lineReader.on('line', function(line) {
count += line.trim().split(/\s+/).length;
});
lineReader.on('close', function() {
context.success('The file ' + fileName + ' has ' + count +
' words');
});
},
};