AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
AWS KMSSDK for Go V2 の例
次のコード例は、AWS SDK for GoでV2 を使用してアクションを実行する方法を示していますAWS KMS。
「アクション」は、個々のサービス関数の呼び出し方法を示すコードの抜粋です。
「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。
それぞれの例にはGitHub、へのリンクがあり、コンテキストでコードを設定および実行する方法についての説明が記載されています。
トピック
アクション
次のコード例は、を作成する方法を示していますAWS KMS key。
- SDK for Go V2
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 package main import ( "context" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/kms" ) // KMSCreateKeyAPI defines the interface for the CreateKey function. // We use this interface to test the function using a mocked service. type KMSCreateKeyAPI interface { CreateKey(ctx context.Context, params *kms.CreateKeyInput, optFns ...func(*kms.Options)) (*kms.CreateKeyOutput, error) } // MakeKey creates an AWS Key Management Service (AWS KMS) key (KMS key). // Inputs: // c is the context of the method call, which includes the AWS Region. // api is the interface that defines the method call. // input defines the input arguments to the service call. // Output: // If success, a CreateKeyOutput object containing the result of the service call and nil. // Otherwise, nil and an error from the call to CreateKey. func MakeKey(c context.Context, api KMSCreateKeyAPI, input *kms.CreateKeyInput) (*kms.CreateKeyOutput, error) { return api.CreateKey(c, input) } func main() { cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := kms.NewFromConfig(cfg) input := &kms.CreateKeyInput{} result, err := MakeKey(context.TODO(), client, input) if err != nil { fmt.Println("Got error creating key:") fmt.Println(err) return } fmt.Println(*result.KeyMetadata.KeyId) }
-
API の詳細については、AWS SDK for GoAPI CreateKey
リファレンスのを参照してください。
-
次のコード例は、KMS キーで暗号化された暗号化テキストを復号する方法を示しています。
- SDK for Go V2
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 package main import ( "context" b64 "encoding/base64" "flag" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/kms" ) // KMSDecryptAPI defines the interface for the Decrypt function. // We use this interface to test the function using a mocked service. type KMSDecryptAPI interface { Decrypt(ctx context.Context, params *kms.DecryptInput, optFns ...func(*kms.Options)) (*kms.DecryptOutput, error) } // DecodeData decrypts some text that was encrypted with an AWS Key Management Service (AWS KMS) key (KMS key). // Inputs: // c is the context of the method call, which includes the AWS Region. // api is the interface that defines the method call. // input defines the input arguments to the service call. // Output: // If success, a DecryptOutput object containing the result of the service call and nil. // Otherwise, nil and an error from the call to Decrypt. func DecodeData(c context.Context, api KMSDecryptAPI, input *kms.DecryptInput) (*kms.DecryptOutput, error) { return api.Decrypt(c, input) } func main() { data := flag.String("d", "", "The encrypted data, as a string") flag.Parse() if *data == "" { fmt.Println("You must supply the encrypted data as a string") fmt.Println("-d DATA") return } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := kms.NewFromConfig(cfg) blob, err := b64.StdEncoding.DecodeString(*data) if err != nil { panic("error converting string to blob, " + err.Error()) } input := &kms.DecryptInput{ CiphertextBlob: blob, } result, err := DecodeData(context.TODO(), client, input) if err != nil { fmt.Println("Got error decrypting data: ", err) return } fmt.Println(string(result.Plaintext)) }
-
API の詳細については、AWS SDK for GoAPI リファレンスの「復号化
」を参照してください。
-
次のコード例は、KMS キーを使用してテキストを削除する方法を示しています。
- SDK for Go V2
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 package main import ( "context" b64 "encoding/base64" "flag" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/kms" ) // KMSEncryptAPI defines the interface for the Encrypt function. // We use this interface to test the function using a mocked service. type KMSEncryptAPI interface { Encrypt(ctx context.Context, params *kms.EncryptInput, optFns ...func(*kms.Options)) (*kms.EncryptOutput, error) } // EncryptText encrypts some text using an AWS Key Management Service (AWS KMS) key (KMS key). // Inputs: // c is the context of the method call, which includes the AWS Region. // api is the interface that defines the method call. // input defines the input arguments to the service call. // Output: // If success, an EncryptOutput object containing the result of the service call and nil. // Otherwise, nil and an error from the call to Encrypt. func EncryptText(c context.Context, api KMSEncryptAPI, input *kms.EncryptInput) (*kms.EncryptOutput, error) { return api.Encrypt(c, input) } func main() { keyID := flag.String("k", "", "The ID of a KMS key") text := flag.String("t", "", "The text to encrypt") flag.Parse() if *keyID == "" || *text == "" { fmt.Println("You must supply the ID of a KMS key and some text") fmt.Println("-k KEY-ID -t \"text\"") return } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := kms.NewFromConfig(cfg) input := &kms.EncryptInput{ KeyId: keyID, Plaintext: []byte(*text), } result, err := EncryptText(context.TODO(), client, input) if err != nil { fmt.Println("Got error encrypting data:") fmt.Println(err) return } blobString := b64.StdEncoding.EncodeToString(result.CiphertextBlob) fmt.Println(blobString) }
-
API の詳細については、AWS SDK for GoAPI リファレンスの「暗号化
」を参照してください。
-
次のコード例は、ある KMS キーから別の KMS キーに暗号テキストを再暗号化する方法を示しています。
- SDK for Go V2
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 package main import ( "context" "flag" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/kms" ) // KMSReEncryptAPI defines the interface for the ReEncrypt function. // We use this interface to test the function using a mocked service. type KMSReEncryptAPI interface { ReEncrypt(ctx context.Context, params *kms.ReEncryptInput, optFns ...func(*kms.Options)) (*kms.ReEncryptOutput, error) } // ReEncryptText reencrypts some text using a new AWS Key Management Service (AWS KMS) key (KMS key). // Inputs: // c is the context of the method call, which includes the AWS Region. // api is the interface that defines the method call. // input defines the input arguments to the service call. // Output: // If success, a ReEncryptOutput object containing the result of the service call and nil. // Otherwise, nil and an error from the call to ReEncrypt. func ReEncryptText(c context.Context, api KMSReEncryptAPI, input *kms.ReEncryptInput) (*kms.ReEncryptOutput, error) { return api.ReEncrypt(c, input) } func main() { keyID := flag.String("k", "", "The ID of a KMS key") data := flag.String("d", "", "The data to reencrypt, as a string") flag.Parse() if *keyID == "" || *data == "" { fmt.Println("You must supply the ID of a KMS key and data") fmt.Println("-k KEY-ID -d DATA") return } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := kms.NewFromConfig(cfg) blob := []byte(*data) input := &kms.ReEncryptInput{ CiphertextBlob: blob, DestinationKeyId: keyID, } result, err := ReEncryptText(context.TODO(), client, input) if err != nil { fmt.Println("Got error reencrypting data:") fmt.Println(err) return } fmt.Println("Blob (base-64 byte array):") fmt.Println(result.CiphertextBlob) }
-
API の詳細については、AWS SDK for GoAPI ReEncrypt
リファレンスのを参照してください。
-