示例#1
0
        private void QueryQueue()
        {
            var _client = new AmazonSQSClient(
                awsAccessKeyId: AccessKeyId,
                awsSecretAccessKey: SecretKey,
                region: Amazon.RegionEndpoint.USEast1);

            var _request = new ReceiveMessageRequest
            {
                QueueUrl = SQSUrl, MaxNumberOfMessages = 10
            };

            var _response = _client.ReceiveMessage(_request);

            if (_response.Messages.Count > 0)
            {
                (new Thread(() =>
                {
                    _relayHelper = MainClass.container.Resolve <IRelayHelper>();
                    _relayHelper.Alarm();
                })).Start();

                try { _client.PurgeQueue(new PurgeQueueRequest {
                        QueueUrl = SQSUrl
                    }); } catch { }
            }
        }
示例#2
0
        public static void PurgeQueueByQueueUrl(string queueUrl)
        {
            using (var client = new AmazonSQSClient(Settings.AccessKey, Settings.Secret))
            {
                var request = new PurgeQueueRequest()
                {
                    QueueUrl = queueUrl
                };

                client.PurgeQueue(request);
            }
        }
示例#3
0
        public void PurgeMessages()
        {
            PurgeQueueRequest request = new PurgeQueueRequest {
                QueueUrl = QueueUrl
            };
            var response = client.PurgeQueue(request);

            if (response.HttpStatusCode.IsSuccess())
            {
                Console.WriteLine("Message(s) Deleted Succsessfully");
            }
        }
        public void PurgeMessages()
        {
            PurgeQueueRequest request = new PurgeQueueRequest
            {
                QueueUrl = "https://sqs.us-east-1.amazonaws.com/491483104165/newappqueue"
            };

            var response = client.PurgeQueue(request);

            if (response.HttpStatusCode.IsSuccess())
            {
                Console.WriteLine($"Message(s) queued successfully");
            }
        }
示例#5
0
        public void AllDeleteMessagesByQueueUrl()
        {
            PurgeQueueRequest request = new PurgeQueueRequest
            {
                QueueUrl = queueURL
            };

            var response = client.PurgeQueue(request);

            if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("All Messages Removed");
            }
            Console.ReadLine();
        }
        public CloudPlatformResponse DeleteMessages(CloudPlatformRequest request)
        {
            var sqsClient  = new AmazonSQSClient();
            var cpResponse = new CloudPlatformResponse();

            try
            {
                var queueUrl = sqsClient.GetQueueUrl(request.Source).QueueUrl;
                var response = sqsClient.PurgeQueue(queueUrl);
                cpResponse.HttpStatusCode = response.HttpStatusCode;
            }
            catch (PurgeQueueInProgressException)
            {
                //retry
                Thread.Sleep(TimeSpan.FromSeconds(60));
                cpResponse.HttpStatusCode = HttpStatusCode.InternalServerError;
            }
            return(cpResponse);
        }
示例#7
0
        /// <summary>
        /// Purges the specified queue name.
        /// </summary>
        public void Purge()
        {
            try
            {
                using (var client = new AmazonSQSClient())
                {
                    _logger.InfoFormat("SqsMessageConsumer: Purging the queue {0}", _queueUrl);

                    client.PurgeQueue(_queueUrl);

                    _logger.InfoFormat("SqsMessageConsumer: Purged the queue {0}", _queueUrl);
                }
            }
            catch (Exception exception)
            {
                _logger.ErrorException("SqsMessageConsumer: Error purging queue {0}", exception, _queueUrl);
                throw;
            }
        }
