简体   繁体   中英

AWS sqs queues don't list all queues

I am new to AWS. I am following AWS tutorials quick start. I was following the AWS sqs code and sqs documentation.

This is the following code which I wrote

import boto3
from boto3.session import Session

session = Session(aws_access_key_id="aswe343ffsf34r3fef3f",
                       aws_secret_access_key="3423d23r2fwwfe232r2r",
                    region_name="ap-southeast-1")

sqs = session.resource("sqs")
q_client = boto3.client("sqs")


sqs.create_queue(QueueName='test-One',
            Attributes = {'DelaySeconds': '5'
})


sqs.create_queue(QueueName='test-Three',
            Attributes = {'DelaySeconds': '5' })

#print "The queue path is : ",queue.url
#print dict(queue.attributes)
#print " DILAY  ",queue.attributes.get("DelaySeconds")

q_client.create_queue(QueueName='test-Two',Attributes = {
    "DelaySeconds" : "5"
})

#q_client.delete_queue(QueueUrl = 'https://us-west-2.queue.amazonaws.com/978916941101/test')

q = q_client.list_queues()

print "QUEUE - URLS ",q.get("QueueUrls")

qList = sqs.queues.all()

for q in qList:
    print q.url

The output of the above code is

(env1)rahul@ubuntu:~/rahul/PythonPractise/Boto3_Practise$ python clientTwo.py
QUEUE - URLS  ['https://us-west-2.queue.amazonaws.com/978916941101/test-Two']
https://ap-southeast-1.queue.amazonaws.com/978916941101/test
https://ap-southeast-1.queue.amazonaws.com/978916941101/test-1
https://ap-southeast-1.queue.amazonaws.com/978916941101/test-One
https://ap-southeast-1.queue.amazonaws.com/978916941101/test-Three
(env1)rahul@ubuntu:~/rahul/PythonPractise/Boto3_Practise$

My question is,

why queue Client is not able to list queues created from "resource" and why resource is does not list the queues created from client.

when I do

(env1)rahul@ubuntu:~/rahul/PythonPractise/Boto3_Practise$ aws configure list
  Name                    Value             Type    Location
  ----                    -----             ----    --------
   profile                <not set>             None    None
   access_key     ****************ef3f shared-credentials-file
  secret_key     ****************2r2r shared-credentials-file
   region                us-west-2      config-file    ~/.aws/config

Is it because of the regions ?

You create the resource from your custom session, which has ap-southeast-1 as the region. You create the client from the boto3 default session, which you have set to us-west-2 . When you declare a region, you only have access to resources in that region.

This code should list all the queues (with or without a prefix). It's part of a complete SQS example I put on GitHub .

import logging
import boto3

logger = logging.getLogger(__name__)
sqs = boto3.resource('sqs')

def get_queues(prefix=None):
    if prefix:
        queue_iter = sqs.queues.filter(QueueNamePrefix=prefix)
    else:
        queue_iter = sqs.queues.all()
    queues = list(queue_iter)
    if queues:
        logger.info("Got queues: %s", ', '.join([q.url for q in queues]))
    else:
        logger.warning("No queues found.")
    return queues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM