Skip to content

Commit 7f3306c

Browse files
authored
Upgraded storage sample (GoogleCloudPlatform#160)
* Update storage sample to use gcloud. * Upgrade storage sample. * Fix link
1 parent 99cf3ef commit 7f3306c

File tree

8 files changed

+385
-145
lines changed

8 files changed

+385
-145
lines changed

storage/README.md

+27-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ amount of data at any time.
1111

1212
* [Setup](#setup)
1313
* [Samples](#samples)
14-
* [Authentication](#authentication)
14+
* [Buckets](#buckets)
1515

1616
## Setup
1717

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

2626
## Samples
2727

28-
### Authentication
28+
### Buckets
2929

30-
View the [documentation][authentication_docs] or the [source code][authentication_code].
30+
View the [documentation][buckets_docs] or the [source code][buckets_code].
3131

32-
__Run the sample:__
32+
__Usage:__
3333

34-
node authSample <your-project-id>
34+
```
35+
Usage: node buckets [COMMAND] [ARGS...]
3536
36-
[authentication_docs]: https://cloud.google.com/storage/docs/authentication#acd-examples
37-
[authentication_code]: authSample.js
37+
Commands:
38+
39+
create [BUCKET_NAME]
40+
list
41+
delete [BUCKET_NAME]
42+
```
43+
44+
__Create a bucket:__
45+
46+
node buckets create [BUCKET_NAME]
47+
48+
__List buckets:__
49+
50+
node buckets list
51+
52+
__Delete a bucket:__
53+
54+
node buckets delete [BUCKET_NAME]
55+
56+
[buckets_docs]: https://cloud.google.com/storage/docs/json_api/v1/json-api-nodejs-samples
57+
[buckets_code]: buckets.js

storage/authSample.js

-83
This file was deleted.

storage/buckets.js

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START all]
17+
// [START setup]
18+
// By default, gcloud will authenticate using the service account file specified
19+
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
20+
// project specified by the GCLOUD_PROJECT environment variable. See
21+
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
22+
var gcloud = require('gcloud');
23+
24+
// Get a reference to the storage component
25+
var storage = gcloud.storage();
26+
// [END setup]
27+
28+
// [START create]
29+
/**
30+
* Creates a new bucket with the given name.
31+
*
32+
* @param {string} name The name of the new bucket.
33+
* @param {function} cb The callback function.
34+
*/
35+
function createBucketExample (name, callback) {
36+
if (!name) {
37+
return callback(new Error('"name" is required!'));
38+
}
39+
40+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage?method=createBucket
41+
storage.createBucket(name, function (err, bucket, apiResponse) {
42+
if (err) {
43+
return callback(err);
44+
}
45+
46+
console.log('Created bucket: ' + name);
47+
return callback(null, bucket);
48+
});
49+
}
50+
// [END create]
51+
52+
// [START list]
53+
/**
54+
* Fetches all of the current project's buckets.
55+
*
56+
* @param {function} cb The callback function.
57+
*/
58+
function listBucketsExample (callback) {
59+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage?method=getBuckets
60+
storage.getBuckets(function (err, buckets, apiResponse) {
61+
if (err) {
62+
return callback(err);
63+
}
64+
65+
console.log('Found ' + buckets.length + ' buckets!');
66+
return callback(null, buckets, apiResponse);
67+
});
68+
}
69+
// [END list]
70+
71+
// [START delete]
72+
/**
73+
* Deletes the specified bucket.
74+
*
75+
* @param {string} name The name of the bucket to delete.
76+
* @param {function} cb The callback function.
77+
*/
78+
function deleteBucketExample (name, callback) {
79+
if (!name) {
80+
return callback(new Error('"name" is required!'));
81+
}
82+
83+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage?method=bucket
84+
var bucket = storage.bucket(name);
85+
86+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/bucket?method=delete
87+
bucket.delete(function (err, apiResponse) {
88+
if (err) {
89+
return callback(err);
90+
}
91+
92+
console.log('Deleted bucket: ' + name);
93+
return callback(null, apiResponse);
94+
});
95+
}
96+
// [END delete]
97+
98+
// [START usage]
99+
function printUsage () {
100+
console.log('Usage: node buckets [COMMAND] [ARGS...]');
101+
console.log('\nCommands:\n');
102+
console.log('\tcreate [BUCKET_NAME]');
103+
console.log('\tlist');
104+
console.log('\tdelete [BUCKET_NAME]');
105+
}
106+
// [END usage]
107+
108+
// Run the command-line program
109+
function main (args, cb) {
110+
var command = args.shift();
111+
if (command === 'create') {
112+
createBucketExample(args[0], cb);
113+
} else if (command === 'list') {
114+
listBucketsExample(cb);
115+
} else if (command === 'delete') {
116+
deleteBucketExample(args[0], cb);
117+
} else {
118+
printUsage();
119+
cb();
120+
}
121+
}
122+
123+
if (module === require.main) {
124+
main(process.argv.slice(2), console.log);
125+
}
126+
// [END all]
127+
128+
exports.createBucketExample = createBucketExample;
129+
exports.listBucketsExample = listBucketsExample;
130+
exports.deleteBucketExample = deleteBucketExample;
131+
exports.printUsage = printUsage;
132+
exports.main = main;

storage/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
1010
},
1111
"dependencies": {
12-
"googleapis": "^12.0.0"
12+
"gcloud": "^0.37.0"
1313
},
1414
"devDependencies": {
15-
"mocha": "^2.5.3"
15+
"mocha": "^3.0.1"
1616
}
1717
}

storage/system-test/authSample.test.js

-35
This file was deleted.

storage/system-test/buckets.test.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var bucketsExample = require('../buckets');
17+
var bucketName = '' + new Date().getTime() + Math.floor(Math.random() * 100000000);
18+
19+
describe('storage:buckets', function () {
20+
describe('create', function () {
21+
it('should create a bucket', function (done) {
22+
bucketsExample.createBucketExample(bucketName, function (err, bucket) {
23+
assert.ifError(err);
24+
assert.equal(bucket.name, bucketName);
25+
assert(console.log.calledWith('Created bucket: ' + bucketName));
26+
done();
27+
});
28+
});
29+
});
30+
describe('list', function () {
31+
it('should list buckets', function (done) {
32+
bucketsExample.listBucketsExample(function (err, buckets) {
33+
assert.ifError(err);
34+
assert(Array.isArray(buckets));
35+
assert(buckets.length > 0);
36+
assert(console.log.calledWith('Found ' + buckets.length + ' buckets!'));
37+
done();
38+
});
39+
});
40+
});
41+
describe('delete', function () {
42+
it('should delete a bucket', function (done) {
43+
bucketsExample.deleteBucketExample(bucketName, function (err, apiResponse) {
44+
assert.ifError(err);
45+
console.log(apiResponse);
46+
assert(console.log.calledWith('Deleted bucket: ' + bucketName));
47+
done();
48+
});
49+
});
50+
});
51+
});

storage/test/authSample.test.js

-18
This file was deleted.

0 commit comments

Comments
 (0)