-
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
base: main
Are you sure you want to change the base?
Changes from all commits
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] | ||
| } | ||
|
Comment on lines
+25
to
+63
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', 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();
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
);
process.exitCode = 1;
}
// [END storage_add_bucket_owner]
} |
||
| 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] | ||
| } | ||
|
Comment on lines
+25
to
+67
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',
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();
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
);
process.exitCode = 1;
}
// [END storage_add_file_owner]
} |
||
| main(...process.argv.slice(2)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "name": "@google-cloud/storage-samples", | ||
| "description": "Samples for the Cloud Storage Client Library for Node.js.", | ||
| "license": "Apache-2.0", | ||
| "author": "Google Inc.", | ||
| "engines": { | ||
| "node": ">=12" | ||
| }, | ||
| "repository": "googleapis/nodejs-storage", | ||
| "private": true, | ||
| "files": [ | ||
| "*.js" | ||
| ], | ||
| "scripts": { | ||
| "cleanup": "node scripts/cleanup", | ||
| "test": "mocha system-test/*.js --timeout 800000" | ||
| }, | ||
| "dependencies": { | ||
| "@google-cloud/pubsub": "^4.0.0", | ||
| "@google-cloud/storage": "^7.19.0", | ||
| "node-fetch": "^2.6.7", | ||
| "uuid": "^8.0.0", | ||
| "yargs": "^16.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "chai": "^4.2.0", | ||
| "mocha": "^8.0.0", | ||
| "p-limit": "^3.1.0" | ||
| } | ||
| } |
| 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] | ||
| } | ||
|
Comment on lines
+25
to
+56
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') {
// [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();
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
);
process.exitCode = 1;
}
// [END storage_print_bucket_acl]
} |
||
|
|
||
| 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] | ||
| } | ||
|
Comment on lines
+25
to
+63
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', 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();
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
);
process.exitCode = 1;
}
// [END storage_print_bucket_acl_for_user]
} |
||
|
|
||
| main(...process.argv.slice(2)); | ||
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.
The
mainfunction is synchronous and calls theasyncfunctionaddBucketDefaultOwnerwithoutawait. This creates a race condition where the script can terminate before the asynchronous operation completes. To fix this,mainshould beasyncand the logic can be simplified by removing the unnecessary inner function. Settingprocess.exitCode = 1on error will also correctly signal failure to calling scripts.