使用 Amazon 开发工具包获取 IAM policy - Amazon Identity and Access Management
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon 开发工具包获取 IAM policy

以下代码示例显示如何获取 IAM policy。

.NET
Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅 GitHub

using System; using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; var client = new AmazonIdentityManagementServiceClient(); var request = new GetPolicyRequest { PolicyArn = "POLICY_ARN", }; var response = await client.GetPolicyAsync(request); Console.Write($"{response.Policy.PolicyName} was created on "); Console.WriteLine($"{response.Policy.CreateDate}");
  • 有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 GetPolicy

Go
SDK for Go V2
提示

要了解如何设置和运行此示例,请参阅 GitHub

// GetPolicy getPolicyResponse, err := service.GetPolicy(context.Background(), &iam.GetPolicyInput{ PolicyArn: policyArn, }) if err != nil { panic("Couldn't get policy from ARN: " + err.Error()) } fmt.Printf("policy: %s, name %s\n", *getPolicyResponse.Policy.Arn, *getPolicyResponse.Policy.PolicyName)
  • 有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 GetPolicy

JavaScript
SDK for JavaScript V3
提示

要了解如何设置和运行此示例,请参阅 GitHub

创建客户端。

import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; // For example, "us-east-1". // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };

获取策略。

// Import required AWS SDK clients and commands for Node.js. import { iamClient } from "./libs/iamClient.js"; import { GetPolicyCommand } from "@aws-sdk/client-iam"; // Set the parameters. const params = { PolicyArn: "POLICY_ARN" /* required */, }; const run = async () => { try { const data = await iamClient.send(new GetPolicyCommand(params)); console.log("Success", data.Policy); } catch (err) { console.log("Error", err); } }; run();
SDK for JavaScript V2
提示

要了解如何设置和运行此示例,请参阅 GitHub

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the IAM service object var iam = new AWS.IAM({apiVersion: '2010-05-08'}); var params = { PolicyArn: 'arn:aws:iam::aws:policy/AWSLambdaExecute' }; iam.getPolicy(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Policy.Description); } });
Kotlin
SDK for Kotlin
注意

这是适用于预览版中功能的预发行文档。本文档随时可能更改。

提示

要了解如何设置和运行此示例,请参阅 GitHub

suspend fun getIAMPolicy(policyArnVal: String?) { val request = GetPolicyRequest { policyArn = policyArnVal } IamClient { region = "AWS_GLOBAL" }.use { iamClient -> val response = iamClient.getPolicy(request) println("Successfully retrieved policy ${response.policy?.policyName}") } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Kotlin API 参考》中的 GetPolicy

PHP
SDK for PHP
提示

要了解如何设置和运行此示例,请参阅 GitHub

$uuid = uniqid(); $service = new IamService(); public function getPolicy($policyArn) { return $this->customWaiter(function () use ($policyArn) { return $this->iamClient->getPolicy(['PolicyArn' => $policyArn]); }); }
  • 有关 API 详细信息,请参阅《Amazon SDK for PHP API 参考》中的 GetPolicy

Python
适用于 Python (Boto3) 的 SDK
提示

要了解如何设置和运行此示例,请参阅 GitHub

def get_default_policy_statement(policy_arn): """ Gets the statement of the default version of the specified policy. :param policy_arn: The ARN of the policy to look up. :return: The statement of the default policy version. """ try: policy = iam.Policy(policy_arn) # To get an attribute of a policy, the SDK first calls get_policy. policy_doc = policy.default_version.document policy_statement = policy_doc.get('Statement', None) logger.info("Got default policy doc for %s.", policy.policy_name) logger.info(policy_doc) except ClientError: logger.exception("Couldn't get default policy statement for %s.", policy_arn) raise else: return policy_statement
  • 有关 API 详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 GetPolicy

Ruby
SDK for Ruby
提示

要了解如何设置和运行此示例,请参阅 GitHub

# Gets data about a policy. # # @param policy_arn [String] The ARN of the policy to look up. # @return [Aws::IAM::Policy] The retrieved policy. def get_policy(policy_arn) policy = @iam_resource.policy(policy_arn) puts("Got policy '#{policy.policy_name}'. Its ID is: #{policy.policy_id}.") rescue Aws::Errors::ServiceError => e puts("Couldn't get policy '#{policy_arn}'. Here's why:") puts("\t#{e.code}: #{e.message}") raise else policy end
  • 有关 API 详细信息,请参阅《Amazon SDK for Ruby API 参考》中的 GetPolicy

有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 IAM 与 Amazon 开发工具包配合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。