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

Add Cloud Storage file samples. #162

Merged
merged 1 commit into from
Aug 8, 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
1 change: 1 addition & 0 deletions appengine/disk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
seen.txt
17 changes: 0 additions & 17 deletions appengine/disk/seen.txt

This file was deleted.

11 changes: 6 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,17 @@
"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",
"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
63 changes: 31 additions & 32 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 @@ -105,28 +101,31 @@ function printUsage () {
}
// [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();
// The command-line program
var program = {
createBucket: createBucket,
listBuckets: listBuckets,
deleteBucket: deleteBucket,
printUsage: printUsage,

// Executed when this program is run from the command-line
main: function (args, cb) {
var command = args.shift();
if (command === 'create') {
this.createBucket(args[0], cb);
} else if (command === 'list') {
this.listBuckets(cb);
} else if (command === 'delete') {
this.deleteBucket(args[0], cb);
} else {
this.printUsage();
}
}
}
};

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

exports.createBucketExample = createBucketExample;
exports.listBucketsExample = listBucketsExample;
exports.deleteBucketExample = deleteBucketExample;
exports.printUsage = printUsage;
exports.main = main;
module.exports = program;
Loading