-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(storage): migrate ACL samples and tests #4260
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
Changes from all commits
dd5f514
362130e
ba49dd3
06783ba
f69aa7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // 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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on bucket and | ||
| * file Access Control Lists with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the README.md under /storage and the documentation | ||
| * at https://cloud.google.com/storage/docs. | ||
| */ | ||
|
|
||
| function main(bucketName = 'my-bucket', userEmail = 'jdobry@google.com') { | ||
| // [START storage_add_bucket_default_owner] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The email address of the user to add | ||
| // const userEmail = 'user-email-to-add'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function addBucketDefaultOwner() { | ||
| try { | ||
| // Makes the user an owner in the default ACL of the bucket. You can use | ||
| // addAllUsers(), addDomain(), addProject(), addGroup(), and | ||
| // addAllAuthenticatedUsers() to grant access to different types of entities. | ||
| // You can also use "readers" and "writers" to grant different roles. | ||
| await storage.bucket(bucketName).acl.default.owners.addUser(userEmail); | ||
|
|
||
| console.log( | ||
| `Added user ${userEmail} as an owner on bucket ${bucketName}.` | ||
| ); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing add bucket default owner ACL:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| addBucketDefaultOwner(); | ||
| // [END storage_add_bucket_default_owner] | ||
| } | ||
| main(...process.argv.slice(2)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // 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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on bucket and | ||
| * file Access Control Lists with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the README.md under /storage and the documentation | ||
| * at https://cloud.google.com/storage/docs. | ||
| */ | ||
|
|
||
| function main(bucketName = 'my-bucket', userEmail = 'jdobry@google.com') { | ||
| // [START storage_add_bucket_owner] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The email address of the user to add | ||
| // const userEmail = 'user-email-to-add'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function addBucketOwner() { | ||
| try { | ||
| // Makes the user an owner of the bucket. You can use addAllUsers(), | ||
| // addDomain(), addProject(), addGroup(), and addAllAuthenticatedUsers() | ||
| // to grant access to different types of entities. You can also use "readers" | ||
| // and "writers" to grant different roles. | ||
| await storage.bucket(bucketName).acl.owners.addUser(userEmail); | ||
|
|
||
| console.log( | ||
| `Added user ${userEmail} as an owner on bucket ${bucketName}.` | ||
| ); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing add bucket owner ACL:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| addBucketOwner(); | ||
| // [END storage_add_bucket_owner] | ||
| } | ||
angelcaamal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| main(...process.argv.slice(2)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // 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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on bucket and | ||
| * file Access Control Lists with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the README.md under /storage and the documentation | ||
| * at https://cloud.google.com/storage/docs. | ||
| */ | ||
|
|
||
| function main( | ||
| bucketName = 'my-bucket', | ||
| fileName = 'test.txt', | ||
| userEmail = 'jdobry@google.com' | ||
| ) { | ||
| // [START storage_add_file_owner] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The name of the file to access | ||
| // const fileName = 'file.txt'; | ||
|
|
||
| // The email address of the user to add | ||
| // const userEmail = 'user-email-to-add'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function addFileOwner() { | ||
| try { | ||
| await storage | ||
| .bucket(bucketName) | ||
| .file(fileName) | ||
| .acl.owners.addUser(userEmail); | ||
|
|
||
| console.log(`Added user ${userEmail} as an owner on file ${fileName}.`); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing add file owner ACL:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| addFileOwner(); | ||
| // [END storage_add_file_owner] | ||
| } | ||
angelcaamal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| main(...process.argv.slice(2)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // 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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on bucket and | ||
| * file Access Control Lists with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the README.md under /storage and the documentation | ||
| * at https://cloud.google.com/storage/docs. | ||
| */ | ||
|
|
||
| function main(bucketName = 'my-bucket') { | ||
| // [START storage_print_bucket_acl] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function printBucketAcl() { | ||
| try { | ||
| // Gets the ACL for the bucket | ||
| const [acls] = await storage.bucket(bucketName).acl.get(); | ||
|
|
||
| acls.forEach(acl => { | ||
| console.log(`${acl.role}: ${acl.entity}`); | ||
| }); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing print bucket ACL:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
| printBucketAcl(); | ||
| // [END storage_print_bucket_acl] | ||
| } | ||
angelcaamal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| main(...process.argv.slice(2)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // 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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on bucket and | ||
| * file Access Control Lists with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the README.md under /storage and the documentation | ||
| * at https://cloud.google.com/storage/docs. | ||
| */ | ||
|
|
||
| function main(bucketName = 'my-bucket', userEmail = 'jdobry@google.com') { | ||
| // [START storage_print_bucket_acl_for_user] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The email address of the user to check | ||
| // const userEmail = 'user-email-to-check'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function printBucketAclForUser() { | ||
| try { | ||
| const options = { | ||
| // Specify the user | ||
| entity: `user-${userEmail}`, | ||
| }; | ||
|
|
||
| // Gets the user's ACL for the bucket | ||
| const [aclObject] = await storage.bucket(bucketName).acl.get(options); | ||
|
|
||
| console.log(`${aclObject.role}: ${aclObject.entity}`); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing print bucket ACL for user:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| printBucketAclForUser(); | ||
| // [END storage_print_bucket_acl_for_user] | ||
| } | ||
angelcaamal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| main(...process.argv.slice(2)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // 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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on bucket and | ||
| * file Access Control Lists with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the README.md under /storage and the documentation | ||
| * at https://cloud.google.com/storage/docs. | ||
| */ | ||
|
|
||
| function main(bucketName = 'my-bucket', fileName = 'test.txt') { | ||
| // [START storage_print_file_acl] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The ID of your GCS file | ||
| // const fileName = 'your-file-name'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function printFileAcl() { | ||
| try { | ||
| // Gets the ACL for the file | ||
| const [acls] = await storage.bucket(bucketName).file(fileName).acl.get(); | ||
|
|
||
| acls.forEach(acl => { | ||
| console.log(`${acl.role}: ${acl.entity}`); | ||
| }); | ||
| } catch (error) { | ||
| console.error('Error executing print file ACL:', error.message || error); | ||
| } | ||
| } | ||
|
|
||
| printFileAcl(); | ||
| // [END storage_print_file_acl] | ||
| } | ||
|
Comment on lines
+25
to
+57
Contributor
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 async function main(bucketName = 'my-bucket', fileName = 'test.txt') {
// [START storage_print_file_acl]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';
// The ID of your GCS file
// const fileName = 'your-file-name';
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
try {
// Gets the ACL for the file
const [acls] = await storage.bucket(bucketName).file(fileName).acl.get();
acls.forEach(acl => {
console.log(`${acl.role}: ${acl.entity}`);
});
} catch (error) {
console.error('Error executing print file ACL:', error.message || error);
process.exitCode = 1;
}
// [END storage_print_file_acl]
} |
||
| main(...process.argv.slice(2)); | ||
Uh oh!
There was an error while loading. Please reload this page.