-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathput-bucket-acl.js
More file actions
105 lines (99 loc) · 3.05 KB
/
put-bucket-acl.js
File metadata and controls
105 lines (99 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[s3.JavaScript.perms.putBucketAclV3]
import {
PutBucketAclCommand,
S3Client,
S3ServiceException,
} from "@aws-sdk/client-s3";
/**
* Grant read access to a user using their canonical AWS account ID.
*
* Most Amazon S3 use cases don't require the use of access control lists (ACLs).
* We recommend that you disable ACLs, except in unusual circumstances where
* you need to control access for each object individually. Consider a policy instead.
* For more information see https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucket-policies.html.
* @param {{ bucketName: string, granteeCanonicalUserId: string, ownerCanonicalUserId }}
*/
export const main = async ({
bucketName,
granteeCanonicalUserId,
ownerCanonicalUserId,
}) => {
const client = new S3Client({});
const command = new PutBucketAclCommand({
Bucket: bucketName,
AccessControlPolicy: {
Grants: [
{
Grantee: {
// The canonical ID of the user. This ID is an obfuscated form of your AWS account number.
// It's unique to Amazon S3 and can't be found elsewhere.
// For more information, see https://docs.aws.amazon.com/AmazonS3/latest/userguide/finding-canonical-user-id.html.
ID: granteeCanonicalUserId,
Type: "CanonicalUser",
},
// One of FULL_CONTROL | READ | WRITE | READ_ACP | WRITE_ACP
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_Grant.html#AmazonS3-Type-Grant-Permission
Permission: "READ",
},
],
Owner: {
ID: ownerCanonicalUserId,
},
},
});
try {
await client.send(command);
console.log(`Granted READ access to ${bucketName}`);
} catch (caught) {
if (
caught instanceof S3ServiceException &&
caught.name === "NoSuchBucket"
) {
console.error(
`Error from S3 while setting ACL for bucket ${bucketName}. The bucket doesn't exist.`,
);
} else if (caught instanceof S3ServiceException) {
console.error(
`Error from S3 while setting ACL for bucket ${bucketName}. ${caught.name}: ${caught.message}`,
);
} else {
throw caught;
}
}
};
// snippet-end:[s3.JavaScript.perms.putBucketAclV3]
// Call function if run directly
import { parseArgs } from "node:util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";
const loadArgs = () => {
const options = {
bucketName: {
type: "string",
required: true,
},
granteeCanonicalUserId: {
type: "string",
required: true,
},
ownerCanonicalUserId: {
type: "string",
required: true,
},
};
const results = parseArgs({ options });
const { errors } = validateArgs({ options }, results);
return { errors, results };
};
if (isMain(import.meta.url)) {
const { errors, results } = loadArgs();
if (!errors) {
main(results.values);
} else {
console.error(errors.join("\n"));
}
}