From 36b77ec5552a90701f77a828f931b2ec27387553 Mon Sep 17 00:00:00 2001 From: Jerjou Cheng Date: Fri, 14 Aug 2015 14:58:15 -0700 Subject: [PATCH] Add sample for using cloud storage api. --- .gitignore | 1 + .jshintrc | 7 ++++ .travis.yml | 12 ++++-- README.md | 46 ++--------------------- cloud-storage/authSample.js | 54 +++++++++++++++++++++++++++ nodejs-docs-samples.json.enc | Bin 0 -> 2048 bytes package.json | 22 +++++++++++ test/cloud-storage/testAuthSample.js | 27 ++++++++++++++ 8 files changed, 122 insertions(+), 47 deletions(-) create mode 100644 .gitignore create mode 100644 .jshintrc create mode 100644 cloud-storage/authSample.js create mode 100644 nodejs-docs-samples.json.enc create mode 100644 package.json create mode 100644 test/cloud-storage/testAuthSample.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..c2658d7d1b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 00000000000..a0f3b329d5f --- /dev/null +++ b/.jshintrc @@ -0,0 +1,7 @@ +{ + "camelcase" : true, + "indent" : 2, + "strict" : true, + "globalstrict" : true, + "node" : true +} diff --git a/.travis.yml b/.travis.yml index c520c59544a..cb525c568eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,15 @@ sudo: false #add language, etc. here +language: node_js +node_js: + - "stable" + - "0.10" cache: directories: - $HOME/gcloud/ env: - - PATH=$PATH:$HOME/gcloud/google-cloud-sdk/bin GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/client_secrets.json #Other environment variables on same line + - PATH=$PATH:$HOME/gcloud/google-cloud-sdk/bin GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/nodejs-docs-samples.json TEST_BUCKET_NAME=cloud-samples-tests TEST_PROJECT_ID=cloud-samples-tests #Other environment variables on same line before_install: #ENCRYPT YOUR PRIVATE KEY (If you need authentication) @@ -27,9 +31,9 @@ before_install: printf '\ny\n\ny\ny\n' | ./google-cloud-sdk/install.sh && cd $TRAVIS_BUILD_DIR; fi - - gcloud -q components update - - if [ -a client_secrets.json ]; then - gcloud -q auth activate-service-account --key-file client_secrets.json; + - openssl aes-256-cbc -K $encrypted_4fda24e244ca_key -iv $encrypted_4fda24e244ca_iv -in nodejs-docs-samples.json.enc -out nodejs-docs-samples.json -d + - if [ -a nodejs-docs-samples.json ]; then + gcloud auth activate-service-account --key-file nodejs-docs-samples.json; fi install: diff --git a/README.md b/README.md index f17a3a8016e..ddf5b486c4d 100644 --- a/README.md +++ b/README.md @@ -1,52 +1,12 @@ -## Project Name +## Google Cloud Platform NodeJS Samples + +This repository holds the samples used in the nodejs documentation on [cloud.google.com](https://cloud.google.com). -Project description. See our other [Google Cloud Platform github repos](https://github.com/GoogleCloudPlatform) for sample applications and scaffolding for other frameworks and use cases. -## Run Locally -1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app). -2. Setup the gcloud tool. - - ``` - gcloud components update app - gcloud auth login - gcloud config set project - ``` - You don't need a valid app-id to run locally, but will need a valid id to deploy below. - -1. Clone this repo. - - ``` - git clone https://github.com/GoogleCloudPlatform/.git - ``` -1. Run this project locally from the command line. - - ``` - gcloud preview app run / - ``` - -1. Visit the application at [http://localhost:8080](http://localhost:8080). - -## Deploying - -1. Use the [Cloud Developer Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical) -2. Configure gcloud with your app id. - - ``` - gcloud config set project - ``` -1. Use the [Admin Console](https://appengine.google.com) to view data, queues, and other App Engine specific administration tasks. -1. Use gcloud to deploy your app. - - ``` - gcloud preview app deploy / - ``` - -1. Congratulations! Your application is now live at your-app-id.appspot.com - ## Contributing changes * See [CONTRIBUTING.md](CONTRIBUTING.md) diff --git a/cloud-storage/authSample.js b/cloud-storage/authSample.js new file mode 100644 index 00000000000..1ee0ae08faa --- /dev/null +++ b/cloud-storage/authSample.js @@ -0,0 +1,54 @@ +/** + * @fileoverview Sample code that grabs default credentials from the + * environment, then uses it to make an api call. + */ + +var google = require('googleapis'); + +/** + * Fetches a list of the buckets under the given project id. + * + * @param {String} projectId - the project id that owns the buckets. + * @param {requestCallback} callback - a function to be called when the server + * responds with the list of buckets. + */ +// [START list_buckets] +function listBuckets(projectId, callback) { + google.auth.getApplicationDefault(function(error, authClient) { + if (error) { + return callback(error); + } + + // Depending on the environment that provides the default credentials + // (eg Compute Engine, App Engine), the credentials may require us to + // specify the scopes we need explicitly. + // Check for this case, and inject the Cloud Storage scope if required. + if (authClient.createScopedRequired && + authClient.createScopedRequired()) { + authClient = authClient.createScoped( + ['https://www.googleapis.com/auth/devstorage.read_write']); + } + + // Create the service object. + var storage = google.storage('v1'); + // Make the api call to list the buckets. + storage.buckets.list({ + auth: authClient, + project: projectId + }, callback); + }); +} +// [END list_buckets] + +module.exports = { + listBuckets: listBuckets +}; + +/** + * Callback for remote requests. + * + * @callback requestCallback + * @param {Object} error - if there's an error with the request, this is pass + * through to the callback. + * @param {Object} response - the response for the request. + */ diff --git a/nodejs-docs-samples.json.enc b/nodejs-docs-samples.json.enc new file mode 100644 index 0000000000000000000000000000000000000000..10b10bd1e252f44166786f9b29928acd35cadd7c GIT binary patch literal 2048 zcmV+b2>-8qjvW?``tXo$v=kkCBW)B8 zw4tp_W#smQiC$)fcJD^q@3?<9a(pSHrydU|zcS!xr@~Nrz)9qU)V3uN?;Xu5*Y1LT zOf6m_uEJb!9>~6u51~9Z5j&xxIa0eX%t{KeII}&;d9cJaOhjhRf6-8~y?D^%<(ELzsjS8%pxamWgK;9X- zH&~cQf`qw4xAo4aw2EqN+(21YX}M;#Snyf}_lD8;>k~-ulro8NbTOuk;u+BZw+7qy zpBTUZ;LT0r`C1}vfiyU1N@=s8^t&pM*K*M3b=SgKCR0s#50w((5M2{@h}7?>ae`hZ ztHSFi<#}Ayj84AksqyrpUQdaO(HVk7eC#k%lVXgfz9Bkr3+pE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-9j8S>z(BIcLV>nC4xzcI+7f+A%fSs`paLqp-Ml z{TN~S1)lBMMsqbvqjC$wUid(07*BkLiBIwpQ6l9MmU2Im#*ZOmvpl~&tD&(kdqk~L zLj0_Pe$`sDEN@uxtt%Z6rlSPF?$wtj4y$6hHKDBG?;6e_;HNh_oKcSXmm`vA&leG_qH@!&hgve_RD z`oFf74g9w6c||9iaVZ0lgnAj9M#LI+41u?>ZuxjPLdo-EEMPi}O;3Ql;U9)F8vyX8 e`yJ}DoU8-gU&@Ru#4dtodM&kl664w9J{M$HbMy@W literal 0 HcmV?d00001 diff --git a/package.json b/package.json new file mode 100644 index 00000000000..2b59df20b63 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "nodejs-docs-samples", + "version": "0.0.1", + "description": "Samples used in the nodejs documentation on cloud.google.com", + "main": "index.js", + "scripts": { + "test": "mocha --recursive" + }, + "author": "jerjou@google.com", + "license": "Apache 2", + "dependencies": { + "googleapis": "~2.1.3" + }, + "devDependencies": { + "mocha": "~2.2.5", + "lodash": "~3.10.1" + }, + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/python-docs-samples.git" + } +} diff --git a/test/cloud-storage/testAuthSample.js b/test/cloud-storage/testAuthSample.js new file mode 100644 index 00000000000..22049f89141 --- /dev/null +++ b/test/cloud-storage/testAuthSample.js @@ -0,0 +1,27 @@ +/** + * @fileoverview Tests for the list-buckets module. + */ + +var assert = require("assert"); +var _ = require('lodash'); + +var authSample = require("../../cloud-storage/authSample"); + +describe('listBuckets', function() { + it('returns a list of buckets', function (done) { + authSample.listBuckets( + process.env.TEST_PROJECT_ID, + function(error, success) { + if (error) { + done('Should not have returned an error: ' + error); + } else { + assert(success.items.length > 0); + assert(_.find(success.items, function(item) { + return item.name == process.env.TEST_BUCKET_NAME; + })); + done(); + } + }); + }); +}); +