我有一个情况,脚本将所有未使用的卷 ID 写入 AWS SQS 队列,一段时间后,我们需要接收所有带有卷 ID 的消息并删除这些卷。有没有办法使用 python boto 库来实现这一点?
使用 boto 库接收 AWS SQS 队列中的所有消息,直到队列为空
receive_message()
命令:
从指定队列中检索一条或多条消息(最多 10 条)。
delete_message()
或 delete_message_batch()
来删除消息,并为从 ReceiptHandle
调用获取的每条消息传递 receive_message()
。
要处理队列中的所有消息,请继续循环内接收和删除消息。
这就是我所做的。
while True:
response = self.client.receive_message(
QueueUrl=self.queue_url, MaxNumberOfMessages=10, WaitTimeSeconds=20
)
if "Messages" not in response:
break
for message in response["Messages"]:
recieved_item = message["Body"]
# Process your item here
# delete the message from the queue
self.client.delete_message(
QueueUrl=self.queue_url, ReceiptHandle=message["ReceiptHandle"]
)
退出 while 循环后,您可以删除队列