diff --git a/.kokoro/functions/gcs.cfg b/.kokoro/functions/gcs.cfg deleted file mode 100644 index ed7fdcc9da..0000000000 --- a/.kokoro/functions/gcs.cfg +++ /dev/null @@ -1,13 +0,0 @@ -# Format: //devtools/kokoro/config/proto/build.proto - -# Set the folder in which the tests are run -env_vars: { - key: "PROJECT" - value: "functions/gcs" -} - -# Tell the trampoline which build file to use. -env_vars: { - key: "TRAMPOLINE_BUILD_FILE" - value: "github/nodejs-docs-samples/.kokoro/build.sh" -} diff --git a/functions/gcs/README.md b/functions/gcs/README.md deleted file mode 100644 index 37459dd57b..0000000000 --- a/functions/gcs/README.md +++ /dev/null @@ -1,47 +0,0 @@ -Google Cloud Platform logo - -# Google Cloud Functions Cloud Storage sample - -This recipe demonstrates how to load a file from Cloud Storage. - -View the [source code][code]. - -[code]: index.js - -## Deploy and Test - -1. Follow the [Cloud Functions quickstart guide][quickstart] to setup Cloud -Functions for your project. - -1. Clone this repository: - - git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git - cd nodejs-docs-samples/functions/gcs - -1. Create a Cloud Storage Bucket: - - gsutil mb gs://YOUR_BUCKET_NAME - - * Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket. - -1. Upload the sample file to the bucket: - - gsutil cp sample.txt gs://YOUR_BUCKET_NAME - - * Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket. - -1. Deploy the "wordCount" function with an HTTP trigger: - - gcloud functions deploy wordCount --trigger-http - -1. Call the "wordCount" function using the sample file: - - gcloud functions call wordCount --data '{"bucket":"YOUR_BUCKET_NAME","file":"sample.txt"}' - - * Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket. - - You should see something like this in your console - - The file sample.txt has 114 words - -[quickstart]: https://cloud.google.com/functions/quickstart diff --git a/functions/gcs/index.js b/functions/gcs/index.js deleted file mode 100644 index 4c855d6563..0000000000 --- a/functions/gcs/index.js +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Copyright 2016, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -// [START functions_word_count_setup] -const Storage = require('@google-cloud/storage'); -const readline = require('readline'); - -// Instantiates a client -const storage = Storage(); -// [END functions_word_count_setup] - -function getFileStream(file) { - if (!file.bucket) { - throw new Error( - 'Bucket not provided. Make sure you have a "bucket" property in your request' - ); - } - if (!file.name) { - throw new Error( - 'Filename not provided. Make sure you have a "name" property in your request' - ); - } - - return storage - .bucket(file.bucket) - .file(file.name) - .createReadStream(); -} - -// [START functions_word_count_read] -/** - * 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"}' - * - * @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. - */ -exports.wordCount = (event, callback) => { - const file = event.data; - - if (file.resourceState === 'not_exists') { - // This is a file deletion event, so skip it - callback(); - return; - } - - let count = 0; - const options = { - input: getFileStream(file), - }; - - // Use the readline module to read the stream line by line. - readline - .createInterface(options) - .on('line', line => { - count += line.trim().split(/\s+/).length; - }) - .on('close', () => { - callback(null, `File ${file.name} has ${count} words`); - }); -}; -// [END functions_word_count_read] diff --git a/functions/gcs/package.json b/functions/gcs/package.json deleted file mode 100644 index 7827205e79..0000000000 --- a/functions/gcs/package.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "name": "nodejs-docs-samples-functions-cloud-storage", - "version": "0.0.1", - "private": true, - "license": "Apache-2.0", - "author": "Google Inc.", - "repository": { - "type": "git", - "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" - }, - "engines": { - "node": ">=8" - }, - "scripts": { - "test": "ava -T 20s --verbose test/*.test.js" - }, - "dependencies": { - "@google-cloud/storage": "1.7.0", - "request": "2.88.0" - }, - "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0", - "ava": "0.25.0", - "proxyquire": "2.1.0", - "sinon": "4.4.2" - }, - "cloud-repo-tools": { - "requiresKeyFile": true, - "requiresProjectId": true - } -} diff --git a/functions/gcs/sample.txt b/functions/gcs/sample.txt deleted file mode 100644 index fa128797ee..0000000000 --- a/functions/gcs/sample.txt +++ /dev/null @@ -1,14 +0,0 @@ -Shall I compare thee to a summer's day? -Thou art more lovely and more temperate: -Rough winds do shake the darling buds of May, -And summer's lease hath all too short a date: -Sometime too hot the eye of heaven shines, -And often is his gold complexion dimm'd; -And every fair from fair sometime declines, -By chance, or nature's changing course, untrimm'd; -But thy eternal summer shall not fade -Nor lose possession of that fair thou ow'st; -Nor shall Death brag thou wander'st in his shade, -When in eternal lines to time thou grow'st; -So long as men can breathe or eyes can see, -So long lives this, and this gives life to thee. diff --git a/functions/gcs/test/index.test.js b/functions/gcs/test/index.test.js deleted file mode 100644 index 10f3ad6631..0000000000 --- a/functions/gcs/test/index.test.js +++ /dev/null @@ -1,110 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const fs = require(`fs`); -const path = require(`path`); -const proxyquire = require(`proxyquire`).noCallThru(); -const sinon = require(`sinon`); -const test = require(`ava`); - -const filename = `sample.txt`; - -function getSample() { - const filePath = path.join(__dirname, `../${filename}`); - const file = { - createReadStream: () => fs.createReadStream(filePath, {encoding: `utf8`}), - }; - const bucket = { - file: sinon.stub().returns(file), - }; - const storage = { - bucket: sinon.stub().returns(bucket), - }; - const StorageMock = sinon.stub().returns(storage); - - return { - program: proxyquire(`../`, { - '@google-cloud/storage': StorageMock, - }), - mocks: { - Storage: StorageMock, - storage: storage, - bucket: bucket, - file: file, - }, - }; -} - -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`}}), - Error, - expectedMsg - ); -}); - -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`}}), - Error, - expectedMsg - ); -}); - -test.cb.serial(`Does nothing for deleted files`, t => { - const event = { - data: { - resourceState: `not_exists`, - }, - }; - const sample = getSample(); - - sample.program.wordCount(event, (err, message) => { - t.ifError(err); - t.is(message, undefined); - t.deepEqual(sample.mocks.storage.bucket.callCount, 0); - t.deepEqual(sample.mocks.bucket.file.callCount, 0); - t.end(); - }); -}); - -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`, - }, - }; - - const sample = getSample(); - sample.program.wordCount(event, (err, message) => { - t.ifError(err); - t.deepEqual(message, expectedMsg); - t.deepEqual(sample.mocks.storage.bucket.calledOnce, true); - t.deepEqual(sample.mocks.storage.bucket.firstCall.args, [ - event.data.bucket, - ]); - t.deepEqual(sample.mocks.bucket.file.calledOnce, true); - t.deepEqual(sample.mocks.bucket.file.firstCall.args, [event.data.name]); - t.end(); - }); -});