Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions storage/addBucketDefaultOwnerAcl.js
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]
}
Comment on lines +25 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The main function is synchronous and calls the async function addBucketDefaultOwner without await. This creates a race condition where the script can terminate before the asynchronous operation completes. To fix this, main should be async and the logic can be simplified by removing the unnecessary inner function. Setting process.exitCode = 1 on error will also correctly signal failure to calling scripts.

async 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();

  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
    );
    process.exitCode = 1;
  }
  // [END storage_add_bucket_default_owner]
}

main(...process.argv.slice(2));
64 changes: 64 additions & 0 deletions storage/addBucketOwnerAcl.js
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The main function is synchronous and calls the async function addBucketOwner without await. This creates a race condition where the script can terminate before the asynchronous operation completes. To fix this, main should be async and the logic can be simplified by removing the unnecessary inner function. Setting process.exitCode = 1 on error will also correctly signal failure to calling scripts.

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));
68 changes: 68 additions & 0 deletions storage/addFileOwnerAcl.js
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The main function is synchronous and calls the async function addFileOwner without await. This creates a race condition where the script can terminate before the asynchronous operation completes. To fix this, main should be async and the logic can be simplified by removing the unnecessary inner function. Setting process.exitCode = 1 on error will also correctly signal failure to calling scripts.

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));
30 changes: 30 additions & 0 deletions storage/package.json
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"
}
}
58 changes: 58 additions & 0 deletions storage/printBucketAcl.js
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The main function is synchronous and calls the async function printBucketAcl without await. This creates a race condition where the script can terminate before the asynchronous operation completes. To fix this, main should be async and the logic can be simplified by removing the unnecessary inner function. Setting process.exitCode = 1 on error will also correctly signal failure to calling scripts.

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));
65 changes: 65 additions & 0 deletions storage/printBucketAclForUser.js
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The main function is synchronous and calls the async function printBucketAclForUser without await. This creates a race condition where the script can terminate before the asynchronous operation completes. To fix this, main should be async and the logic can be simplified by removing the unnecessary inner function. Setting process.exitCode = 1 on error will also correctly signal failure to calling scripts.

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));
Loading
Loading