addToPrincipalPolicy和addToPolicy都是IAM(身份和访问管理)API中的方法,用于向IAM策略添加权限。它们的区别在于:
addToPrincipalPolicy:将权限添加到IAM策略中指定的角色或用户的权限策略中。
示例代码:
iam = boto3.client('iam') role_name = 'example_role' policy_arn = 'arn:aws:iam::aws:policy/ReadOnlyAccess'
response = iam.attach_role_policy(RoleName=role_name, PolicyArn=policy_arn)
addToPolicy:将权限添加到IAM策略本身中。
示例代码:
iam = boto3.client('iam') policy_arn = 'arn:aws:iam::aws:policy/CustomPolicy'
policy_document = { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "", "Resource": "" } ] } response = iam.create_policy( PolicyName='example_policy', PolicyDocument=json.dumps(policy_document) )
response = iam.attach_user_policy( UserName='example_user', PolicyArn=policy_arn )