Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Google Functions GCS sample #620

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions functions/gcs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,16 @@ function getFileStream (file) {
* Reads file and responds with the number of words in the file.
*
* @example
* gcloud alpha functions call wordCount --data '{"bucket":"YOUR_BUCKET_NAME","name":"sample.txt"}'
* gcloud functions call wordCount --data '{"bucket":"YOUR_BUCKET_NAME","name":"sample.txt"}'
*
* @param {object} event The Cloud Functions event.
* @param {object} event.data A Google Cloud Storage File object.
* @param {string} event.data.bucket Name of a Cloud Storage bucket.
* @param {string} event.data.name Name of a file in the Cloud Storage bucket.
* @param {function} callback The callback function.
* @param {Object} req Cloud Function request context.
* @param {object} req.body A Google Cloud Storage File object.
* @param {string} req.body.bucket Name of a Cloud Storage bucket.
* @param {string} req.body.name Name of a file in the Cloud Storage bucket.
* @param {Object} res Cloud Function response context.
*/
exports.wordCount = (event, callback) => {
const file = event.data;

if (file.resourceState === 'not_exists') {
// This is a file deletion event, so skip it
callback();
return;
}
exports.wordCount = (req, res) => {
const file = req.body;

let count = 0;
const options = {
Expand All @@ -67,7 +61,7 @@ exports.wordCount = (event, callback) => {
count += line.trim().split(/\s+/).length;
})
.on('close', () => {
callback(null, `File ${file.name} has ${count} words`);
res.send(`File ${file.name} has ${count} words`);
});
};
// [END functions_word_count_read]
14 changes: 5 additions & 9 deletions functions/gcs/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test.serial(`Fails without a bucket`, (t) => {
const expectedMsg = `Bucket not provided. Make sure you have a "bucket" property in your request`;

t.throws(
() => getSample().program.wordCount({ data: { name: `file` } }),
() => getSample().program.wordCount({ name: `file` }),
Error,
expectedMsg
);
Expand All @@ -63,17 +63,15 @@ test.serial(`Fails without a file`, (t) => {
const expectedMsg = `Filename not provided. Make sure you have a "file" property in your request`;

t.throws(
() => getSample().program.wordCount({ data: { bucket: `bucket` } }),
() => getSample().program.wordCount({ bucket: `bucket` }),
Error,
expectedMsg
);
});

test.cb.serial(`Does nothing for deleted files`, (t) => {
const event = {
data: {
resourceState: `not_exists`
}
resourceState: `not_exists`
};
const sample = getSample();

Expand All @@ -89,10 +87,8 @@ test.cb.serial(`Does nothing for deleted files`, (t) => {
test.cb.serial(`Reads the file line by line`, (t) => {
const expectedMsg = `File ${filename} has 114 words`;
const event = {
data: {
bucket: `bucket`,
name: `sample.txt`
}
bucket: `bucket`,
name: `sample.txt`
};

const sample = getSample();
Expand Down