Skip to content

Commit 24d8a8a

Browse files
angelcaamaliennae
andauthored
feat(storage): migrate ACL samples and tests (#4260)
* feat(storage): introduce ACL samples and tests for migration * test(storage): fix ACL test cases --------- Co-authored-by: Jennifer Davis <sigje@google.com>
1 parent 74b581d commit 24d8a8a

11 files changed

Lines changed: 787 additions & 0 deletions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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));

storage/addBucketOwnerAcl.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_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 addBucketOwner() {
43+
try {
44+
// Makes the user an owner of the bucket. You can use addAllUsers(),
45+
// addDomain(), addProject(), addGroup(), and addAllAuthenticatedUsers()
46+
// to grant access to different types of entities. You can also use "readers"
47+
// and "writers" to grant different roles.
48+
await storage.bucket(bucketName).acl.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 owner ACL:',
56+
error.message || error
57+
);
58+
}
59+
}
60+
61+
addBucketOwner();
62+
// [END storage_add_bucket_owner]
63+
}
64+
main(...process.argv.slice(2));

storage/addFileOwnerAcl.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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(
26+
bucketName = 'my-bucket',
27+
fileName = 'test.txt',
28+
userEmail = 'jdobry@google.com'
29+
) {
30+
// [START storage_add_file_owner]
31+
/**
32+
* TODO(developer): Uncomment the following lines before running the sample.
33+
*/
34+
// The ID of your GCS bucket
35+
// const bucketName = 'your-unique-bucket-name';
36+
37+
// The name of the file to access
38+
// const fileName = 'file.txt';
39+
40+
// The email address of the user to add
41+
// const userEmail = 'user-email-to-add';
42+
43+
// Imports the Google Cloud client library
44+
const {Storage} = require('@google-cloud/storage');
45+
46+
// Creates a client
47+
const storage = new Storage();
48+
49+
async function addFileOwner() {
50+
try {
51+
await storage
52+
.bucket(bucketName)
53+
.file(fileName)
54+
.acl.owners.addUser(userEmail);
55+
56+
console.log(`Added user ${userEmail} as an owner on file ${fileName}.`);
57+
} catch (error) {
58+
console.error(
59+
'Error executing add file owner ACL:',
60+
error.message || error
61+
);
62+
}
63+
}
64+
65+
addFileOwner();
66+
// [END storage_add_file_owner]
67+
}
68+
main(...process.argv.slice(2));

storage/printBucketAcl.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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') {
26+
// [START storage_print_bucket_acl]
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+
// Imports the Google Cloud client library
34+
const {Storage} = require('@google-cloud/storage');
35+
36+
// Creates a client
37+
const storage = new Storage();
38+
39+
async function printBucketAcl() {
40+
try {
41+
// Gets the ACL for the bucket
42+
const [acls] = await storage.bucket(bucketName).acl.get();
43+
44+
acls.forEach(acl => {
45+
console.log(`${acl.role}: ${acl.entity}`);
46+
});
47+
} catch (error) {
48+
console.error(
49+
'Error executing print bucket ACL:',
50+
error.message || error
51+
);
52+
}
53+
}
54+
printBucketAcl();
55+
// [END storage_print_bucket_acl]
56+
}
57+
58+
main(...process.argv.slice(2));

storage/printBucketAclForUser.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_print_bucket_acl_for_user]
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 check
34+
// const userEmail = 'user-email-to-check';
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 printBucketAclForUser() {
43+
try {
44+
const options = {
45+
// Specify the user
46+
entity: `user-${userEmail}`,
47+
};
48+
49+
// Gets the user's ACL for the bucket
50+
const [aclObject] = await storage.bucket(bucketName).acl.get(options);
51+
52+
console.log(`${aclObject.role}: ${aclObject.entity}`);
53+
} catch (error) {
54+
console.error(
55+
'Error executing print bucket ACL for user:',
56+
error.message || error
57+
);
58+
}
59+
}
60+
61+
printBucketAclForUser();
62+
// [END storage_print_bucket_acl_for_user]
63+
}
64+
65+
main(...process.argv.slice(2));

storage/printFileAcl.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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', fileName = 'test.txt') {
26+
// [START storage_print_file_acl]
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 ID of your GCS file
34+
// const fileName = 'your-file-name';
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 printFileAcl() {
43+
try {
44+
// Gets the ACL for the file
45+
const [acls] = await storage.bucket(bucketName).file(fileName).acl.get();
46+
47+
acls.forEach(acl => {
48+
console.log(`${acl.role}: ${acl.entity}`);
49+
});
50+
} catch (error) {
51+
console.error('Error executing print file ACL:', error.message || error);
52+
}
53+
}
54+
55+
printFileAcl();
56+
// [END storage_print_file_acl]
57+
}
58+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)