本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 适用于 PHP 的 Amazon SDK 版本 3 来管理 IAM 用户
IAM 用户是在 Amazon 中创建的一个实体,代表使用其与 Amazon 进行交互的人员或服务。Amazon 中的用户包括名称和凭证。
以下示例演示如何:
-
使用 CreateUser 创建新 IAM 用户。
-
使用 ListUsers 列出 IAM 用户。
-
使用 UpdateUser 更新 IAM 用户。
-
使用 GetUser 检索有关 IAM 用户的信息。
-
使用 DeleteUser 删除 IAM 用户。
适用于 PHP 的 Amazon SDKGitHub 上提供了
凭证
运行示例代码之前,请配置您的 Amazon 凭证,如 Amazon 使用 适用于 PHP 的 Amazon SDK 版本 3 进行身份验证 中所述。然后导入 适用于 PHP 的 Amazon SDK,如 安装适用于 PHP 的 Amazon SDK 版本 3 中所述。
创建 IAM 用户
导入。
require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;
示例代码
$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->createUser(array( // UserName is required 'UserName' => 'string', )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
列出 IAM 用户
导入。
require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;
示例代码
$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->listUsers(); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
更新 IAM 用户
导入。
require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;
示例代码
$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->updateUser([ // UserName is required 'UserName' => 'string1', 'NewUserName' => 'string' ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
获取有关 IAM 用户的信息
导入。
require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;
示例代码
$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->getUser([ 'UserName' => 'string', ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
删除 IAM 用户
导入。
require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;
示例代码
$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->deleteUser([ // UserName is required 'UserName' => 'string' ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }