-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add sample for using cloud storage api. #1
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"camelcase" : true, | ||
"indent" : 2, | ||
"strict" : true, | ||
"globalstrict" : true, | ||
"node" : true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,43 @@ | ||
sudo: false | ||
#add language, etc. here | ||
language: node_js | ||
node_js: | ||
- "stable" | ||
- "0.10" | ||
|
||
cache: | ||
directories: | ||
- $HOME/gcloud/ | ||
- $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) | ||
# 1. Install and login to the Travis CLI: | ||
# $ gem install travis | ||
# $ travis login | ||
# 2. Move your json private key to client_secrets.json | ||
# 3. Run: | ||
# $ travis encrypt-file client_secrets.json --add | ||
# 4. Commit changes: | ||
# $ git add client_secrets.json.enc | ||
# $ git commit client_secrets.json.enc .travis.yml | ||
#ENCRYPT YOUR PRIVATE KEY (If you need authentication) | ||
# 1. Install and login to the Travis CLI: | ||
# $ gem install travis | ||
# $ travis login | ||
# 2. Move your json private key to client_secrets.json | ||
# 3. Run: | ||
# $ travis encrypt-file client_secrets.json --add | ||
# 4. Commit changes: | ||
# $ git add client_secrets.json.enc | ||
# $ git commit client_secrets.json.enc .travis.yml | ||
|
||
- if [ ! -d $HOME/gcloud/google-cloud-sdk ]; then | ||
mkdir -p $HOME/gcloud && | ||
wget https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz --directory-prefix=$HOME/gcloud && | ||
cd $HOME/gcloud && | ||
tar xzf google-cloud-sdk.tar.gz && | ||
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; | ||
fi | ||
- if [ ! -d $HOME/gcloud/google-cloud-sdk ]; then | ||
mkdir -p $HOME/gcloud && | ||
wget https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz --directory-prefix=$HOME/gcloud && | ||
cd $HOME/gcloud && | ||
tar xzf google-cloud-sdk.tar.gz && | ||
printf '\ny\n\ny\ny\n' | ./google-cloud-sdk/install.sh && | ||
cd $TRAVIS_BUILD_DIR; | ||
fi | ||
- openssl aes-256-cbc -K $encrypted_95e832a36b06_key -iv $encrypted_95e832a36b06_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: | ||
#Add app specific setup here | ||
#Use '-q' to disable interactive prompts | ||
#Add app specific setup here | ||
#Use '-q' to disable interactive prompts | ||
|
||
script: | ||
#Test and/or deploy your app with gcloud commands here! | ||
#Test and/or deploy your app with gcloud commands here! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
/** | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* 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 | ||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"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", | ||
"private": true, | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you mark this as a private package to prevent accidental publication?