Skip to content

Commit

Permalink
Add sample for using cloud storage api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerjou Cheng committed Aug 14, 2015
1 parent bc52aa7 commit 81dd1f1
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
7 changes: 7 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"camelcase" : true,
"indent" : 2,
"strict" : true,
"globalstrict" : true,
"node" : true
}
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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:
Expand Down
47 changes: 4 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
## Project Name
## Google Cloud Platform NodeJS Samples

Project description.
This repository holds the samples used in the nodejs documentation on [cloud.google.com](https://cloud.google.com).

[![Build Status](https://travis-ci.org/GoogleCloudPlatform/nodejs-docs-samples.svg)](https://travis-ci.org/GoogleCloudPlatform/nodejs-docs-samples)

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 <your-app-id>
```
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/<REPO NAME>.git
```
1. Run this project locally from the command line.

```
gcloud preview app run <REPO NAME>/
```

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 <your-app-id>
```
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 <REPO NAME>/
```

1. Congratulations! Your application is now live at your-app-id.appspot.com

## Contributing changes

* See [CONTRIBUTING.md](CONTRIBUTING.md)
Expand Down
54 changes: 54 additions & 0 deletions cloud-storage/authSample.js
Original file line number Diff line number Diff line change
@@ -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.
*/
Binary file added nodejs-docs-samples.json.enc
Binary file not shown.
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
27 changes: 27 additions & 0 deletions test/cloud-storage/testAuthSample.js
Original file line number Diff line number Diff line change
@@ -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();
}
});
});
});

0 comments on commit 81dd1f1

Please sign in to comment.