本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS KMS 使用 SDK for Rust 的範例
下列程式碼範例示範如何使用 AWS SDK for Rust 搭配 來執行動作和實作常見案例 AWS KMS。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
主題
動作
下列程式碼範例示範如何使用 CreateKey
。
- SDK for Rust
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn make_key(client: &Client) -> Result<(), Error> { let resp = client.create_key().send().await?; let id = resp.key_metadata.as_ref().unwrap().key_id(); println!("Key: {}", id); Ok(()) }
-
如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 CreateKey
。
-
下列程式碼範例示範如何使用 Decrypt
。
- SDK for Rust
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn decrypt_key(client: &Client, key: &str, filename: &str) -> Result<(), Error> { // Open input text file and get contents as a string // input is a base-64 encoded string, so decode it: let data = fs::read_to_string(filename) .map(|input| { base64::decode(input).expect("Input file does not contain valid base 64 characters.") }) .map(Blob::new); let resp = client .decrypt() .key_id(key) .ciphertext_blob(data.unwrap()) .send() .await?; let inner = resp.plaintext.unwrap(); let bytes = inner.as_ref(); let s = String::from_utf8(bytes.to_vec()).expect("Could not convert to UTF-8"); println!(); println!("Decoded string:"); println!("{}", s); Ok(()) }
-
如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的解密
。
-
下列程式碼範例示範如何使用 Encrypt
。
- SDK for Rust
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn encrypt_string( verbose: bool, client: &Client, text: &str, key: &str, out_file: &str, ) -> Result<(), Error> { let blob = Blob::new(text.as_bytes()); let resp = client.encrypt().key_id(key).plaintext(blob).send().await?; // Did we get an encrypted blob? let blob = resp.ciphertext_blob.expect("Could not get encrypted text"); let bytes = blob.as_ref(); let s = base64::encode(bytes); let mut ofile = File::create(out_file).expect("unable to create file"); ofile.write_all(s.as_bytes()).expect("unable to write"); if verbose { println!("Wrote the following to {:?}", out_file); println!("{}", s); } Ok(()) }
-
如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的加密
。
-
下列程式碼範例示範如何使用 GenerateDataKey
。
- SDK for Rust
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn make_key(client: &Client, key: &str) -> Result<(), Error> { let resp = client .generate_data_key() .key_id(key) .key_spec(DataKeySpec::Aes256) .send() .await?; // Did we get an encrypted blob? let blob = resp.ciphertext_blob.expect("Could not get encrypted text"); let bytes = blob.as_ref(); let s = base64::encode(bytes); println!(); println!("Data key:"); println!("{}", s); Ok(()) }
-
如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 GenerateDataKey
。
-
下列程式碼範例示範如何使用 GenerateDataKeyWithoutPlaintext
。
- SDK for Rust
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn make_key(client: &Client, key: &str) -> Result<(), Error> { let resp = client .generate_data_key_without_plaintext() .key_id(key) .key_spec(DataKeySpec::Aes256) .send() .await?; // Did we get an encrypted blob? let blob = resp.ciphertext_blob.expect("Could not get encrypted text"); let bytes = blob.as_ref(); let s = base64::encode(bytes); println!(); println!("Data key:"); println!("{}", s); Ok(()) }
-
如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 GenerateDataKeyWithoutPlaintext
。
-
下列程式碼範例示範如何使用 GenerateRandom
。
- SDK for Rust
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn make_string(client: &Client, length: i32) -> Result<(), Error> { let resp = client .generate_random() .number_of_bytes(length) .send() .await?; // Did we get an encrypted blob? let blob = resp.plaintext.expect("Could not get encrypted text"); let bytes = blob.as_ref(); let s = base64::encode(bytes); println!(); println!("Data key:"); println!("{}", s); Ok(()) }
-
如需 API 詳細資訊,請參閱 SDK AWS for Rust API 參考中的 GenerateRandom
。
-
下列程式碼範例示範如何使用 ListKeys
。
- SDK for Rust
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn show_keys(client: &Client) -> Result<(), Error> { let resp = client.list_keys().send().await?; let keys = resp.keys.unwrap_or_default(); let len = keys.len(); for key in keys { println!("Key ARN: {}", key.key_arn.as_deref().unwrap_or_default()); } println!(); println!("Found {} keys", len); Ok(()) }
-
如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 ListKeys
。
-
下列程式碼範例示範如何使用 ReEncrypt
。
- SDK for Rust
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn reencrypt_string( verbose: bool, client: &Client, input_file: &str, output_file: &str, first_key: &str, new_key: &str, ) -> Result<(), Error> { // Get blob from input file // Open input text file and get contents as a string // input is a base-64 encoded string, so decode it: let data = fs::read_to_string(input_file) .map(|input_file| base64::decode(input_file).expect("invalid base 64")) .map(Blob::new); let resp = client .re_encrypt() .ciphertext_blob(data.unwrap()) .source_key_id(first_key) .destination_key_id(new_key) .send() .await?; // Did we get an encrypted blob? let blob = resp.ciphertext_blob.expect("Could not get encrypted text"); let bytes = blob.as_ref(); let s = base64::encode(bytes); let o = &output_file; let mut ofile = File::create(o).expect("unable to create file"); ofile.write_all(s.as_bytes()).expect("unable to write"); if verbose { println!("Wrote the following to {}:", output_file); println!("{}", s); } else { println!("Wrote base64-encoded output to {}", output_file); } Ok(()) }
-
如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 ReEncrypt
。
-