在开发过程中,我在我的 Amazon SQS 上生成了大量虚假消息。我正要编写一个小应用程序来删除所有消息(我在开发过程中经常这样做)。有谁知道清除队列的工具?
9 回答
如果您不想编写脚本或删除队列。您可以更改队列配置:
- 右键单击队列>
configure queue
- 更改
Message Retention period
为 1 分钟(可以设置的最短时间)。 - 等待一段时间让所有消息消失。
我发现这种方式适用于删除队列中的所有消息而不删除队列。
自 2014 年 12 月起,sqs 控制台的队列操作菜单中现在有一个清除队列选项。
对于任何来这里的人,正在寻找一种在 C# 中大量删除 SQS 消息的方法......
//C# Console app which deletes all messages from a specified queue
//AWS .NET library required.
using System;
using System.Net;
using System.Configuration;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using Amazon;
using Amazon.SQS;
using Amazon.SQS.Model;
using System.Timers;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace QueueDeleter
{
class Program
{
public static System.Timers.Timer myTimer;
static NameValueCollection appConfig = ConfigurationManager.AppSettings;
static string accessKeyID = appConfig["AWSAccessKey"];
static string secretAccessKeyID = appConfig["AWSSecretKey"];
static private AmazonSQS sqs;
static string myQueueUrl = "https://queue.amazonaws.com/1640634564530223/myQueueUrl";
public static String messageReceiptHandle;
public static void Main(string[] args)
{
sqs = AWSClientFactory.CreateAmazonSQSClient(accessKeyID, secretAccessKeyID);
myTimer = new System.Timers.Timer();
myTimer.Interval = 10;
myTimer.Elapsed += new ElapsedEventHandler(checkQueue);
myTimer.AutoReset = true;
myTimer.Start();
Console.Read();
}
static void checkQueue(object source, ElapsedEventArgs e)
{
myTimer.Stop();
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
receiveMessageRequest.QueueUrl = myQueueUrl;
ReceiveMessageResponse receiveMessageResponse = sqs.ReceiveMessage(receiveMessageRequest);
if (receiveMessageResponse.IsSetReceiveMessageResult())
{
ReceiveMessageResult receiveMessageResult = receiveMessageResponse.ReceiveMessageResult;
if (receiveMessageResult.Message.Count < 1)
{
Console.WriteLine("Can't find any visible messages.");
myTimer.Start();
return;
}
foreach (Message message in receiveMessageResult.Message)
{
Console.WriteLine("Printing received message.\n");
messageReceiptHandle = message.ReceiptHandle;
Console.WriteLine("Message Body:");
if (message.IsSetBody())
{
Console.WriteLine(" Body: {0}", message.Body);
}
sqs.DeleteMessage(new DeleteMessageRequest().WithQueueUrl(myQueueUrl).WithReceiptHandle(messageReceiptHandle));
}
}
else
{
Console.WriteLine("No new messages.");
}
myTimer.Start();
}
}
}
检查队列中的第一项。向下滚动到队列中的最后一项。按住shift,点击项目。所有将被选中。
我认为最好的方法是删除队列并再次创建它,只需 2 个请求。
我认为最好的方法是将保留期更改为 1 分钟,但如果有人需要,这里是 Python 代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import boto.sqs
from boto.sqs.message import Message
import time
import os
startTime = program_start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
### Lets connect to SQS:
qcon = boto.sqs.connect_to_region(region,aws_access_key_id='xxx',aws_secret_access_key='xxx')
SHQueue = qcon.get_queue('SQS')
m = Message()
### Read file and write to SQS
counter = 0
while counter < 1000: ## For deleting 1000*10 items, change to True if you want delete all
links = SHQueue.get_messages(10)
for link in links:
m = link
SHQueue.delete_message(m)
counter += 1
#### The End
print "\n\nTerminating...\n"
print "Start: ", program_start_time
print "End time: ", time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
选项 1:boto sqs 有一个用于 python 的 purge_queue 方法:
purge_queue(queue)
Purge all messages in an SQS Queue.
Parameters: queue (A Queue object) – The SQS queue to be purged
Return type: bool
Returns: True if the command succeeded, False otherwise
资料来源:http ://boto.readthedocs.org/en/latest/ref/sqs.html
适合我的代码:
conn = boto.sqs.connect_to_region('us-east-1',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)
q = conn.create_queue("blah")
#add some messages here
#invoke the purge_queue method of the conn, and pass in the
#queue to purge.
conn.purge_queue(self.queue)
对我来说,它删除了队列。但是,Amazon SQS 仅允许您每 60 秒运行一次。所以我不得不使用下面的辅助解决方案:
选项 2:通过在 while 循环中使用所有消息并将它们丢弃来执行清除:
all_messages = []
rs = self.queue.get_messages(10)
while len(rs) > 0:
all_messages.extend(rs)
rs = self.queue.get_messages(10)
要从 API 中清除 SQS,请参阅:
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_PurgeQueue.html