使用 AWS KMS API 和 适用于 PHP 的 AWS SDK 版本 3 来使用别名 - 适用于 PHP 的 AWS SDK

使用 AWS KMS API 和 适用于 PHP 的 AWS SDK 版本 3 来使用别名

AWS Key Management Service (AWS KMS) 为 AWS KMS key(称为别名)提供可选的显示名称。

以下示例演示如何:

适用于 PHP 的 AWS SDKGitHub 上提供了的所有示例代码。

凭证

运行示例代码之前,请配置您的 AWS 凭证,如 通过适用于 PHP 的 AWS SDK 版本 3 使用 AWS 进行身份验证 中所述。然后导入 适用于 PHP 的 AWS SDK,如 安装适用于 PHP 的 AWS SDK 版本 3 中所述。

有关使用 AWS Key Management Service (AWS KMS) 的更多信息,请参阅 AWS KMS 开发人员指南

创建别名

要为 KMS 密钥创建别名,请使用 CreateAlias 操作。别名在账户和 AWS 区域中必须是唯一的。如果为已有别名的 KMS 密钥创建别名,CreateAlias 会为同一 KMS 密钥创建另一个别名。它不会替换现有别名。

导入

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

示例代码

$KmsClient = new Aws\Kms\KmsClient([ 'profile' => 'default', 'version' => '2014-11-01', 'region' => 'us-east-2' ]); $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $aliasName = "alias/projectKey1"; try { $result = $KmsClient->createAlias([ 'AliasName' => $aliasName, 'TargetKeyId' => $keyId, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

查看别名

要列出调用方 AWS 账户 和 AWS 区域 中的所有别名,请使用 ListAliases 操作。

导入

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

示例代码

$KmsClient = new Aws\Kms\KmsClient([ 'profile' => 'default', 'version' => '2014-11-01', 'region' => 'us-east-2' ]); $limit = 10; try { $result = $KmsClient->listAliases([ 'Limit' => $limit, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

更新别名

要将现有别名与其他 KMS 密钥关联,请使用 UpdateAlias 操作。

导入

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

示例代码

$KmsClient = new Aws\Kms\KmsClient([ 'profile' => 'default', 'version' => '2014-11-01', 'region' => 'us-east-2' ]); $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $aliasName = "alias/projectKey1"; try { $result = $KmsClient->updateAlias([ 'AliasName' => $aliasName, 'TargetKeyId' => $keyId, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

删除别名

要删除别名,请使用 DeleteAlias 操作。删除别名不会影响底层 KMS 密钥。

导入

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

示例代码

$KmsClient = new Aws\Kms\KmsClient([ 'profile' => 'default', 'version' => '2014-11-01', 'region' => 'us-east-2' ]); $aliasName = "alias/projectKey1"; try { $result = $KmsClient->deleteAlias([ 'AliasName' => $aliasName, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }