Skip to content

Commit

Permalink
Add Cloud Storage file samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Aug 8, 2016
1 parent 1dc0854 commit 05f1cbd
Show file tree
Hide file tree
Showing 13 changed files with 1,122 additions and 59 deletions.
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"describe",
"it",
"assert",
"sinon"
"sinon",
"uuid"
],
"ignore": [
"appengine/bower/public/bower_components",
Expand All @@ -58,17 +59,18 @@
"all-cover": "npm run pretest && nyc --cache npm run all-test && nyc report --reporter=html && nyc report --reporter=lcov"
},
"devDependencies": {
"async": "^1.5.2",
"async": "^2.0.1",
"intelli-espower-loader": "^1.0.1",
"mocha": "^2.5.3",
"mocha": "^3.0.2",
"node-uuid": "^1.4.7",
"nodejs-repo-tools": "git+https://github.com/GoogleCloudPlatform/nodejs-repo-tools.git#bbbb6035d77671eb053dbe6b6f0e3ff983f79639",
"nyc": "^6.4.4",
"nyc": "^7.1.0",
"power-assert": "^1.4.1",
"proxyquire": "^1.7.10",
"request": "^2.72.0",
"semistandard": "^8.0.0",
"shelljs": "^0.7.3",
"sinon": "^1.17.5",
"supertest": "^1.2.0"
"supertest": "^2.0.0"
}
}
32 changes: 23 additions & 9 deletions storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ amount of data at any time.
* [Setup](#setup)
* [Samples](#samples)
* [Buckets](#buckets)
* [Files](#files)

## Setup

Expand All @@ -29,7 +30,7 @@ amount of data at any time.

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

__Usage:__
__Usage:__ `node buckets --help`

```
Usage: node buckets [COMMAND] [ARGS...]
Expand All @@ -41,17 +42,30 @@ Commands:
delete [BUCKET_NAME]
```

__Create a bucket:__
[buckets_docs]: https://cloud.google.com/storage/docs
[buckets_code]: buckets.js

### Files

node buckets create [BUCKET_NAME]
View the [documentation][files_docs] or the [source code][files_code].

__List buckets:__
__Usage:__ `node files --help`

node buckets list
```
Usage: node files [COMMAND] [ARGS...]
__Delete a bucket:__
Commands:
node buckets delete [BUCKET_NAME]
list [BUCKET_NAME]
listByPrefix [BUCKET_NAME] [PREFIX] [DELIMITER]
upload [BUCKET_NAME] [FILE_NAME]
download [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]
delete [BUCKET_NAME] [FILE_NAME]
getMetadata [BUCKET_NAME] [FILE_NAME]
makePublic [BUCKET_NAME] [FILE_NAME]
move [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]
copy [BUCKET_NAME] [SRC_FILE_NAME] [DEST_BUCKET_NAME] [DEST_FILE_NAME]
```

[buckets_docs]: https://cloud.google.com/storage/docs/json_api/v1/json-api-nodejs-samples
[buckets_code]: buckets.js
[files_docs]: https://cloud.google.com/storage/docs
[files_code]: files.js
34 changes: 15 additions & 19 deletions storage/buckets.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@ var storage = gcloud.storage();
* @param {string} name The name of the new bucket.
* @param {function} cb The callback function.
*/
function createBucketExample (name, callback) {
function createBucket (name, callback) {
if (!name) {
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) {
storage.createBucket(name, function (err, bucket) {
if (err) {
return callback(err);
}

console.log('Created bucket: ' + name);
console.log('Created bucket: %s', name);
return callback(null, bucket);
});
}
Expand All @@ -55,15 +54,14 @@ function createBucketExample (name, callback) {
*
* @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) {
function listBuckets (callback) {
storage.getBuckets(function (err, buckets) {
if (err) {
return callback(err);
}

console.log('Found ' + buckets.length + ' buckets!');
return callback(null, buckets, apiResponse);
console.log('Found %d buckets!', buckets.length);
return callback(null, buckets);
});
}
// [END list]
Expand All @@ -75,21 +73,19 @@ function listBucketsExample (callback) {
* @param {string} name The name of the bucket to delete.
* @param {function} cb The callback function.
*/
function deleteBucketExample (name, callback) {
function deleteBucket (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);
console.log('Deleted bucket: %s', name);
return callback(null, apiResponse);
});
}
Expand All @@ -109,11 +105,11 @@ function printUsage () {
function main (args, cb) {
var command = args.shift();
if (command === 'create') {
createBucketExample(args[0], cb);
createBucket(args[0], cb);
} else if (command === 'list') {
listBucketsExample(cb);
listBuckets(cb);
} else if (command === 'delete') {
deleteBucketExample(args[0], cb);
deleteBucket(args[0], cb);
} else {
printUsage();
cb();
Expand All @@ -125,8 +121,8 @@ if (module === require.main) {
}
// [END all]

exports.createBucketExample = createBucketExample;
exports.listBucketsExample = listBucketsExample;
exports.deleteBucketExample = deleteBucketExample;
exports.createBucket = createBucket;
exports.listBuckets = listBuckets;
exports.deleteBucket = deleteBucket;
exports.printUsage = printUsage;
exports.main = main;
Loading

0 comments on commit 05f1cbd

Please sign in to comment.