AWS 文档 SDK 示例
将 DetachPolicy 与 AWS SDK 或 CLI 配合使用
以下代码示例演示如何使用 DetachPolicy。
- .NET
-
- 适用于 .NET 的 SDK
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 using System; using System.Threading.Tasks; using Amazon.Organizations; using Amazon.Organizations.Model; /// <summary> /// Shows how to detach a policy from an AWS Organizations organization, /// organizational unit, or account. /// </summary> public class DetachPolicy { /// <summary> /// Initializes the Organizations client object and uses it to call /// DetachPolicyAsync to detach the policy. /// </summary> public static async Task Main() { // Create the client object using the default account. IAmazonOrganizations client = new AmazonOrganizationsClient(); var policyId = "p-00000000"; var targetId = "r-0000"; var request = new DetachPolicyRequest { PolicyId = policyId, TargetId = targetId, }; var response = await client.DetachPolicyAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully detached policy with Policy Id: {policyId}."); } else { Console.WriteLine("Could not detach the policy."); } } }-
有关 API 的详细信息,请参阅《适用于 .NET 的 AWS SDK API Reference》中的 DetachPolicy。
-
- CLI
-
- AWS CLI
-
从根、OU 或账户分离策略
以下示例演示了如何从 OU 分离策略:
aws organizations detach-policy --target-idou-examplerootid111-exampleouid111--policy-idp-examplepolicyid111-
有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DetachPolicy
。
-
- Python
-
- 适用于 Python 的 SDK (Boto3)
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 def detach_policy(policy_id, target_id, orgs_client): """ Detaches a policy from a target. :param policy_id: The ID of the policy to detach. :param target_id: The ID of the resource where the policy is currently attached. :param orgs_client: The Boto3 Organizations client. """ try: orgs_client.detach_policy(PolicyId=policy_id, TargetId=target_id) logger.info("Detached policy %s from target %s.", policy_id, target_id) except ClientError: logger.exception( "Couldn't detach policy %s from target %s.", policy_id, target_id ) raise-
有关 API 详细信息,请参阅《AWS SDK for Python (Boto3) API Reference》中的 DetachPolicy。
-
DescribePolicy
ListAccounts