PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRest.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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/
bTls.i = 1
port.i = 443
bAutoReconnect.i = 1
success = CkRest::ckConnect(rest,"kms.us-west-2.amazonaws.com",port,bTls,bAutoReconnect)
; Provide AWS credentials for the REST call.
authAws.i = CkAuthAws::ckCreate()
If authAws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
CkAuthAws::setCkSecretKey(authAws, "AWS_SECRET_KEY")
; the region should match our URL above..
CkAuthAws::setCkRegion(authAws, "us-west-2")
CkAuthAws::setCkServiceName(authAws, "kms")
CkRest::ckSetAuthAws(rest,authAws)
CkRest::ckAddHeader(rest,"X-Amz-Target","TrentService.ListKeys")
CkRest::ckAddHeader(rest,"Content-Type","application/x-amz-json-1.1")
strJson.s = CkRest::ckFullRequestString(rest,"POST","/","{}")
If CkRest::ckLastMethodSuccess(rest) <> 1
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
ProcedureReturn
EndIf
; A successful response will have a status code equal to 200.
If CkRest::ckResponseStatusCode(rest) <> 200
Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest))
Debug "response status text = " + CkRest::ckResponseStatusText(rest)
Debug "response header: " + CkRest::ckResponseHeader(rest)
Debug "response body: " + strJson
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
ProcedureReturn
EndIf
; Examine the successful JSON response (shown below)
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoad(json,strJson)
CkJsonObject::setCkEmitCompact(json, 0)
Debug CkJsonObject::ckEmit(json)
; 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
KeyArn.s
KeyId.s
KeyCount.i = CkJsonObject::ckIntOf(json,"KeyCount")
Truncated.i = CkJsonObject::ckBoolOf(json,"Truncated")
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(json,"Keys")
While i < count_i
CkJsonObject::setCkI(json, i)
KeyArn = CkJsonObject::ckStringOf(json,"Keys[i].KeyArn")
KeyId = CkJsonObject::ckStringOf(json,"Keys[i].KeyId")
i = i + 1
Wend
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure