Sample code for 30+ languages & platforms
Rust

AWS KMS List Keys

See more AWS KMS Examples

Gets a list of all KMS keys in the caller's AWS account and Region.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let rest = chilkat::Rest::new();

// Connect to the Amazon AWS REST server.
// Make sure to use the region that is correct for you.
// such as https://kms.us-west-2.amazonaws.com/
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("kms.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect).is_ok();

// Provide AWS credentials for the REST call.
let auth_aws = chilkat::AuthAws::new();
auth_aws.set_access_key("AWS_ACCESS_KEY");
auth_aws.set_secret_key("AWS_SECRET_KEY");
// the region should match our URL above..
auth_aws.set_region("us-west-2");
auth_aws.set_service_name("kms");

let _ = rest.set_auth_aws(&auth_aws);

let _ = rest.add_header("X-Amz-Target", "TrentService.ListKeys");
let _ = rest.add_header("Content-Type", "application/x-amz-json-1.1");

let Ok(str_json) = rest.full_request_string("POST", "/", "{}") else {
    println!("{}", rest.last_error_text());
    return;
};

// A successful response will have a status code equal to 200.
if rest.response_status_code() != 200 {
    println!("response status code = {}", rest.response_status_code());
    println!("response status text = {}", rest.response_status_text());
    println!("response header: {}", rest.response_header());
    println!("response body: {}", str_json);
    return;
}

// Examine the successful JSON response (shown below)
let json = chilkat::JsonObject::new();
let _ = json.load(&str_json);
json.set_emit_compact(false);

println!("{}", json.emit().unwrap_or_default());

// Sample output:

// {
//   "KeyCount": 4,
//   "Keys": [
//     {
//       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/082f8520-7afc-4a09-b703-89b7243072e5",
//       "KeyId": "082f8520-7afc-4a09-b703-89b7243072e5"
//     },
//     {
//       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/17432483-ff08-4950-93d3-f46ebb5e17d1",
//       "KeyId": "17432483-ff08-4950-93d3-f46ebb5e17d1"
//     },
//     {
//       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/1b0e5b3c-0675-4510-adb6-a75b40a93da0",
//       "KeyId": "1b0e5b3c-0675-4510-adb6-a75b40a93da0"
//     },
//     {
//       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/265e3993-428b-4581-9466-b1030a53062f",
//       "KeyId": "265e3993-428b-4581-9466-b1030a53062f"
//     },
//   ],
//   "Truncated": false
// }

// Use this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

let key_count = json.int_of("KeyCount");
let truncated = json.bool_of("Truncated");
let mut i = 0;
let count_i = json.size_of_array("Keys");
while i < count_i {
    json.set_i(i);
    let _ = json.string_of("Keys[i].KeyArn").unwrap_or_default();
    let _ = json.string_of("Keys[i].KeyId").unwrap_or_default();
    i = i + 1;
}