|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +/** |
| 18 | + * This application demonstrates how to perform basic operations on bucket and |
| 19 | + * file Access Control Lists with the Google Cloud Storage API. |
| 20 | + * |
| 21 | + * For more information, see the README.md under /storage and the documentation |
| 22 | + * at https://cloud.google.com/storage/docs. |
| 23 | + */ |
| 24 | + |
| 25 | +function main(bucketName = 'my-bucket', userEmail = 'jdobry@google.com') { |
| 26 | + // [START storage_add_bucket_default_owner] |
| 27 | + /** |
| 28 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 29 | + */ |
| 30 | + // The ID of your GCS bucket |
| 31 | + // const bucketName = 'your-unique-bucket-name'; |
| 32 | + |
| 33 | + // The email address of the user to add |
| 34 | + // const userEmail = 'user-email-to-add'; |
| 35 | + |
| 36 | + // Imports the Google Cloud client library |
| 37 | + const {Storage} = require('@google-cloud/storage'); |
| 38 | + |
| 39 | + // Creates a client |
| 40 | + const storage = new Storage(); |
| 41 | + |
| 42 | + async function addBucketDefaultOwner() { |
| 43 | + try { |
| 44 | + // Makes the user an owner in the default ACL of the bucket. You can use |
| 45 | + // addAllUsers(), addDomain(), addProject(), addGroup(), and |
| 46 | + // addAllAuthenticatedUsers() to grant access to different types of entities. |
| 47 | + // You can also use "readers" and "writers" to grant different roles. |
| 48 | + await storage.bucket(bucketName).acl.default.owners.addUser(userEmail); |
| 49 | + |
| 50 | + console.log( |
| 51 | + `Added user ${userEmail} as an owner on bucket ${bucketName}.` |
| 52 | + ); |
| 53 | + } catch (error) { |
| 54 | + console.error( |
| 55 | + 'Error executing add bucket default owner ACL:', |
| 56 | + error.message || error |
| 57 | + ); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + addBucketDefaultOwner(); |
| 62 | + // [END storage_add_bucket_default_owner] |
| 63 | +} |
| 64 | +main(...process.argv.slice(2)); |
0 commit comments