MCPcopy Create free account
hub / github.com/awsdocs/aws-doc-sdk-examples / main

Function main

javascriptv3/example_code/s3/actions/put-bucket-acl.js:20–68  ·  view source on GitHub ↗
({
  bucketName,
  granteeCanonicalUserId,
  ownerCanonicalUserId,
})

Source from the content-addressed store, hash-verified

18 * @param {{ bucketName: string, granteeCanonicalUserId: string, ownerCanonicalUserId }}
19 */
20export const main = async ({
21 bucketName,
22 granteeCanonicalUserId,
23 ownerCanonicalUserId,
24}) => {
25 const client = new S3Client({});
26 const command = new PutBucketAclCommand({
27 Bucket: bucketName,
28 AccessControlPolicy: {
29 Grants: [
30 {
31 Grantee: {
32 // The canonical ID of the user. This ID is an obfuscated form of your AWS account number.
33 // It's unique to Amazon S3 and can't be found elsewhere.
34 // For more information, see https://docs.aws.amazon.com/AmazonS3/latest/userguide/finding-canonical-user-id.html.
35 ID: granteeCanonicalUserId,
36 Type: "CanonicalUser",
37 },
38 // One of FULL_CONTROL | READ | WRITE | READ_ACP | WRITE_ACP
39 // https://docs.aws.amazon.com/AmazonS3/latest/API/API_Grant.html#AmazonS3-Type-Grant-Permission
40 Permission: "READ",
41 },
42 ],
43 Owner: {
44 ID: ownerCanonicalUserId,
45 },
46 },
47 });
48
49 try {
50 await client.send(command);
51 console.log(`Granted READ access to ${bucketName}`);
52 } catch (caught) {
53 if (
54 caught instanceof S3ServiceException &&
55 caught.name === "NoSuchBucket"
56 ) {
57 console.error(
58 `Error from S3 while setting ACL for bucket ${bucketName}. The bucket doesn't exist.`,
59 );
60 } else if (caught instanceof S3ServiceException) {
61 console.error(
62 `Error from S3 while setting ACL for bucket ${bucketName}. ${caught.name}: ${caught.message}`,
63 );
64 } else {
65 throw caught;
66 }
67 }
68};
69// snippet-end:[s3.JavaScript.perms.putBucketAclV3]
70
71// Call function if run directly

Callers 1

put-bucket-acl.jsFile · 0.70

Calls 2

sendMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected