Skip to content
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

Upgraded storage sample #160

Merged
merged 3 commits into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ amount of data at any time.

* [Setup](#setup)
* [Samples](#samples)
* [Authentication](#authentication)
* [Buckets](#buckets)

## Setup

Expand All @@ -25,13 +25,33 @@ amount of data at any time.

## Samples

### Authentication
### Buckets

View the [documentation][authentication_docs] or the [source code][authentication_code].
View the [documentation][buckets_docs] or the [source code][buckets_code].

__Run the sample:__
__Usage:__

node authSample <your-project-id>
```
Usage: node buckets [COMMAND] [ARGS...]

[authentication_docs]: https://cloud.google.com/storage/docs/authentication#acd-examples
[authentication_code]: authSample.js
Commands:

create [BUCKET_NAME]
list
delete [BUCKET_NAME]
```

__Create a bucket:__

node buckets create [BUCKET_NAME]

__List buckets:__

node buckets list

__Delete a bucket:__

node buckets delete [BUCKET_NAME]

[buckets_docs]: https://cloud.google.com/storage/docs/json_api/v1/json-api-nodejs-samples
[buckets_code]: buckets.js
83 changes: 0 additions & 83 deletions storage/authSample.js

This file was deleted.

132 changes: 132 additions & 0 deletions storage/buckets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright 2015-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 all]
// [START setup]
// By default, gcloud will authenticate using the service account file specified
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
// project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
var gcloud = require('gcloud');

// Get a reference to the storage component
var storage = gcloud.storage();
// [END setup]

// [START create]
/**
* Creates a new bucket with the given name.
*
* @param {string} name The name of the new bucket.
* @param {function} cb The callback function.
*/
function createBucketExample (name, callback) {
if (!name) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't the client library handle this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does, don't know what came over me when I decided to check the argument in my sample function.

return callback(new Error('"name" is required!'));
}

// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage?method=createBucket
storage.createBucket(name, function (err, bucket, apiResponse) {
if (err) {
return callback(err);
}

console.log('Created bucket: ' + name);
return callback(null, bucket);
});
}
// [END create]

// [START list]
/**
* Fetches all of the current project's buckets.
*
* @param {function} cb The callback function.
*/
function listBucketsExample (callback) {
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage?method=getBuckets
storage.getBuckets(function (err, buckets, apiResponse) {
if (err) {
return callback(err);
}

console.log('Found ' + buckets.length + ' buckets!');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print the list

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list will get printed when the user runs the sample. If I print it here it will get printed twice.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha.

return callback(null, buckets, apiResponse);
});
}
// [END list]

// [START delete]
/**
* Deletes the specified bucket.
*
* @param {string} name The name of the bucket to delete.
* @param {function} cb The callback function.
*/
function deleteBucketExample (name, callback) {
if (!name) {
return callback(new Error('"name" is required!'));
}

// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage?method=bucket
var bucket = storage.bucket(name);

// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/bucket?method=delete
bucket.delete(function (err, apiResponse) {
if (err) {
return callback(err);
}

console.log('Deleted bucket: ' + name);
return callback(null, apiResponse);
});
}
// [END delete]

// [START usage]
function printUsage () {
console.log('Usage: node buckets [COMMAND] [ARGS...]');
console.log('\nCommands:\n');
console.log('\tcreate [BUCKET_NAME]');
console.log('\tlist');
console.log('\tdelete [BUCKET_NAME]');
}
// [END usage]

// Run the command-line program
function main (args, cb) {
var command = args.shift();
if (command === 'create') {
createBucketExample(args[0], cb);
} else if (command === 'list') {
listBucketsExample(cb);
} else if (command === 'delete') {
deleteBucketExample(args[0], cb);
} else {
printUsage();
cb();
}
}

if (module === require.main) {
main(process.argv.slice(2), console.log);
}
// [END all]

exports.createBucketExample = createBucketExample;
exports.listBucketsExample = listBucketsExample;
exports.deleteBucketExample = deleteBucketExample;
exports.printUsage = printUsage;
exports.main = main;
4 changes: 2 additions & 2 deletions storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
},
"dependencies": {
"googleapis": "^12.0.0"
"gcloud": "^0.37.0"
},
"devDependencies": {
"mocha": "^2.5.3"
"mocha": "^3.0.1"
}
}
35 changes: 0 additions & 35 deletions storage/system-test/authSample.test.js

This file was deleted.

51 changes: 51 additions & 0 deletions storage/system-test/buckets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2015-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';

var bucketsExample = require('../buckets');
var bucketName = '' + new Date().getTime() + Math.floor(Math.random() * 100000000);

describe('storage:buckets', function () {
describe('create', function () {
it('should create a bucket', function (done) {
bucketsExample.createBucketExample(bucketName, function (err, bucket) {
assert.ifError(err);
assert.equal(bucket.name, bucketName);
assert(console.log.calledWith('Created bucket: ' + bucketName));
done();
});
});
});
describe('list', function () {
it('should list buckets', function (done) {
bucketsExample.listBucketsExample(function (err, buckets) {
assert.ifError(err);
assert(Array.isArray(buckets));
assert(buckets.length > 0);
assert(console.log.calledWith('Found ' + buckets.length + ' buckets!'));
done();
});
});
});
describe('delete', function () {
it('should delete a bucket', function (done) {
bucketsExample.deleteBucketExample(bucketName, function (err, apiResponse) {
assert.ifError(err);
console.log(apiResponse);
assert(console.log.calledWith('Deleted bucket: ' + bucketName));
done();
});
});
});
});
18 changes: 0 additions & 18 deletions storage/test/authSample.test.js

This file was deleted.

Loading