Golang AWS SDK V2 Pagination

Hello. New to Golang and am wanting to use it in conjunction with AWS services. Trying to write a go program with aws-go-sdk-v2 that will return sqs queue url’s. Have 1000+ queue’s created, so wanted to use pagination. I am using the following code:

package main

 import (
    "fmt"
    "context"

    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/sqs"
 )

 func listqueue(p string) string{
    //Initialize the session
    cfg, err := config.LoadDefaultConfig(context.TODO(),
            config.WithRegion("us-east-1"),
    )

    if err != nil {
      fmt.Println("We got an error!!")
      return "nil"
    }

    //Create a SQS service client
    client := sqs.NewFromConfig(cfg)

    params := &sqs.ListQueuesInput{}

    if p != "" {
            params = &sqs.ListQueuesInput{
                    QueueNamePrefix: aws.String(p),
            }
    }

    // List the queues available in a given region.
    totalQueues := 0
    paginator := sqs.NewListQueuesPaginator(client, params)
    for paginator.HasMorePages(){
       output, err := paginator.NextPage(context.TODO())
       if err != nil {
            fmt.Println("Error", err)
            return "nil"
       }
       totalQueues += len(output.QueueUrls)
    }
    fmt.Println("total objects:", totalQueues)
    fmt.Println("Success")

    return "True"

Issue(s) I’m having is that the program ends after getting 1000 queue urls. I know I have more than 1000 queues. I thought that with pagination it would return 1000 per page, but does the paginator only return 1000 total?

Also, how do I go about returning the queue urls to another module? I tried doing:

return output.QueueUrls

but am getting an error doing compile that output is not defined. Guessing this is because it is defined in the for loop, but I tried initializing outside of the for loop and still got an error:

output := []*string

Any help with this would be greatly appreciated.

Was able to figure out my first issue. I needed to pass in the ListQueuesPaginatorOptions to the NewListQueuesPaginator function:

...

paginator := sqs.NewListQueuesPaginator(client, params, func(o *sqs.ListQueuesPaginatorOptions) {
    o.Limit = 1000
})

Issue now is how to return the entire list of url’s to a different module

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.