示例#8
0
        public void SetQueueConfigurationTests()
        {
            var filterRule = new FilterRule("Prefix", "test/");
            var s3Config   = new AmazonS3Config();

            using (var s3Client = new AmazonS3Client(s3Config))
                using (var sqsClient = new AmazonSQSClient())
                {
                    var createResponse = sqsClient.CreateQueue("events-test-" + DateTime.Now.Ticks);
                    var bucketName     = S3TestUtils.CreateBucket(s3Client);
                    try
                    {
                        var queueArn = sqsClient.AuthorizeS3ToSendMessage(createResponse.QueueUrl, bucketName);

                        PutBucketNotificationRequest putRequest = new PutBucketNotificationRequest
                        {
                            BucketName          = bucketName,
                            QueueConfigurations = new List <QueueConfiguration>
                            {
                                new QueueConfiguration
                                {
                                    Id     = "the-queue-test",
                                    Queue  = queueArn,
                                    Events = { EventType.ObjectCreatedPut },
                                    Filter = new Filter
                                    {
                                        S3KeyFilter = new S3KeyFilter
                                        {
                                            FilterRules = new List <FilterRule>
                                            {
                                                filterRule
                                            }
                                        }
                                    }
                                }
                            }
                        };

                        s3Client.PutBucketNotification(putRequest);

                        var getResponse = s3Client.GetBucketNotification(bucketName);

                        Assert.AreEqual(1, getResponse.QueueConfigurations.Count);
                        Assert.AreEqual(1, getResponse.QueueConfigurations[0].Events.Count);
                        Assert.AreEqual(EventType.ObjectCreatedPut, getResponse.QueueConfigurations[0].Events[0]);

                        Assert.IsNotNull(getResponse.QueueConfigurations[0].Filter);
                        Assert.IsNotNull(getResponse.QueueConfigurations[0].Filter.S3KeyFilter);
                        Assert.IsNotNull(getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules);
                        Assert.AreEqual(1, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules.Count);
                        Assert.AreEqual(filterRule.Name, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules[0].Name);
                        Assert.AreEqual(filterRule.Value, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules[0].Value);

                        Assert.AreEqual("the-queue-test", getResponse.QueueConfigurations[0].Id);
                        Assert.AreEqual(queueArn, getResponse.QueueConfigurations[0].Queue);

                        // Purge queue to remove test message sent configuration was setup.
                        sqsClient.PurgeQueue(createResponse.QueueUrl);
                        Thread.Sleep(TimeSpan.FromSeconds(1));

                        var putObjectRequest = new PutObjectRequest
                        {
                            BucketName  = bucketName,
                            Key         = "test/data.txt",
                            ContentBody = "Important Data"
                        };

                        s3Client.PutObject(putObjectRequest);

                        string messageBody = null;
                        for (int i = 0; i < 5 && messageBody == null; i++)
                        {
                            var receiveResponse = sqsClient.ReceiveMessage(new ReceiveMessageRequest {
                                QueueUrl = createResponse.QueueUrl, WaitTimeSeconds = 20
                            });
                            if (receiveResponse.Messages.Count != 0)
                            {
                                messageBody = receiveResponse.Messages[0].Body;
                            }
                        }


                        var evnt = S3EventNotification.ParseJson(messageBody);

                        Assert.AreEqual(1, evnt.Records.Count);
                        Assert.AreEqual(putObjectRequest.BucketName, evnt.Records[0].S3.Bucket.Name);
                        Assert.AreEqual(putObjectRequest.Key, evnt.Records[0].S3.Object.Key);
                        Assert.AreEqual(putObjectRequest.ContentBody.Length, evnt.Records[0].S3.Object.Size);
                        Assert.IsNotNull(evnt.Records[0].S3.Object.Sequencer);
                    }
                    finally
                    {
                        sqsClient.DeleteQueue(createResponse.QueueUrl);
                        AmazonS3Util.DeleteS3BucketWithObjects(s3Client, bucketName);
                    }
                }
        }
        public void SetQueueConfigurationTests()
        {
            var filterRule = new FilterRule("Prefix", "test/");
            var s3Config   = new AmazonS3Config();

            using (var s3Client = new AmazonS3Client(s3Config))
                using (var sqsClient = new AmazonSQSClient())
                    using (var stsClient = new AmazonSecurityTokenServiceClient())
                    {
                        var createResponse = sqsClient.CreateQueue("events-test-" + DateTime.Now.Ticks);
                        var bucketName     = S3TestUtils.CreateBucketWithWait(s3Client);

                        try
                        {
                            var queueArn = sqsClient.AuthorizeS3ToSendMessage(createResponse.QueueUrl, bucketName);

                            PutBucketNotificationRequest putRequest = new PutBucketNotificationRequest
                            {
                                BucketName          = bucketName,
                                QueueConfigurations = new List <QueueConfiguration>
                                {
                                    new QueueConfiguration
                                    {
                                        Id     = "the-queue-test",
                                        Queue  = queueArn,
                                        Events = { EventType.ObjectCreatedPut },
                                        Filter = new Filter
                                        {
                                            S3KeyFilter = new S3KeyFilter
                                            {
                                                FilterRules = new List <FilterRule>
                                                {
                                                    filterRule
                                                }
                                            }
                                        }
                                    }
                                }
                            };

                            s3Client.PutBucketNotification(putRequest);

                            var getResponse = S3TestUtils.WaitForConsistency(() =>
                            {
                                var res = s3Client.GetBucketNotification(bucketName);
                                return(res.QueueConfigurations?.Count > 0 && res.QueueConfigurations[0].Id == "the-queue-test" ? res : null);
                            });

                            var getAttributeResponse = sqsClient.GetQueueAttributes(new GetQueueAttributesRequest
                            {
                                QueueUrl       = createResponse.QueueUrl,
                                AttributeNames = new List <string> {
                                    "All"
                                }
                            });

                            var policy     = Policy.FromJson(getAttributeResponse.Policy);
                            var conditions = policy.Statements[0].Conditions;
                            Assert.AreEqual(2, conditions.Count);

                            var accountCondition = conditions.FirstOrDefault(x => string.Equals(x.ConditionKey, ConditionFactory.SOURCE_ACCOUNT_KEY));
                            Assert.IsNotNull(accountCondition);
                            Assert.AreEqual(ConditionFactory.StringComparisonType.StringEquals.ToString(), accountCondition.Type);
                            Assert.AreEqual(12, accountCondition.Values[0].Length);

                            var currentAccountId = stsClient.GetCallerIdentity(new GetCallerIdentityRequest()).Account;
                            Assert.AreEqual(currentAccountId, accountCondition.Values[0]);


                            Assert.AreEqual(1, getResponse.QueueConfigurations.Count);
                            Assert.AreEqual(1, getResponse.QueueConfigurations[0].Events.Count);
                            Assert.AreEqual(EventType.ObjectCreatedPut, getResponse.QueueConfigurations[0].Events[0]);

                            Assert.IsNotNull(getResponse.QueueConfigurations[0].Filter);
                            Assert.IsNotNull(getResponse.QueueConfigurations[0].Filter.S3KeyFilter);
                            Assert.IsNotNull(getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules);
                            Assert.AreEqual(1, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules.Count);
                            Assert.AreEqual(filterRule.Name, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules[0].Name);
                            Assert.AreEqual(filterRule.Value, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules[0].Value);

                            Assert.AreEqual("the-queue-test", getResponse.QueueConfigurations[0].Id);
                            Assert.AreEqual(queueArn, getResponse.QueueConfigurations[0].Queue);

                            // Purge queue to remove test message sent configuration was setup.
                            sqsClient.PurgeQueue(createResponse.QueueUrl);
                            //We must wait 60 seconds or the next message being sent to the queue could be deleted while the queue is being purged.
                            Thread.Sleep(TimeSpan.FromSeconds(60));

                            var putObjectRequest = new PutObjectRequest
                            {
                                BucketName  = bucketName,
                                Key         = "test/data.txt",
                                ContentBody = "Important Data"
                            };

                            s3Client.PutObject(putObjectRequest);

                            string messageBody = null;
                            for (int i = 0; i < 5 && messageBody == null; i++)
                            {
                                var receiveResponse = sqsClient.ReceiveMessage(new ReceiveMessageRequest {
                                    QueueUrl = createResponse.QueueUrl, WaitTimeSeconds = 20
                                });
                                if (receiveResponse.Messages.Count != 0)
                                {
                                    messageBody = receiveResponse.Messages[0].Body;
                                }
                            }


                            var evnt = S3EventNotification.ParseJson(messageBody);

                            Assert.AreEqual(1, evnt.Records.Count);
                            Assert.AreEqual(putObjectRequest.BucketName, evnt.Records[0].S3.Bucket.Name);
                            Assert.AreEqual(putObjectRequest.Key, evnt.Records[0].S3.Object.Key);
                            Assert.AreEqual(putObjectRequest.ContentBody.Length, evnt.Records[0].S3.Object.Size);
                            Assert.IsNotNull(evnt.Records[0].S3.Object.Sequencer);
                        }
                        finally
                        {
                            sqsClient.DeleteQueue(createResponse.QueueUrl);
                            AmazonS3Util.DeleteS3BucketWithObjects(s3Client, bucketName);
                        }
                    }
        }
示例#10
0
 public void Purge(string queueUrl)
 {
     _client.PurgeQueue(_queueUrl);
 }