diff --git a/storage/addBucketDefaultOwnerAcl.js b/storage/addBucketDefaultOwnerAcl.js new file mode 100644 index 0000000000..38431f62a0 --- /dev/null +++ b/storage/addBucketDefaultOwnerAcl.js @@ -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)); diff --git a/storage/addBucketOwnerAcl.js b/storage/addBucketOwnerAcl.js new file mode 100644 index 0000000000..b3ee2f0844 --- /dev/null +++ b/storage/addBucketOwnerAcl.js @@ -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] +} +main(...process.argv.slice(2)); diff --git a/storage/addFileOwnerAcl.js b/storage/addFileOwnerAcl.js new file mode 100644 index 0000000000..a908a03e9c --- /dev/null +++ b/storage/addFileOwnerAcl.js @@ -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] +} +main(...process.argv.slice(2)); diff --git a/storage/package.json b/storage/package.json new file mode 100644 index 0000000000..71acc18ab5 --- /dev/null +++ b/storage/package.json @@ -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" + } +} diff --git a/storage/printBucketAcl.js b/storage/printBucketAcl.js new file mode 100644 index 0000000000..c14e682e16 --- /dev/null +++ b/storage/printBucketAcl.js @@ -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] +} + +main(...process.argv.slice(2)); diff --git a/storage/printBucketAclForUser.js b/storage/printBucketAclForUser.js new file mode 100644 index 0000000000..27420478fd --- /dev/null +++ b/storage/printBucketAclForUser.js @@ -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] +} + +main(...process.argv.slice(2)); diff --git a/storage/printFileAcl.js b/storage/printFileAcl.js new file mode 100644 index 0000000000..0d9ead9b18 --- /dev/null +++ b/storage/printFileAcl.js @@ -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] +} +main(...process.argv.slice(2)); diff --git a/storage/printFileAclForUser.js b/storage/printFileAclForUser.js new file mode 100644 index 0000000000..396cb393ba --- /dev/null +++ b/storage/printFileAclForUser.js @@ -0,0 +1,74 @@ +// 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_print_file_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 ID of your GCS file + // const fileName = 'your-file-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 printFileAclForUser() { + try { + const options = { + // Specify the user + entity: `user-${userEmail}`, + }; + + // Gets the user's ACL for the file + const [aclObject] = await storage + .bucket(bucketName) + .file(fileName) + .acl.get(options); + + console.log(`${aclObject.role}: ${aclObject.entity}`); + } catch (error) { + console.error( + 'Error executing print file ACL for user:', + error.message || error + ); + } + } + + printFileAclForUser(); + // [END storage_print_file_acl_for_user] +} +main(...process.argv.slice(2)); diff --git a/storage/removeBucketDefaultOwner.js b/storage/removeBucketDefaultOwner.js new file mode 100644 index 0000000000..51087943ff --- /dev/null +++ b/storage/removeBucketDefaultOwner.js @@ -0,0 +1,62 @@ +// 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_remove_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 remove + // const userEmail = 'user-email-to-remove'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function removeBucketDefaultOwner() { + try { + // Removes the user from the access control list of the bucket. You can use + // deleteAllUsers(), deleteDomain(), deleteProject(), deleteGroup(), and + // deleteAllAuthenticatedUsers() to remove access for different types of entities. + await storage.bucket(bucketName).acl.default.owners.deleteUser(userEmail); + + console.log(`Removed user ${userEmail} from bucket ${bucketName}.`); + } catch (error) { + console.error( + 'Error executing remove bucket default owner ACL:', + error.message || error + ); + } + } + + removeBucketDefaultOwner(); + // [END storage_remove_bucket_default_owner] +} + +main(...process.argv.slice(2)); diff --git a/storage/removeBucketOwnerAcl.js b/storage/removeBucketOwnerAcl.js new file mode 100644 index 0000000000..2ee60765a8 --- /dev/null +++ b/storage/removeBucketOwnerAcl.js @@ -0,0 +1,63 @@ +// 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_remove_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 remove + // const userEmail = 'user-email-to-remove'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function removeBucketOwner() { + try { + // Removes the user from the access control list of the bucket. You can use + // deleteAllUsers(), deleteDomain(), deleteProject(), deleteGroup(), and + // deleteAllAuthenticatedUsers() to remove access for different types of entities. + await storage.bucket(bucketName).acl.owners.deleteUser(userEmail); + + console.log(`Removed user ${userEmail} from bucket ${bucketName}.`); + } catch (error) { + console.error( + 'Error executing remove bucket owner ACL:', + error.message || error + ); + } + } + + removeBucketOwner(); + + // [END storage_remove_bucket_owner] +} + +main(...process.argv.slice(2)); diff --git a/storage/removeFileOwnerAcl.js b/storage/removeFileOwnerAcl.js new file mode 100644 index 0000000000..ccd2fec08a --- /dev/null +++ b/storage/removeFileOwnerAcl.js @@ -0,0 +1,72 @@ +// 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_remove_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 ID of your GCS file + // const fileName = 'your-file-name'; + + // The email address of the user to remove + // const userEmail = 'user-email-to-remove'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function removeFileOwner() { + try { + // Removes the user from the access control list of the file. You can use + // deleteAllUsers(), deleteDomain(), deleteProject(), deleteGroup(), and + // deleteAllAuthenticatedUsers() to remove access for different types of entities. + await storage + .bucket(bucketName) + .file(fileName) + .acl.owners.deleteUser(userEmail); + + console.log(`Removed user ${userEmail} from file ${fileName}.`); + } catch (error) { + console.error( + 'Error executing remove file owner ACL:', + error.message || error + ); + } + } + + removeFileOwner(); + // [END storage_remove_file_owner] +} + +main(...process.argv.slice(2)); diff --git a/storage/resources/.gitignore b/storage/resources/.gitignore new file mode 100644 index 0000000000..6738013702 --- /dev/null +++ b/storage/resources/.gitignore @@ -0,0 +1 @@ +downloaded.txt diff --git a/storage/resources/resourcesSub1/testSub1.txt b/storage/resources/resourcesSub1/testSub1.txt new file mode 100644 index 0000000000..51f4b307d5 --- /dev/null +++ b/storage/resources/resourcesSub1/testSub1.txt @@ -0,0 +1,2 @@ +Sub1 +Hello World! \ No newline at end of file diff --git a/storage/resources/test.txt b/storage/resources/test.txt new file mode 100644 index 0000000000..c57eff55eb --- /dev/null +++ b/storage/resources/test.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/storage/resources/test2.txt b/storage/resources/test2.txt new file mode 100644 index 0000000000..010302410b --- /dev/null +++ b/storage/resources/test2.txt @@ -0,0 +1 @@ +Hello World 2! \ No newline at end of file diff --git a/storage/scripts/cleanup b/storage/scripts/cleanup new file mode 100644 index 0000000000..61bd73114f --- /dev/null +++ b/storage/scripts/cleanup @@ -0,0 +1,44 @@ +#!/usr/bin/env node + +// Copyright 2019 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'; + +const {Storage} = require('@google-cloud/storage'); +const storage = new Storage(); +const NAME_REG_EXP = /^nodejs-storage-samples-[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/; + +storage + .getBuckets() + .then(([buckets]) => { + let promise = Promise.resolve(); + + buckets + .filter((bucket) => NAME_REG_EXP.test(bucket.name)) + .forEach((bucket) => { + promise = promise.then(() => { + return bucket.deleteFiles() + .then(() => bucket.deleteFiles(), console.error) + .then(() => { + console.log(`Deleting ${bucket.name}`); + return bucket.delete(); + }, console.error) + .catch(console.error); + }); + }); + }) + .catch((err) => { + console.error('ERROR:', err); + }); diff --git a/storage/system-test/acl.test.js b/storage/system-test/acl.test.js new file mode 100644 index 0000000000..5074b04b76 --- /dev/null +++ b/storage/system-test/acl.test.js @@ -0,0 +1,144 @@ +// Copyright 2019 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'; + +const {Storage} = require('@google-cloud/storage'); +const {assert} = require('chai'); +const {before, after, it} = require('mocha'); +const cp = require('child_process'); +const uuid = require('uuid'); +const path = require('path'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const storage = new Storage(); +const bucketName = `nodejs-storage-samples-${uuid.v4()}`; +const bucket = storage.bucket(bucketName); +const userEmail = 'jdobry@google.com'; +const fileName = 'test.txt'; +const filePath = path.join(__dirname, '..', 'resources', fileName); + +before(async () => { + await bucket.create(); + await bucket.upload(filePath); +}); + +after(async () => { + try { + await bucket.deleteFiles({force: true}); + } catch (err) { + // ignore error + } + try { + await bucket.deleteFiles({force: true}); + } catch (err) { + // ignore error + } + try { + await bucket.delete(); + } catch (err) { + // ignore error + } +}); + +it('should print acl for a bucket', () => { + const out = execSync(`node printBucketAcl.js ${bucketName}`); + assert.match(out, /OWNER: project-editors-/); + assert.match(out, /OWNER: project-owners-/); + assert.match(out, /READER: project-viewers-/); +}); + +it('should print acl for a file', () => { + const out = execSync(`node printFileAcl.js ${bucketName} ${fileName}`); + assert.match(out, /OWNER: project-editors-/); + assert.match(out, /OWNER: project-owners-/); + assert.match(out, /READER: project-viewers-/); +}); + +it('should print a users acl for a bucket', async () => { + await bucket.acl.readers.addUser(userEmail); + const out = execSync( + `node printBucketAclForUser.js ${bucketName} ${userEmail}` + ); + assert.match(out, new RegExp(`READER: user-${userEmail}`)); + await bucket.acl.readers.deleteUser(userEmail); +}); + +it('should add a user as an owner on a bucket', () => { + const out = execSync(`node addBucketOwnerAcl.js ${bucketName} ${userEmail}`); + assert.match( + out, + new RegExp(`Added user ${userEmail} as an owner on bucket ${bucketName}.`) + ); +}); + +it('should remove a user from a bucket', () => { + const out = execSync( + `node removeBucketOwnerAcl.js ${bucketName} ${userEmail}` + ); + assert.match( + out, + new RegExp(`Removed user ${userEmail} from bucket ${bucketName}.`) + ); +}); + +it('should add a user as a default owner on a bucket', () => { + const out = execSync( + `node addBucketDefaultOwnerAcl.js ${bucketName} ${userEmail}` + ); + assert.match( + out, + new RegExp(`Added user ${userEmail} as an owner on bucket ${bucketName}.`) + ); +}); + +it('should remove a default user from a bucket', () => { + const out = execSync( + `node removeBucketDefaultOwner.js ${bucketName} ${userEmail}` + ); + assert.match( + out, + new RegExp(`Removed user ${userEmail} from bucket ${bucketName}.`) + ); +}); + +it('should print a users acl for a file', async () => { + await bucket.file(fileName).acl.readers.addUser(userEmail); + const out = execSync( + `node printFileAclForUser.js ${bucketName} ${fileName} ${userEmail}` + ); + assert.match(out, new RegExp(`READER: user-${userEmail}`)); + await bucket.file(fileName).acl.readers.deleteUser(userEmail); +}); + +it('should add a user as an owner on a bucket', () => { + const out = execSync( + `node addFileOwnerAcl.js ${bucketName} ${fileName} ${userEmail}` + ); + assert.match( + out, + new RegExp(`Added user ${userEmail} as an owner on file ${fileName}.`) + ); +}); + +it('should remove a user from a bucket', () => { + const out = execSync( + `node removeFileOwnerAcl.js ${bucketName} ${fileName} ${userEmail}` + ); + assert.match( + out, + new RegExp(`Removed user ${userEmail} from file ${fileName}.`) + ); +}); diff --git a/storage/system-test/test_9d800329-00da-4cdd-9a3e-7ac6743d5813.txt b/storage/system-test/test_9d800329-00da-4cdd-9a3e-7ac6743d5813.txt new file mode 100644 index 0000000000..e69de29bb2