Amazon SQS: Receive Messages with Long Polling
See more Amazon SQS Examples
Make one long poll for up to five messages (SQS allows 1–10) and iterate every returned message. WaitTimeSeconds=20 reduces empty polling; the HTTP read timeout is longer. No messages is a normal result and does not prove the queue is permanently empty. The code requests current MessageSystemAttributeNames instead of deprecated AttributeNames, plus custom attributes.
The 60-second visibility timeout starts when messages are received, not when each loop iteration begins. Choose batch size and visibility for actual processing time; ChangeMessageVisibility can extend processing time or release a delivery. This inspection example prints bodies, verifies body MD5, and deletes nothing; messages become eligible for redelivery. Real consumers should commit their work before deleting and handle duplicates idempotently. Message IDs, delivery receipt handles, counts and millisecond timestamps remain strings. Keep real payloads and receipt handles private.
Uses SQS's AWS JSON protocol: POST to the regional root / with Content-Type: application/x-amz-json-1.0 and an operation-specific X-Amz-Target: AmazonSQS.... Chilkat.Rest and AuthAws sign with AWS Signature Version 4, service sqs. AWS still supports the older Query/XML protocol; these examples use JSON for simpler request and response handling. No AWS SDK is required.
Provide current IAM credentials in a private qa_data/aws/sqs.json with string fields access_key, secret_key, and session_token for temporary credentials. Queue-specific examples also read queue_url from this file. Set the region in the code to match the queue. These examples use commercial regional endpoints; other partitions or custom endpoints require corresponding changes. Prefer short-lived role credentials. The file is explicitly loaded; profiles and credential refresh are not automatic. Keep it outside public content and source control. The temporary token is included in the signed query; keep URLs and diagnostics private. TLS certificate verification is enabled.
Transport completion, HTTP status, and response validity are checked separately. Use bounded backoff with jitter for retryable errors, and refresh expired credentials. A timed-out write or receive may already have been accepted. Standard queues can deliver duplicates: make application processing idempotent and delete only after successful processing. Automatic reconnection is disabled for writes and receives to avoid replay after a lost response. Read-only queue listing permits reconnection.
Required IAM action: sqs:ReceiveMessage. Scope access to the intended queue. Customer-managed KMS encryption can require additional KMS permissions for producers and consumers.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_TextCompare
oleobject loo_Credentials
long li_SameText1
long li_SameText2
string ls_Region
oleobject loo_Endpoint
string ls_QueueUrl
long li_SameText3
oleobject loo_ExpectedPrefix
oleobject loo_QueueText
oleobject loo_Aws
oleobject loo_Rest
long li_SameText4
oleobject loo_Connection
oleobject loo_Body
oleobject loo_Result
oleobject loo_RequestBody
oleobject loo_ResponseBody
long li_MessagesType
long li_NumMessages
oleobject loo_Digest
oleobject loo_ReturnedMd5
long i
long li_SameText5
long li_SameText6
long li_SameText7
long li_SameText8
string ls_MessageId
string ls_ReceiptHandle
string ls_MessageBody
string ls_LocalMd5
li_Success = 0
// StringBuilder compares text by value in every generated language.
loo_TextCompare = create oleobject
li_rc = loo_TextCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_TextCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the SQS operation.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")
loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/sqs.json")
if li_Success <> 1 then
Write-Debug "Could not load private AWS credentials."
destroy loo_TextCompare
destroy loo_Credentials
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("access_key"))
li_SameText1 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_SameText1 = 1) then
Write-Debug "Missing or invalid access_key."
destroy loo_TextCompare
destroy loo_Credentials
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_SameText2 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_SameText2 = 1) then
Write-Debug "Missing or invalid secret_key."
destroy loo_TextCompare
destroy loo_Credentials
return
end if
// Set this once to the region containing the queue.
ls_Region = "us-west-2"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Endpoint.Append("sqs.")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
return
end if
li_Success = loo_Endpoint.Append(ls_Region)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
return
end if
li_Success = loo_Endpoint.Append(".amazonaws.com")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
return
end if
// Use the exact QueueUrl returned by CreateQueue, GetQueueUrl, or ListQueues.
// This example targets a standard commercial regional endpoint, not a custom endpoint.
ls_QueueUrl = loo_Credentials.StringOf("queue_url")
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_QueueUrl)
li_SameText3 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("queue_url") <> 1) OR (li_SameText3 = 1) then
Write-Debug "Missing or invalid queue_url in sqs.json."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
return
end if
loo_ExpectedPrefix = create oleobject
li_rc = loo_ExpectedPrefix.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_ExpectedPrefix.Append("https://")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
return
end if
li_Success = loo_ExpectedPrefix.Append(loo_Endpoint.GetAsString())
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
return
end if
li_Success = loo_ExpectedPrefix.Append("/")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
return
end if
loo_QueueText = create oleobject
li_rc = loo_QueueText.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_QueueText.Append(ls_QueueUrl)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
return
end if
if loo_QueueText.StartsWith(loo_ExpectedPrefix.GetAsString(),1) <> 1 then
Write-Debug "Queue URL must use HTTPS and the configured SQS region."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
return
end if
loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")
loo_Aws.AccessKey = loo_Credentials.StringOf("access_key")
loo_Aws.SecretKey = loo_Credentials.StringOf("secret_key")
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "sqs"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
Write-Debug "Could not configure AWS signing."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
return
end if
// Temporary IAM/STS credentials require their session token on every signed request.
// AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
// Leave session_token absent or empty only for credentials that do not have one.
if loo_Credentials.HasMember("session_token") = 1 then
if loo_Credentials.JsonTypeOf("session_token") <> 1 then
Write-Debug "session_token must be a string."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("session_token"))
li_SameText4 = loo_TextCompare.ContentsEqual("",1)
if li_SameText4 <> 1 then
li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
if li_Success <> 1 then
Write-Debug "Could not set the session-token parameter."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
return
end if
end if
end if
// Supply a Socket to explicitly enable TLS certificate verification.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")
loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
Write-Debug "TLS connection failed; inspect diagnostics privately."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
// Do not automatically reconnect/replay a write or receive after a lost response.
li_Success = loo_Rest.UseConnection(loo_Connection,0)
if li_Success <> 1 then
Write-Debug "Could not attach the TLS connection."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
li_Success = loo_Rest.AddHeader("Content-Type","application/x-amz-json-1.0")
if li_Success <> 1 then
Write-Debug "Could not set the content type."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")
loo_Result.Strict = 1
loo_RequestBody = create oleobject
li_rc = loo_RequestBody.ConnectToNewObject("Chilkat.StringBuilder")
loo_ResponseBody = create oleobject
li_rc = loo_ResponseBody.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Rest.AddHeader("X-Amz-Target","AmazonSQS.ReceiveMessage")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_Body.UpdateString("QueueUrl",ls_QueueUrl)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_Body.UpdateInt("MaxNumberOfMessages",5)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_Body.UpdateInt("WaitTimeSeconds",20)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_Body.UpdateInt("VisibilityTimeout",60)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_Body.UpdateString("MessageSystemAttributeNames[0]","ApproximateReceiveCount")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_Body.UpdateString("MessageSystemAttributeNames[1]","SentTimestamp")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_Body.UpdateString("MessageAttributeNames[0]","All")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
// Rest and Socket read timeouts are 60 seconds, longer than this 20-second poll.
// EmitSb appends, so clear both builders before each request/page.
loo_RequestBody.Clear()
loo_ResponseBody.Clear()
li_Success = loo_Body.EmitSb(loo_RequestBody)
if li_Success <> 1 then
Write-Debug "Could not serialize JSON."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_Rest.FullRequestSb("POST","/",loo_RequestBody,loo_ResponseBody)
if li_Success <> 1 then
Write-Debug "Request did not complete; inspect diagnostics privately."
// A write or receive may have reached SQS. Do not blindly replay it.
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
if loo_Rest.ResponseStatusCode <> 200 then
Write-Debug "SQS HTTP status: " + string(loo_Rest.ResponseStatusCode)
Write-Debug "AWS request ID: " + loo_Rest.ResponseHdrByName("x-amzn-RequestId")
li_Success = loo_Result.LoadSb(loo_ResponseBody)
if (li_Success = 1) AND (loo_Result.JsonTypeOf("__type") = 1) then
Write-Debug "AWS error type: " + loo_Result.StringOf("__type")
else
Write-Debug "AWS error type: " + loo_Rest.ResponseHdrByName("x-amzn-errortype")
end if
// Error messages, payloads, receipt handles and signed URLs can be private.
// HTTP 400 can be throttling, expired credentials, or invalid parameters.
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_ResponseBody.Trim()
if li_Success <> 1 then
Write-Debug "Could not inspect response."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
if loo_ResponseBody.StartsWith("{",1) <> 1 then
Write-Debug "Expected a JSON object response."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_Success = loo_Result.LoadSb(loo_ResponseBody)
if li_Success <> 1 then
Write-Debug "Expected well-formed JSON."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_MessagesType = loo_Result.JsonTypeOf("Messages")
if (li_MessagesType = -1) OR (li_MessagesType = 6) then
Write-Debug "No messages returned by this poll."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
if li_MessagesType <> 4 then
Write-Debug "Expected Messages array."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
li_NumMessages = loo_Result.SizeOfArray("Messages")
if li_NumMessages = 0 then
Write-Debug "No messages returned by this poll."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
return
end if
loo_Digest = create oleobject
li_rc = loo_Digest.ConnectToNewObject("Chilkat.Crypt2")
loo_Digest.HashAlgorithm = "md5"
loo_Digest.Charset = "utf-8"
loo_Digest.EncodingMode = "hex"
loo_ReturnedMd5 = create oleobject
li_rc = loo_ReturnedMd5.ConnectToNewObject("Chilkat.StringBuilder")
i = 0
do while (i < li_NumMessages)
loo_Result.I = i
if loo_Result.JsonTypeOf("Messages[i]") <> 3 then
Write-Debug "Expected a message object."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Result.StringOf("Messages[i].MessageId"))
li_SameText5 = loo_TextCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("Messages[i].MessageId") <> 1) OR (li_SameText5 = 1) then
Write-Debug "Missing or invalid Messages[i].MessageId."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Result.StringOf("Messages[i].ReceiptHandle"))
li_SameText6 = loo_TextCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("Messages[i].ReceiptHandle") <> 1) OR (li_SameText6 = 1) then
Write-Debug "Missing or invalid Messages[i].ReceiptHandle."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Result.StringOf("Messages[i].Body"))
li_SameText7 = loo_TextCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("Messages[i].Body") <> 1) OR (li_SameText7 = 1) then
Write-Debug "Missing or invalid Messages[i].Body."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Result.StringOf("Messages[i].MD5OfBody"))
li_SameText8 = loo_TextCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("Messages[i].MD5OfBody") <> 1) OR (li_SameText8 = 1) then
Write-Debug "Missing or invalid Messages[i].MD5OfBody."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5
return
end if
ls_MessageId = loo_Result.StringOf("Messages[i].MessageId")
ls_ReceiptHandle = loo_Result.StringOf("Messages[i].ReceiptHandle")
ls_MessageBody = loo_Result.StringOf("Messages[i].Body")
ls_LocalMd5 = loo_Digest.HashStringENC(ls_MessageBody)
if loo_Digest.LastMethodSuccess <> 1 then
Write-Debug "Could not calculate body MD5."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5
return
end if
loo_ReturnedMd5.Clear()
li_Success = loo_ReturnedMd5.Append(loo_Result.StringOf("Messages[i].MD5OfBody"))
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5
return
end if
if loo_ReturnedMd5.ContentsEqual(ls_LocalMd5,0) <> 1 then
Write-Debug "Body MD5 mismatch; do not process or delete this message."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5
return
end if
Write-Debug "Message ID: " + ls_MessageId
// This body display is for demonstration. Keep real message contents out of shared logs.
Write-Debug "Body: " + ls_MessageBody
if loo_Result.JsonTypeOf("Messages[i].Attributes.ApproximateReceiveCount") = 1 then
Write-Debug "Approximate receive count: " + loo_Result.StringOf("Messages[i].Attributes.ApproximateReceiveCount")
end if
if loo_Result.JsonTypeOf("Messages[i].Attributes.SentTimestamp") = 1 then
// Epoch milliseconds stay strings; a 32-bit int would overflow.
Write-Debug "Sent timestamp (milliseconds): " + loo_Result.StringOf("Messages[i].Attributes.SentTimestamp")
end if
if loo_Result.JsonTypeOf("Messages[i].MessageAttributes.content_type.StringValue") = 1 then
Write-Debug "Content type attribute: " + loo_Result.StringOf("Messages[i].MessageAttributes.content_type.StringValue")
end if
// receiptHandle belongs to THIS delivery. Pass it with queueUrl to DeleteMessage
// only after application work succeeds durably. Do not use messageId for deletion.
// This inspection example performs no application work and deletes nothing.
// Visibility is a processing window, not a guarantee against duplicate delivery.
i = i + 1
loop
Write-Debug "Messages inspected: " + string(li_NumMessages)
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5