Skip to content

Commit 36b77ec

Browse files
author
Jerjou Cheng
committed
Add sample for using cloud storage api.
1 parent bc52aa7 commit 36b77ec

File tree

8 files changed

+122
-47
lines changed

8 files changed

+122
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.jshintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"camelcase" : true,
3+
"indent" : 2,
4+
"strict" : true,
5+
"globalstrict" : true,
6+
"node" : true
7+
}

.travis.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
sudo: false
22
#add language, etc. here
3+
language: node_js
4+
node_js:
5+
- "stable"
6+
- "0.10"
37

48
cache:
59
directories:
610
- $HOME/gcloud/
711
env:
8-
- PATH=$PATH:$HOME/gcloud/google-cloud-sdk/bin GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/client_secrets.json #Other environment variables on same line
12+
- 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
913

1014
before_install:
1115
#ENCRYPT YOUR PRIVATE KEY (If you need authentication)
@@ -27,9 +31,9 @@ before_install:
2731
printf '\ny\n\ny\ny\n' | ./google-cloud-sdk/install.sh &&
2832
cd $TRAVIS_BUILD_DIR;
2933
fi
30-
- gcloud -q components update
31-
- if [ -a client_secrets.json ]; then
32-
gcloud -q auth activate-service-account --key-file client_secrets.json;
34+
- openssl aes-256-cbc -K $encrypted_4fda24e244ca_key -iv $encrypted_4fda24e244ca_iv -in nodejs-docs-samples.json.enc -out nodejs-docs-samples.json -d
35+
- if [ -a nodejs-docs-samples.json ]; then
36+
gcloud auth activate-service-account --key-file nodejs-docs-samples.json;
3337
fi
3438

3539
install:

README.md

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,12 @@
1-
## Project Name
1+
## Google Cloud Platform NodeJS Samples
2+
3+
This repository holds the samples used in the nodejs documentation on [cloud.google.com](https://cloud.google.com).
24

3-
Project description.
45

56
See our other [Google Cloud Platform github
67
repos](https://github.com/GoogleCloudPlatform) for sample applications and
78
scaffolding for other frameworks and use cases.
89

9-
## Run Locally
10-
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).
11-
2. Setup the gcloud tool.
12-
13-
```
14-
gcloud components update app
15-
gcloud auth login
16-
gcloud config set project <your-app-id>
17-
```
18-
You don't need a valid app-id to run locally, but will need a valid id to deploy below.
19-
20-
1. Clone this repo.
21-
22-
```
23-
git clone https://github.com/GoogleCloudPlatform/<REPO NAME>.git
24-
```
25-
1. Run this project locally from the command line.
26-
27-
```
28-
gcloud preview app run <REPO NAME>/
29-
```
30-
31-
1. Visit the application at [http://localhost:8080](http://localhost:8080).
32-
33-
## Deploying
34-
35-
1. Use the [Cloud Developer Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical)
36-
2. Configure gcloud with your app id.
37-
38-
```
39-
gcloud config set project <your-app-id>
40-
```
41-
1. Use the [Admin Console](https://appengine.google.com) to view data, queues, and other App Engine specific administration tasks.
42-
1. Use gcloud to deploy your app.
43-
44-
```
45-
gcloud preview app deploy <REPO NAME>/
46-
```
47-
48-
1. Congratulations! Your application is now live at your-app-id.appspot.com
49-
5010
## Contributing changes
5111

5212
* See [CONTRIBUTING.md](CONTRIBUTING.md)

cloud-storage/authSample.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @fileoverview Sample code that grabs default credentials from the
3+
* environment, then uses it to make an api call.
4+
*/
5+
6+
var google = require('googleapis');
7+
8+
/**
9+
* Fetches a list of the buckets under the given project id.
10+
*
11+
* @param {String} projectId - the project id that owns the buckets.
12+
* @param {requestCallback} callback - a function to be called when the server
13+
* responds with the list of buckets.
14+
*/
15+
// [START list_buckets]
16+
function listBuckets(projectId, callback) {
17+
google.auth.getApplicationDefault(function(error, authClient) {
18+
if (error) {
19+
return callback(error);
20+
}
21+
22+
// Depending on the environment that provides the default credentials
23+
// (eg Compute Engine, App Engine), the credentials may require us to
24+
// specify the scopes we need explicitly.
25+
// Check for this case, and inject the Cloud Storage scope if required.
26+
if (authClient.createScopedRequired &&
27+
authClient.createScopedRequired()) {
28+
authClient = authClient.createScoped(
29+
['https://www.googleapis.com/auth/devstorage.read_write']);
30+
}
31+
32+
// Create the service object.
33+
var storage = google.storage('v1');
34+
// Make the api call to list the buckets.
35+
storage.buckets.list({
36+
auth: authClient,
37+
project: projectId
38+
}, callback);
39+
});
40+
}
41+
// [END list_buckets]
42+
43+
module.exports = {
44+
listBuckets: listBuckets
45+
};
46+
47+
/**
48+
* Callback for remote requests.
49+
*
50+
* @callback requestCallback
51+
* @param {Object} error - if there's an error with the request, this is pass
52+
* through to the callback.
53+
* @param {Object} response - the response for the request.
54+
*/

nodejs-docs-samples.json.enc

2 KB
Binary file not shown.

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "nodejs-docs-samples",
3+
"version": "0.0.1",
4+
"description": "Samples used in the nodejs documentation on cloud.google.com",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha --recursive"
8+
},
9+
"author": "[email protected]",
10+
"license": "Apache 2",
11+
"dependencies": {
12+
"googleapis": "~2.1.3"
13+
},
14+
"devDependencies": {
15+
"mocha": "~2.2.5",
16+
"lodash": "~3.10.1"
17+
},
18+
"repository": {
19+
"type": "git",
20+
"url": "https://github.com/GoogleCloudPlatform/python-docs-samples.git"
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @fileoverview Tests for the list-buckets module.
3+
*/
4+
5+
var assert = require("assert");
6+
var _ = require('lodash');
7+
8+
var authSample = require("../../cloud-storage/authSample");
9+
10+
describe('listBuckets', function() {
11+
it('returns a list of buckets', function (done) {
12+
authSample.listBuckets(
13+
process.env.TEST_PROJECT_ID,
14+
function(error, success) {
15+
if (error) {
16+
done('Should not have returned an error: ' + error);
17+
} else {
18+
assert(success.items.length > 0);
19+
assert(_.find(success.items, function(item) {
20+
return item.name == process.env.TEST_BUCKET_NAME;
21+
}));
22+
done();
23+
}
24+
});
25+
});
26+
});
27+

0 commit comments

Comments
 (0)