Gunakan CreateAlias dengan AWS SDKatau CLI - AWS Key Management Service

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan CreateAlias dengan AWS SDKatau CLI

Contoh kode berikut menunjukkan cara menggunakanCreateAlias.

.NET
AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

using System; using System.Threading.Tasks; using Amazon.KeyManagementService; using Amazon.KeyManagementService.Model; /// <summary> /// Creates an alias for an AWS Key Management Service (AWS KMS) key. /// </summary> public class CreateAlias { public static async Task Main() { var client = new AmazonKeyManagementServiceClient(); // The alias name must start with alias/ and can be // up to 256 alphanumeric characters long. var aliasName = "alias/ExampleAlias"; // The value supplied as the TargetKeyId can be either // the key ID or key Amazon Resource Name (ARN) of the // AWS KMS key. var keyId = "1234abcd-12ab-34cd-56ef-1234567890ab"; var request = new CreateAliasRequest { AliasName = aliasName, TargetKeyId = keyId, }; var response = await client.CreateAliasAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Alias, {aliasName}, successfully created."); } else { Console.WriteLine($"Could not create alias."); } } }
  • Untuk API detailnya, lihat CreateAliasdi AWS SDK for .NET APIReferensi.

CLI
AWS CLI

Untuk membuat alias untuk kunci KMS

create-aliasPerintah berikut membuat alias bernama example-alias untuk KMS kunci diidentifikasi oleh ID 1234abcd-12ab-34cd-56ef-1234567890ab kunci.

Nama alias harus dimulai denganalias/. Jangan gunakan nama alias yang dimulai denganalias/aws; ini dicadangkan untuk digunakan oleh AWS.

aws kms create-alias \ --alias-name alias/example-alias \ --target-key-id 1234abcd-12ab-34cd-56ef-1234567890ab

Perintah ini tidak mengembalikan output apa pun. Untuk melihat alias baru, gunakan list-aliases perintah.

Untuk informasi selengkapnya, lihat Menggunakan alias di AWS Panduan Pengembang Layanan Manajemen Kunci.

  • Untuk API detailnya, lihat CreateAliasdi AWS CLI Referensi Perintah.

Java
SDKuntuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/** * Creates a custom alias for the specified target key asynchronously. * * @param targetKeyId the ID of the target key for the alias * @param aliasName the name of the alias to create * @return a {@link CompletableFuture} that completes when the alias creation operation is finished */ public CompletableFuture<Void> createCustomAliasAsync(String targetKeyId, String aliasName) { CreateAliasRequest aliasRequest = CreateAliasRequest.builder() .aliasName(aliasName) .targetKeyId(targetKeyId) .build(); CompletableFuture<CreateAliasResponse> responseFuture = getAsyncClient().createAlias(aliasRequest); responseFuture.whenComplete((response, exception) -> { if (exception == null) { logger.info("{} was successfully created.", aliasName); } else { if (exception instanceof ResourceExistsException) { logger.info("Alias [{}] already exists. Moving on...", aliasName); } else if (exception instanceof KmsException kmsEx) { throw new RuntimeException("KMS error occurred while creating alias: " + kmsEx.getMessage(), kmsEx); } else { throw new RuntimeException("An unexpected error occurred while creating alias: " + exception.getMessage(), exception); } } }); return responseFuture.thenApply(response -> null); }
  • Untuk API detailnya, lihat CreateAliasdi AWS SDK for Java 2.x APIReferensi.

Kotlin
SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

suspend fun createCustomAlias( targetKeyIdVal: String?, aliasNameVal: String?, ) { val request = CreateAliasRequest { aliasName = aliasNameVal targetKeyId = targetKeyIdVal } KmsClient { region = "us-west-2" }.use { kmsClient -> kmsClient.createAlias(request) println("$aliasNameVal was successfully created") } }
  • Untuk API detailnya, lihat CreateAliasdi AWS SDKuntuk API referensi Kotlin.

Python
SDKuntuk Python (Boto3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

class AliasManager: def __init__(self, kms_client): self.kms_client = kms_client self.created_key = None def create_alias(self, key_id): """ Creates an alias for the specified key. :param key_id: The ARN or ID of a key to give an alias. :return: The alias given to the key. """ alias = "" while alias == "": alias = input(f"What alias would you like to give to key {key_id}? ") try: self.kms_client.create_alias(AliasName=alias, TargetKeyId=key_id) except ClientError as err: logger.error( "Couldn't create alias %s. Here's why: %s", alias, err.response["Error"]["Message"], ) else: print(f"Created alias {alias} for key {key_id}.") return alias
  • Untuk API detailnya, lihat CreateAliasdi AWS SDKuntuk Python (Boto3) Referensi. API

Untuk daftar lengkap AWS SDKpanduan pengembang dan contoh kode, lihatPenggunaan AWS KMS dengan sebuah AWS SDK. Topik ini juga mencakup informasi tentang memulai dan detail tentang SDK versi sebelumnya.