Skip to content

Commit e4958b0

Browse files
authored
Storage ACL samples. (#172)
Storage ACL samples.
1 parent e8e3e9d commit e4958b0

File tree

6 files changed

+872
-1
lines changed

6 files changed

+872
-1
lines changed

storage/README.md

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

1212
* [Setup](#setup)
1313
* [Samples](#samples)
14+
* [ACL (Access Control Lists)](#acl-access-control-lists)
1415
* [Buckets](#buckets)
1516
* [Encryption](#encryption)
1617
* [Files](#files)
@@ -28,6 +29,41 @@ amount of data at any time.
2829

2930
## Samples
3031

32+
### ACL (Access Control Lists)
33+
34+
View the [documentation][acl_docs] or the [source code][acl_code].
35+
36+
__Usage:__ `node acl --help`
37+
38+
```
39+
Commands:
40+
add <entity> <role> Add access controls on a bucket or file.
41+
get [entity] Get access controls on a bucket or file.
42+
delete <entity> Delete access controls from a bucket or file.
43+
44+
Options:
45+
--bucket, -b The target storage bucket. [string] [required]
46+
--default, -d Whether to set default access controls. Only valid when setting access controls on
47+
a bucket. [boolean]
48+
--file, -f The target file. [string]
49+
--help Show help [boolean]
50+
51+
Examples:
52+
node acl add [email protected] OWNER -b mybucket Add OWNER access controls for
53+
"[email protected]" to "mybucket".
54+
node acl add viewers-2256 WRITER -b mybucket -d Add default WRITER access controls to
55+
"mybucket" for "viewers-2256".
56+
node acl get editors-1234 -b mybucket Get access controls for "editors-1234" in
57+
"mybucket".
58+
node acl delete -b mybucket -f file.txt Delete all access controls for all entities
59+
from "file.txt" in "mybucket".
60+
61+
For more information, see https://cloud.google.com/storage/docs/access-control/create-manage-lists
62+
```
63+
64+
[acl_docs]: https://cloud.google.com/storage/docs/access-control/create-manage-lists
65+
[acl_code]: acl.js
66+
3167
### Buckets
3268

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

storage/acl.js

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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, the client will authenticate using the service account file
19+
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
20+
// the project specified by the GCLOUD_PROJECT environment variable. See
21+
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
22+
var Storage = require('@google-cloud/storage');
23+
24+
// Instantiate a storage client
25+
var storage = Storage();
26+
// [END setup]
27+
28+
// [START add_access_control]
29+
/**
30+
* Add access controls to a bucket or file.
31+
*
32+
* @param {object} options Configuration options.
33+
* @param {string} options.bucket The bucket for the new access controls.
34+
* @param {string} options.entity The entity for the new access controls.
35+
* @param {string} options.role The role for the new access controls.
36+
* @param {string} [options.file] Optional. The file for the new access controls.
37+
* @param {boolean} [options.default] Optional. Whether to set default access controls.
38+
* @param {function} callback The callback function.
39+
*/
40+
function addAccessControl (options, callback) {
41+
// Reference the specified storage bucket
42+
var bucket = storage.bucket(options.bucket);
43+
// Reference the bucket's acl resource
44+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.add
45+
var acl = bucket.acl;
46+
47+
if (options.file) {
48+
// Optionally target a file's acl resource
49+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/file?method=acl.add
50+
acl = bucket.file(options.file).acl;
51+
} else if (options.default) {
52+
// Optionally add "default" access controls to the bucket
53+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.default.add
54+
acl = acl.default;
55+
}
56+
57+
// Specify the entity and role for the new access control object
58+
var config = {
59+
entity: options.entity,
60+
role: options.role
61+
};
62+
63+
acl.add(config, function (err, aclObject) {
64+
if (err) {
65+
return callback(err);
66+
}
67+
68+
console.log('Added access controls to: gs://%s' + (options.file ? '/%s' : ''), options.bucket, options.file || '');
69+
return callback(null, aclObject);
70+
});
71+
}
72+
// [END add_access_control]
73+
74+
// [START get_access_control]
75+
/**
76+
* Get access controls for a bucket or file.
77+
*
78+
* @param {object} options Configuration options.
79+
* @param {string} options.bucket The bucket to target.
80+
* @param {string} [options.entity] Optional. The entity to filter by.
81+
* @param {string} [options.file] Optional. The file to target.
82+
* @param {boolean} [options.default] Optional. Whether to get default access controls.
83+
* @param {function} callback The callback function.
84+
*/
85+
function getAccessControl (options, callback) {
86+
// Reference the specified storage bucket
87+
var bucket = storage.bucket(options.bucket);
88+
// Reference the bucket's acl resource
89+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.get
90+
var acl = bucket.acl;
91+
92+
if (options.file) {
93+
// Optionally target a file's acl resource
94+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/file?method=acl.get
95+
acl = bucket.file(options.file).acl;
96+
} else if (options.default) {
97+
// Optionally get "default" access controls for the bucket
98+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.default.get
99+
acl = acl.default;
100+
}
101+
102+
if (options.entity) {
103+
// Get a list of access controls for a specific entity
104+
acl.get({ entity: options.entity }, handleResponse);
105+
} else {
106+
// Get a list of all access controls
107+
acl.get(handleResponse);
108+
}
109+
110+
function handleResponse (err, aclObject) {
111+
if (err) {
112+
return callback(err);
113+
}
114+
115+
console.log('Got access controls for: gs://%s' + (options.file ? '/%s' : ''), options.bucket, options.file || '');
116+
return callback(null, aclObject);
117+
}
118+
}
119+
// [END get_access_control]
120+
121+
// [START delete_access_control]
122+
/**
123+
* Delete access controls from a bucket or file.
124+
*
125+
* @param {object} options Configuration options.
126+
* @param {string} options.bucket The bucket to target.
127+
* @param {string} options.entity The entity whose access is to be revoked.
128+
* @param {string} [options.file] Optional. The file to target.
129+
* @param {boolean} [options.default] Optional. Whether to delete default access controls.
130+
* @param {function} callback The callback function.
131+
*/
132+
function deleteAccessControl (options, callback) {
133+
// Reference the specified storage bucket
134+
var bucket = storage.bucket(options.bucket);
135+
// Reference the bucket's acl resource
136+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.delete
137+
var acl = bucket.acl;
138+
139+
if (options.file) {
140+
// Optionally target a file's acl resource
141+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/file?method=acl.delete
142+
acl = bucket.file(options.file).acl;
143+
} else if (options.default) {
144+
// Optionally delete "default" access controls from the bucket
145+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.default.delete
146+
acl = acl.default;
147+
}
148+
149+
// Delete access controls for a specific entity
150+
acl.delete({ entity: options.entity }, function (err) {
151+
if (err) {
152+
return callback(err);
153+
}
154+
155+
console.log('Deleted access controls from: gs://%s' + (options.file ? '/%s' : ''), options.bucket, options.file || '');
156+
return callback(null);
157+
});
158+
}
159+
// [END delete_access_control]
160+
// [END all]
161+
162+
// The command-line program
163+
var cli = require('yargs');
164+
165+
var program = module.exports = {
166+
addAccessControl: addAccessControl,
167+
getAccessControl: getAccessControl,
168+
deleteAccessControl: deleteAccessControl,
169+
main: function (args) {
170+
// Run the command-line program
171+
cli.help().strict().parse(args).argv;
172+
}
173+
};
174+
175+
cli
176+
.command('add <entity> <role>', 'Add access controls on a bucket or file.', {}, function (options) {
177+
program.addAccessControl(options, console.log);
178+
})
179+
.command('get [entity]', 'Get access controls on a bucket or file.', {}, function (options) {
180+
program.getAccessControl(options, console.log);
181+
})
182+
.command('delete <entity>', 'Delete access controls from a bucket or file.', {}, function (options) {
183+
program.deleteAccessControl(options, console.log);
184+
})
185+
.example('node $0 add [email protected] OWNER -b mybucket', 'Add OWNER access controls for "[email protected]" to "mybucket".')
186+
.example('node $0 add viewers-2256 WRITER -b mybucket -d', 'Add default WRITER access controls to "mybucket" for "viewers-2256".')
187+
.example('node $0 get editors-1234 -b mybucket', 'Get access controls for "editors-1234" in "mybucket".')
188+
.example('node $0 delete -b mybucket -f file.txt', 'Delete all access controls for all entities from "file.txt" in "mybucket".')
189+
.options({
190+
bucket: {
191+
alias: 'b',
192+
global: true,
193+
demand: true,
194+
requiresArg: true,
195+
type: 'string',
196+
description: 'The target storage bucket.'
197+
},
198+
default: {
199+
alias: 'd',
200+
global: true,
201+
type: 'boolean',
202+
description: 'Whether to set default access controls. Only valid when setting access controls on a bucket.'
203+
},
204+
file: {
205+
alias: 'f',
206+
global: true,
207+
requiresArg: true,
208+
type: 'string',
209+
description: 'The target file.'
210+
}
211+
})
212+
.wrap(100)
213+
.recommendCommands()
214+
.epilogue('For more information, see https://cloud.google.com/storage/docs/access-control/create-manage-lists');
215+
216+
if (module === require.main) {
217+
program.main(process.argv.slice(2));
218+
}

storage/iam.js

Whitespace-only changes.

storage/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"dependencies": {
1212
"@google-cloud/storage": "^0.1.1",
1313
"googleapis": "^12.2.0",
14-
"moment": "^2.14.1"
14+
"moment": "^2.14.1",
15+
"yargs": "^5.0.0"
1516
},
1617
"devDependencies": {
1718
"mocha": "^3.0.2",

0 commit comments

Comments
 (0)