-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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) { | ||
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!'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. print the list There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
This file was deleted.
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(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
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.
won't the client library handle this?
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.
I think it does, don't know what came over me when I decided to check the argument in my sample function.