Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.6.0+

Amazon SQS: Resolve a Queue URL by Name

See more Amazon SQS Examples

Resolve a case-sensitive queue name in the configured region with GetQueueUrl, instead of assembling a URL or listing every queue. Optionally supply QueueOwnerAWSAccountId for a queue owned by another account; both your identity and the queue resource policy must permit access. The returned URL identifies the queue, while a queue ARN is used in policies and redrive configuration. Preserve account IDs and URLs as strings.

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:GetQueueUrl. Scope access to the intended queue. Customer-managed KMS encryption can require additional KMS permissions for producers and consumers.

AWS GetQueueUrl reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_TextCompare
oleobject loo_Credentials
long li_SameText1
long li_SameText2
string ls_Region
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
long li_SameText3
oleobject loo_Connection
oleobject loo_Body
oleobject loo_Result
oleobject loo_RequestBody
oleobject loo_ResponseBody
string ls_QueueName
string ls_QueueOwnerAccountId
long li_SameText4
long li_SameText5

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

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_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_Aws
        destroy loo_Rest
        return
    end if

    loo_TextCompare.Clear()
    loo_TextCompare.SetString(loo_Credentials.StringOf("session_token"))
    li_SameText3 = loo_TextCompare.ContentsEqual("",1)
    if li_SameText3 <> 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_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_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_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_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.GetQueueUrl")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

ls_QueueName = "ExampleQueue"
//  Leave empty for your account; otherwise supply the owner's 12-digit account ID.
ls_QueueOwnerAccountId = ""
li_Success = loo_Body.UpdateString("QueueName",ls_QueueName)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    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_TextCompare.Clear()
loo_TextCompare.SetString(ls_QueueOwnerAccountId)
li_SameText4 = loo_TextCompare.ContentsEqual("",1)
if li_SameText4 <> 1 then
    li_Success = loo_Body.UpdateString("QueueOwnerAWSAccountId",ls_QueueOwnerAccountId)
    if li_Success <> 1 then
        Write-Debug "Could not prepare the request."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_RequestBody
        destroy loo_ResponseBody
        return
    end if

end if

//  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_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_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_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_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_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_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Result.StringOf("QueueUrl"))
li_SameText5 = loo_TextCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("QueueUrl") <> 1) OR (li_SameText5 = 1) then
    Write-Debug "Missing or invalid QueueUrl."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

Write-Debug "Queue URL: " + loo_Result.StringOf("QueueUrl")
//  Save this exact string as queue_url in the private configuration for other examples.


destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody