如何使用boto删除队列

时间:2016-05-17 16:39:07

标签: boto amazon-sqs

我需要使用boto删除队列。我不确定如何删除队列。我试过这个,但它不起作用:

queue = conn.create_queue(sqs) #sqs is the queue name
conn.delete_queue(queue)

3 个答案:

答案 0 :(得分:1)

在boto v2

>>> import boto.sqs
>>> conn=boto.sqs.connect_to_region('ap-southeast-2')
>>> q = conn.create_queue('foo')
>>> q
Queue(https://ap-southeast-2.queue.amazonaws.com/123456789012/foo)
>>> q.__dict__
{'ResponseMetadata': '', 'url': u'https://ap-southeast-2.queue.amazonaws.com/123456789012/foo', 'CreateQueueResponse': '', 'message_class': <class 'boto.sqs.message.Message'>, 'connection': SQSConnection:ap-southeast-2.queue.amazonaws.com, 'RequestId': u'9bfb9b6d-d9b5-5a29-9ea5-d4dbd5e3ef5a', 'CreateQueueResult': '', 'visibility_timeout': None}
>>> conn.delete_queue(q)
True

在boto3

>>> import boto3
>>> client = boto3.client('sqs')
>>> q = client.create_queue(QueueName='foo')
>>> q
{u'QueueUrl': 'https://ap-southeast-2.queue.amazonaws.com/123456789012/foo', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': 'f6531b80-4387-57a2-98fa-54364841c158'}}
>>> client.delete_queue(QueueUrl=q['QueueUrl'])
{'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': 'f9d3853c-12f2-598b-ad3c-ca8a1f68754a'}}

答案 1 :(得分:0)

首先,你必须得到队列 -

queue = conn.create_queue('myqueue')  // asuming your queue name 'myqueue'

然后你可以简单地删除队列 -

conn.delete_queue(queue)

答案 2 :(得分:0)

在Boto3中,使用“队列”资源。以下是一些代码,它们是GitHub上的一个较大示例的一部分:

import logging
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
sqs = boto3.resource('sqs')

def remove_queue(queue):
    try:
        queue.delete()
        logger.info("Deleted queue with URL=%s.", queue.url)
    except ClientError as error:
        logger.exception("Couldn't delete queue with URL=%s!", queue.url)
        raise error

queue = sqs.create_queue(QueueName=name, Attributes=attributes)
remove_queue(queue)