Esempio n. 1
0
        private void PopulateArn()
        {
            var attributes = _sqsClient.GetQueueAttributes(new GetQueueAttributesRequest
            {
                AttributeNames = new List <string>(new string[] { SQSConstants.ATTRIBUTE_QUEUE_ARN }),
                QueueUrl       = QueueUrl
            });

            QueueArn = attributes.QueueARN;
        }
Esempio n. 2
0
        private static void QueueInfo()
        {
            GetQueueAttributesRequest request = new GetQueueAttributesRequest
            {
                QueueUrl       = QUEUE_URL,
                AttributeNames = new List <string>()
                {
                    "All"
                }
            };

            var response = client.GetQueueAttributes(request);

            Console.WriteLine("Attributes for queue ARN '" + response.QueueARN + "':");
            Console.WriteLine("  Approximate number of messages:" + response.ApproximateNumberOfMessages);
            Console.WriteLine("  Approximate number of messages delayed: " + response.ApproximateNumberOfMessagesDelayed);
            Console.WriteLine("  Approximate number of messages not visible: " + response.ApproximateNumberOfMessagesNotVisible);
            Console.WriteLine("  Queue created on: " + response.CreatedTimestamp);
            Console.WriteLine("  Delay seconds: " + response.DelaySeconds);
            Console.WriteLine("  Queue last modified on: " + response.LastModifiedTimestamp);
            Console.WriteLine("  Maximum message size: " + response.MaximumMessageSize);
            Console.WriteLine("  Message retention period: " + response.MessageRetentionPeriod);
            Console.WriteLine("  Visibility timeout: " + response.VisibilityTimeout);
            Console.WriteLine("  Policy: " + response.Policy);
            Console.WriteLine("  Attributes:");

            foreach (var attr in response.Attributes)
            {
                Console.WriteLine("    " + attr.Key + ": " + attr.Value);
            }
        }
Esempio n. 3
0
        public MessageQueueDetail Build <T>(string uri)
        {
            var listResponse = _client.ListQueues(new ListQueuesRequest().WithQueueNamePrefix(uri));
            var exists       = listResponse.ListQueuesResult.QueueUrl.Any();

            if (exists)
            {
                var queue    = _client.GetQueueUrl(new GetQueueUrlRequest().WithQueueName(uri));
                var request  = new GetQueueAttributesRequest().WithQueueUrl(queue.GetQueueUrlResult.QueueUrl);
                var response = _client.GetQueueAttributes(request);

                return(new MessageQueueDetail
                {
                    MessageCount = response.GetQueueAttributesResult.ApproximateNumberOfMessages,
                    Uri = queue.GetQueueUrlResult.QueueUrl,
                    Exists = true
                });
            }
            else
            {
                return(new MessageQueueDetail
                {
                    MessageCount = null,
                    Uri = uri,
                    Exists = false
                });
            }
        }
Esempio n. 4
0
 private static void MonitorQueue(Config config, AmazonSQSClient client)
 {
     foreach (var @group in config.Groups)
     {
         foreach (var queue in group.Queues)
         {
             try
             {
                 var url        = client.GetQueueUrl(queue);
                 var attributes = client.GetQueueAttributes(
                     new GetQueueAttributesRequest(url.QueueUrl,
                                                   new List <string> {
                     "ApproximateNumberOfMessages"
                 }));
                 if (attributes.ApproximateNumberOfMessages >= group.Threshold)
                 {
                     Console.ForegroundColor = ConsoleColor.Yellow;
                     Console.WriteLine($"{queue} has {attributes.ApproximateNumberOfMessages} messages");
                 }
             }
             catch (Exception e)
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine($"Monitor exeption: {e.Message}");
                 Console.ResetColor();
             }
         }
     }
 }
        public void subscribe_to_topic()
        {
            var getArnRequest = new GetQueueAttributesRequest().WithQueueUrl(_queueUrl).WithAttributeName("QueueArn");
            var clientArn     = _client.GetQueueAttributes(getArnRequest).GetQueueAttributesResult.Attribute[0].Value;

            var sns = Amazon.AWSClientFactory.CreateAmazonSNSClient("AKIAIN2KJH4QJIUV7CGQ",
                                                                    "18ypN0y7SGA+L0XDVMHm9lBVmQ2oF2bdm7CGIijA");

            var subscriptionRequest = new SubscribeRequest()
                                      .WithEndpoint(clientArn)
                                      .WithProtocol("sqs")
                                      .WithTopicArn("arn:aws:sns:us-east-1:451419498740:EventMessage");

            var response = sns.Subscribe(subscriptionRequest);

            Console.WriteLine(response.SubscribeResult.SubscriptionArn);
        }
Esempio n. 6
0
            void SetupTopicAndQueue()
            {
                long ticks = DateTime.Now.Ticks;

                // Setup SNS topic.
                snsClient = new AmazonSimpleNotificationServiceClient(
                    settings.AWSAccessKeyID,
                    settings.AWSSecretAccessKey,
                    RegionEndpoint.GetBySystemName(settings.AWSS3Region.SystemName));

                sqsClient = new AmazonSQSClient(
                    settings.AWSAccessKeyID,
                    settings.AWSSecretAccessKey,
                    RegionEndpoint.GetBySystemName(settings.AWSS3Region.SystemName));

                topicArn = snsClient.CreateTopic(new CreateTopicRequest {
                    Name = "GlacierDownload-" + ticks
                }).TopicArn;
                Debug.WriteLine($"topicArn: {topicArn}");

                var createQueueRequest = new CreateQueueRequest {
                    QueueName = "GlacierDownload-" + ticks
                };
                var createQueueResponse = sqsClient.CreateQueue(createQueueRequest);

                this.queueUrl = createQueueResponse.QueueUrl;
                Debug.WriteLine($"QueueURL: {this.queueUrl}");

                var getQueueAttributesRequest = new GetQueueAttributesRequest {
                    AttributeNames = new List <string> {
                        "QueueArn"
                    },
                    QueueUrl = this.queueUrl
                };
                var response = sqsClient.GetQueueAttributes(getQueueAttributesRequest);

                queueArn = response.QueueARN;
                Debug.WriteLine($"QueueArn: {queueArn}");

                // Setup the Amazon SNS topic to publish to the SQS queue.
                snsClient.Subscribe(new SubscribeRequest()
                {
                    Protocol = "sqs",
                    Endpoint = queueArn,
                    TopicArn = topicArn
                });

                // Add the policy to the queue so SNS can send messages to the queue.
                var policy = SQS_POLICY.Replace("{TopicArn}", topicArn).Replace("{QuernArn}", queueArn);

                sqsClient.SetQueueAttributes(new SetQueueAttributesRequest {
                    QueueUrl   = this.queueUrl,
                    Attributes = new Dictionary <string, string>
                    {
                        { QueueAttributeName.Policy, policy }
                    }
                });
            }
        static void SetupTopicAndQueue(Amazon.RegionEndpoint region)
        {
            long ticks = DateTime.Now.Ticks;

            // Setup SNS topic.
            s_snsClient = new AmazonSimpleNotificationServiceClient(region);
            s_sqsClient = new AmazonSQSClient(region);

            s_topicArn = s_snsClient.CreateTopic(new CreateTopicRequest
            {
                Name = "GlacierDownload-" + ticks
            }).TopicArn;

            WriteLogConsole("topicArn: " + s_topicArn);

            CreateQueueRequest createQueueRequest = new CreateQueueRequest();

            createQueueRequest.QueueName = "GlacierDownload-" + ticks;
            CreateQueueResponse createQueueResponse = s_sqsClient.CreateQueue(createQueueRequest);

            s_queueUrl = createQueueResponse.QueueUrl;

            WriteLogConsole("QueueURL: " + s_queueUrl);

            GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest();

            getQueueAttributesRequest.AttributeNames = new List <string> {
                "QueueArn"
            };
            getQueueAttributesRequest.QueueUrl = s_queueUrl;

            GetQueueAttributesResponse response = s_sqsClient.GetQueueAttributes(getQueueAttributesRequest);

            s_queueArn = response.QueueARN;
            WriteLogConsole("QueueArn: " + s_queueArn);

            // Setup the Amazon SNS topic to publish to the SQS queue.
            s_snsClient.Subscribe(new SubscribeRequest()
            {
                Protocol = "sqs",
                Endpoint = s_queueArn,
                TopicArn = s_topicArn
            });

            // Add the policy to the queue so SNS can send messages to the queue.
            var policy = SQS_POLICY.Replace("{TopicArn}", s_topicArn).Replace("{QuernArn}", s_queueArn);

            s_sqsClient.SetQueueAttributes(new SetQueueAttributesRequest()
            {
                QueueUrl   = s_queueUrl,
                Attributes = new Dictionary <string, string>
                {
                    { QueueAttributeName.Policy, policy }
                }
            });
        }
        public int GetApproximateNumberOfMessagesInQueue()
        {
            var qar = new GetQueueAttributesRequest();

            qar.QueueUrl       = _queueUrl;
            qar.AttributeNames = new List <string> {
                "ApproximateNumberOfMessages"
            };
            var attributes = _sqsClient.GetQueueAttributes(qar);

            return(attributes.ApproximateNumberOfMessages);
        }
Esempio n. 9
0
        static void SetupTopicAndQueue(Amazon.RegionEndpoint awsRegion)
        {
            long ticks = DateTime.Now.Ticks;

            // Setup SNS topic.
            snsClient = new AmazonSimpleNotificationServiceClient(awsRegion);
            sqsClient = new AmazonSQSClient(awsRegion);

            topicArn = snsClient.CreateTopic(new CreateTopicRequest {
                Name = "GlacierInventory-" + ticks
            }).TopicArn;
            Console.WriteLine($"topicArn: {topicArn}");

            CreateQueueRequest createQueueRequest = new CreateQueueRequest();

            createQueueRequest.QueueName = "GlacierInventory-" + ticks;
            CreateQueueResponse createQueueResponse = sqsClient.CreateQueue(createQueueRequest);

            queueUrl = createQueueResponse.QueueUrl;
            Console.WriteLine($"QueueURL: {queueUrl}");

            GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest();

            getQueueAttributesRequest.AttributeNames = new List <string> {
                "QueueArn"
            };
            getQueueAttributesRequest.QueueUrl = queueUrl;
            GetQueueAttributesResponse response = sqsClient.GetQueueAttributes(getQueueAttributesRequest);

            queueArn = response.QueueARN;
            Console.WriteLine($"QueueArn: {queueArn}");

            // Setup the Amazon SNS topic to publish to the SQS queue.
            snsClient.Subscribe(new SubscribeRequest()
            {
                Protocol = "sqs",
                Endpoint = queueArn,
                TopicArn = topicArn
            });

            // Add the policy to the queue so SNS can send messages to the queue.
            var policy = SQS_POLICY.Replace("{TopicArn}", topicArn).Replace("{QuernArn}", queueArn).Replace("{AccountArn}", accountArn);

            sqsClient.SetQueueAttributes(new SetQueueAttributesRequest()
            {
                QueueUrl   = queueUrl,
                Attributes = new Dictionary <string, string>
                {
                    { QueueAttributeName.Policy, policy }
                }
            });
        }
Esempio n. 10
0
        public static int QueueLengthByQueueUrl(string queueUrl)
        {
            using (var client = new AmazonSQSClient(Settings.AccessKey, Settings.Secret))
            {
                var request = new GetQueueAttributesRequest
                {
                    QueueUrl       = queueUrl,
                    AttributeNames = new List <string> {
                        "ApproximateNumberOfMessages"
                    }
                };

                return(client.GetQueueAttributes(request).ApproximateNumberOfMessages);
            }
        }
Esempio n. 11
0
        private static int GetTotalMessages(SqsMessage pQueue)
        {
            AmazonSQSClient           sqs = SqsConfig.Initialize;
            GetQueueAttributesRequest queueAttributesRequest = new GetQueueAttributesRequest(pQueue.QueueUrl, new List <string> {
                "All"
            });
            GetQueueAttributesResult queueAttributesResult = sqs.GetQueueAttributes(queueAttributesRequest);

            if (queueAttributesResult.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new ApplicationException("Problems in the endpoint communication!");
            }

            return(queueAttributesResult.ApproximateNumberOfMessages);
        }
Esempio n. 12
0
        public static void SQSGetQueueAttributes()
        {
            #region SQSGetQueueAttributes
            var client = new AmazonSQSClient();

            var request = new GetQueueAttributesRequest
            {
                QueueUrl       = "https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyTestQueue",
                AttributeNames = new List <string>()
                {
                    "All"
                }
            };

            var response = client.GetQueueAttributes(request);

            Console.WriteLine("Attributes for queue ARN '" + response.QueueARN + "':");
            Console.WriteLine("  Approximate number of messages:" +
                              response.ApproximateNumberOfMessages);
            Console.WriteLine("  Approximate number of messages delayed: " +
                              response.ApproximateNumberOfMessagesDelayed);
            Console.WriteLine("  Approximate number of messages not visible: " +
                              response.ApproximateNumberOfMessagesNotVisible);
            Console.WriteLine("  Queue created on: " + response.CreatedTimestamp);
            Console.WriteLine("  Delay seconds: " + response.DelaySeconds);
            Console.WriteLine("  Queue last modified on: " +
                              response.LastModifiedTimestamp);
            Console.WriteLine("  Maximum message size: " +
                              response.MaximumMessageSize);
            Console.WriteLine("  Message retention period: " +
                              response.MessageRetentionPeriod);
            Console.WriteLine("  Visibility timeout: " + response.VisibilityTimeout);
            Console.WriteLine("  Policy: " + response.Policy);
            Console.WriteLine("  Attributes:");

            foreach (var attr in response.Attributes)
            {
                Console.WriteLine("    " + attr.Key + ": " + attr.Value);
            }
            #endregion

            Console.ReadLine();
        }
Esempio n. 13
0
        public virtual string GetQueueArn(AmazonSQSClient sqsClient, string queueUrl)
        {
            string queueArn;
            // Construct a GetQueueAttributesRequest for the URL to retrieve the ARN
            var getQueueAttributesRequest = new GetQueueAttributesRequest
            {
                QueueUrl       = queueUrl,
                AttributeNames =
                {
                    "QueueArn"
                }
            };

            // Submit the request
            GetQueueAttributesResponse getQueueAttributesResponse =
                sqsClient.GetQueueAttributes(getQueueAttributesRequest);

            // Add the discovered ARN to the queueArnList variable.
            queueArn = getQueueAttributesResponse.QueueARN;
            return(queueArn);
        }
Esempio n. 14
0
        private static Topic SetupTopicAndSubscriptions(string topicFileName, string outputDirectory, string regionSystemName, string archiveId = null, string filename = null)
        {
            var topic = new Topic {
                TopicFileName   = topicFileName,
                OutputDirectory = outputDirectory,
                ArchiveId       = archiveId,
                FileName        = filename,
                DateRequested   = DateTime.Now
            };
            long ticks    = DateTime.Now.Ticks;
            var  settings = SettingsManager.GetSettings();

            #region Setup SNS topic
            var snsClient = new AmazonSimpleNotificationServiceClient(
                settings.AWSAccessKeyID,
                settings.AWSSecretAccessKey,
                RegionEndpoint.GetBySystemName(regionSystemName));

            var sqsClient = new AmazonSQSClient(
                settings.AWSAccessKeyID,
                settings.AWSSecretAccessKey,
                RegionEndpoint.GetBySystemName(regionSystemName));

            var topicArn = snsClient.CreateTopic(new CreateTopicRequest {
                Name = "GlacierDownload-" + ticks
            }).TopicArn;
            //Debug.WriteLine($"topicArn: {topicArn}");
            topic.TopicARN = topicArn;
            #endregion

            #region Setup SQS queue
            var createQueueRequest = new CreateQueueRequest {
                QueueName = "GlacierDownload-" + ticks
            };
            var createQueueResponse = sqsClient.CreateQueue(createQueueRequest);
            var queueUrl            = createQueueResponse.QueueUrl;
            //Debug.WriteLine($"QueueURL: {queueUrl}");
            topic.QueueUrl = queueUrl;

            var getQueueAttributesRequest = new GetQueueAttributesRequest
            {
                AttributeNames = new List <string> {
                    "QueueArn"
                },
                QueueUrl = queueUrl
            };
            var response = sqsClient.GetQueueAttributes(getQueueAttributesRequest);
            var queueArn = response.QueueARN;
            Debug.WriteLine($"QueueArn: {queueArn}");
            topic.QueueARN = queueArn;
            #endregion

            // Setup the Amazon SNS topic to publish to the SQS queue.
            // TODO SMS subscription
            snsClient.Subscribe(new SubscribeRequest()
            {
                Protocol = "sqs",
                Endpoint = queueArn,
                TopicArn = topicArn
            });

            // Add the policy to the queue so SNS can send messages to the queue.
            var policy = SQS_POLICY.Replace("{TopicArn}", topicArn).Replace("{QuernArn}", queueArn);

            sqsClient.SetQueueAttributes(new SetQueueAttributesRequest
            {
                QueueUrl   = queueUrl,
                Attributes = new Dictionary <string, string>
                {
                    { QueueAttributeName.Policy, policy }
                }
            });

            return(topic);
        }
Esempio n. 15
0
        public static void Main(string[] args)
        {
            const bool useEasySubscription = false;
            var        sns = new AmazonSimpleNotificationServiceClient();
            var        sqs = new AmazonSQSClient();

            string nameOfNewTopic = args[0];  //Sanitise this to ensure no illegal characters.
            var    emailAddress   = args[1];

            try
            {
                var topicArn = sns.CreateTopic(
                    new CreateTopicRequest {
                    Name = nameOfNewTopic
                }).TopicArn;

                sns.SetTopicAttributes(new SetTopicAttributesRequest
                {
                    TopicArn       = topicArn,
                    AttributeName  = "DisplayName",
                    AttributeValue = "Sample Notifications"
                });

                RetrieveAllTopics(sns);

                if (string.IsNullOrEmpty(emailAddress) == false)
                {
                    // Subscribe an endpoint - in this case, an email address
                    Console.WriteLine();
                    Console.WriteLine("Subscribing email address {0} to topic...", emailAddress);
                    sns.Subscribe(new SubscribeRequest
                    {
                        TopicArn = topicArn,
                        Protocol = "email",
                        Endpoint = emailAddress
                    });

                    // When using email, recipient must confirm subscription
                    Console.WriteLine();
                    Console.WriteLine("Please check your email and press enter when you are subscribed...");
                    Console.ReadLine();
                }

                Console.WriteLine();
                var sqsRequest = new CreateQueueRequest
                {
                    QueueName = "MyExperimentQueue"
                };

                var createQueueResponse = sqs.CreateQueue(sqsRequest);
                var myQueueUrl          = createQueueResponse.QueueUrl;

                var myQueueArn = sqs.GetQueueAttributes(
                    new GetQueueAttributesRequest
                {
                    QueueUrl       = myQueueUrl,
                    AttributeNames = new List <string> {
                        "All"
                    }
                }).QueueARN;

                ListQueues(sqs);

                if (myQueueArn != null)
                {
                    //https://aws.amazon.com/blogs/developer/subscribing-an-sqs-queue-to-an-sns-topic/

                    if (useEasySubscription)
                    {
                        sns.SubscribeQueue(topicArn, sqs, myQueueUrl);
                    }
                    else
                    {
                        var subscribeRequest = new SubscribeRequest(topicArn, "SQS", myQueueArn);

                        sns.Subscribe(subscribeRequest);

                        ActionIdentifier[] actions = new ActionIdentifier[2];
                        actions[0] = SQSActionIdentifiers.SendMessage;
                        actions[1] = SQSActionIdentifiers.ReceiveMessage;

                        Policy sqsPolicy = new Policy()
                                           .WithStatements(new Statement(Statement.StatementEffect.Allow)
                                                           .WithPrincipals(Principal.AllUsers)
                                                           .WithResources(new Resource(myQueueArn))
                                                           .WithConditions(ConditionFactory.NewSourceArnCondition(topicArn))
                                                           .WithActionIdentifiers(actions));


                        var attributeDictionary = new Dictionary <string, string>();
                        attributeDictionary.Add("Policy", sqsPolicy.ToJson());
                        var attributes = new SetQueueAttributesRequest {
                            QueueUrl = myQueueUrl, Attributes = attributeDictionary
                        };

                        sqs.SetQueueAttributes(attributes);
                    }

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    // Publish message
                    Console.WriteLine();
                    Console.WriteLine("Publishing message to topic...");
                    sns.Publish(new PublishRequest
                    {
                        Subject  = "Test",
                        Message  = "Testing testing 1 2 3",
                        TopicArn = topicArn
                    });
                    var receivedMessageResponse = ReceiveMessage(sqs, myQueueUrl);

                    DeleteReceivedMessage(receivedMessageResponse, myQueueUrl, sqs);
                }

                //Console.WriteLine();
                //Console.WriteLine("Deleting topic...");
                //sns.DeleteTopic(new DeleteTopicRequest
                //{
                //    TopicArn = topicArn
                //});
            }
            catch (AmazonSimpleNotificationServiceException ex)
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
                Console.WriteLine("Response Status Code: " + ex.StatusCode);
                Console.WriteLine("Error Code: " + ex.ErrorCode);
                Console.WriteLine("Error Type: " + ex.ErrorType);
                Console.WriteLine("Request ID: " + ex.RequestId);
            }

            Console.WriteLine();
            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            bool   ClearSQS               = false;
            int    MaxDelete              = 10;
            string AWSProfileName         = "default";
            string AWSCredentialsFilepath = "credentials.ini";
            string AWSSQSServiceURL       = "https://sqs.us-east-1.amazonaws.com";
            string AWSSQSQueueUrl         = "";

            foreach (var arg in args)
            {
                if (string.Compare("-ClearSQS", arg, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    ClearSQS = true;
                }
                else if (arg.StartsWith("-MaxDelete", StringComparison.OrdinalIgnoreCase))
                {
                    var ArgSplit = arg.Split('=');
                    if (ArgSplit.Length == 2)
                    {
                        MaxDelete = int.Parse(ArgSplit[1]);
                    }
                }
                else if (arg.StartsWith("-AWSProfileName", StringComparison.OrdinalIgnoreCase))
                {
                    var ArgSplit = arg.Split('=');
                    if (ArgSplit.Length == 2)
                    {
                        AWSProfileName = ArgSplit[1];
                    }
                }
                else if (arg.StartsWith("-AWSCredentialsFilepath", StringComparison.OrdinalIgnoreCase))
                {
                    var ArgSplit = arg.Split('=');
                    if (ArgSplit.Length == 2)
                    {
                        AWSCredentialsFilepath = ArgSplit[1].Trim('\"');
                    }
                }
                else if (arg.StartsWith("-AWSSQSServiceURL", StringComparison.OrdinalIgnoreCase))
                {
                    var ArgSplit = arg.Split('=');
                    if (ArgSplit.Length == 2)
                    {
                        AWSSQSServiceURL = ArgSplit[1];
                    }
                }
                else if (arg.StartsWith("-AWSSQSQueueUrl", StringComparison.OrdinalIgnoreCase))
                {
                    var ArgSplit = arg.Split('=');
                    if (ArgSplit.Length == 2)
                    {
                        AWSSQSQueueUrl = ArgSplit[1];
                    }
                }
            }

            if (ClearSQS)
            {
                Console.WriteLine("DELETING SQS QUEUE RECORDS");

                AWSCredentials Credentials = new StoredProfileAWSCredentials(AWSProfileName, AWSCredentialsFilepath);

                AmazonSQSConfig SqsConfig = new AmazonSQSConfig
                {
                    ServiceURL = AWSSQSServiceURL
                };

                var SqsClient = new AmazonSQSClient(Credentials, SqsConfig);

                var AttribRequest = new GetQueueAttributesRequest
                {
                    QueueUrl       = AWSSQSQueueUrl,
                    AttributeNames = new List <string>
                    {
                        "ApproximateNumberOfMessages"
                    }
                };

                var AttribResponse = SqsClient.GetQueueAttributes(AttribRequest);
                Console.WriteLine("INITIAL QUEUE SIZE " + AttribResponse.ApproximateNumberOfMessages);

                int DeletedCount = 0;

                ConsoleColor NormalColor = Console.ForegroundColor;

                while (DeletedCount < MaxDelete)
                {
                    int RemainingDeletes = MaxDelete - DeletedCount;

                    var ReceiveRequest = new ReceiveMessageRequest
                    {
                        QueueUrl            = AWSSQSQueueUrl,
                        MaxNumberOfMessages = Math.Min(10, RemainingDeletes)
                    };

                    var ReceiveResponse = SqsClient.ReceiveMessage(ReceiveRequest);

                    if (ReceiveResponse.Messages.Count == 0)
                    {
                        break;
                    }

                    foreach (var Message in ReceiveResponse.Messages)
                    {
                        if (Message != null)
                        {
                            DeletedCount++;

                            var DeleteRequest = new DeleteMessageRequest
                            {
                                QueueUrl      = AWSSQSQueueUrl,
                                ReceiptHandle = Message.ReceiptHandle
                            };

                            var DeleteResponse = SqsClient.DeleteMessage(DeleteRequest);
                            if (DeleteResponse.HttpStatusCode == HttpStatusCode.OK)
                            {
                                Console.ForegroundColor = NormalColor;
                                Console.WriteLine("Deleted record " + Message.Body);
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("FAILED to delete record " + Message.Body);
                            }
                        }
                    }
                }

                AttribRequest = new GetQueueAttributesRequest
                {
                    QueueUrl       = AWSSQSQueueUrl,
                    AttributeNames = new List <string>
                    {
                        "ApproximateNumberOfMessages"
                    }
                };

                AttribResponse          = SqsClient.GetQueueAttributes(AttribRequest);
                Console.ForegroundColor = NormalColor;
                Console.WriteLine("FINAL QUEUE SIZE " + AttribResponse.ApproximateNumberOfMessages);
            }

#if DEBUG
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("PRESS ANY KEY");
            Console.ReadKey();
#endif
        }
        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);
                        }
                    }
        }
Esempio n. 18
0
        public void TestQueueSubscription()
        {
            string topicArn = null;
            string queueUrl = null;

            try
            {
                topicArn = CreateTopic();
                queueUrl = CreateQueue();

                var            subscriptionArn = SubscribeQueue(topicArn, queueUrl);
                var            publishRequest  = GetPublishRequest(topicArn);
                List <Message> messages        = PublishToSNSAndReceiveMessages(publishRequest, topicArn, queueUrl);

                Assert.AreEqual(1, messages.Count);
                var message = messages[0];

                string bodyJson = GetBodyJson(message);

                var json           = ThirdParty.Json.LitJson.JsonMapper.ToObject(bodyJson);
                var messageText    = json["Message"];
                var messageSubject = json["Subject"];
                Assert.AreEqual(publishRequest.Message, messageText.ToString());
                Assert.AreEqual(publishRequest.Subject, messageSubject.ToString());
                var messageAttributes = json["MessageAttributes"];
                Assert.AreEqual(publishRequest.MessageAttributes.Count, messageAttributes.Count);
                foreach (var ma in publishRequest.MessageAttributes)
                {
                    var name  = ma.Key;
                    var value = ma.Value;
                    Assert.IsTrue(messageAttributes.PropertyNames.Contains(name, StringComparer.Ordinal));
                    var jsonAttribute = messageAttributes[name];
                    var jsonType      = jsonAttribute["Type"].ToString();
                    var jsonValue     = jsonAttribute["Value"].ToString();
                    Assert.IsNotNull(jsonType);
                    Assert.IsNotNull(jsonValue);
                    Assert.AreEqual(value.DataType, jsonType);
                    Assert.AreEqual(value.DataType != "Binary"
                                        ? value.StringValue
                                        : Convert.ToBase64String(value.BinaryValue.ToArray()), jsonValue);
                }

                sqsClient.DeleteMessage(new DeleteMessageRequest
                {
                    QueueUrl      = queueUrl,
                    ReceiptHandle = messages[0].ReceiptHandle
                });

                // This will unsubscribe but leave the policy in place.
                Client.Unsubscribe(new UnsubscribeRequest
                {
                    SubscriptionArn = subscriptionArn
                });

                // Subscribe again to see if this affects the policy.
                Client.SubscribeQueue(topicArn, sqsClient, queueUrl);

                Client.Publish(new PublishRequest
                {
                    TopicArn = topicArn,
                    Message  = "Test Message again"
                });

                messages = sqsClient.ReceiveMessage(new ReceiveMessageRequest
                {
                    QueueUrl        = queueUrl,
                    WaitTimeSeconds = 20
                }).Messages;

                Assert.AreEqual(1, messages.Count);

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

                var policy = Policy.FromJson(getAttributeResponse.Policy);
                Assert.AreEqual(1, policy.Statements.Count);
            }
            finally
            {
                if (topicArn != null)
                {
                    Client.DeleteTopic(new DeleteTopicRequest {
                        TopicArn = topicArn
                    });
                }

                if (queueUrl != null)
                {
                    sqsClient.DeleteQueue(new DeleteQueueRequest {
                        QueueUrl = queueUrl
                    });
                }
            }
        }
Esempio n. 19
0
        public void TestQueueSubscription()
        {
            // create new topic
            var topicName          = "dotnetsdkTopic" + DateTime.Now.Ticks;
            var createTopicRequest = new CreateTopicRequest
            {
                Name = topicName
            };
            var createTopicResult = Client.CreateTopic(createTopicRequest);
            var topicArn          = createTopicResult.TopicArn;

            var queueName = "dotnetsdkQueue-" + DateTime.Now.Ticks;
            var queueUrl  = sqsClient.CreateQueue(new CreateQueueRequest
            {
                QueueName = queueName
            }).QueueUrl;

            try
            {
                var subscriptionARN = Client.SubscribeQueue(topicArn, sqsClient, queueUrl);

                // Sleep to wait for the subscribe to complete.
                Thread.Sleep(TimeSpan.FromSeconds(5));

                var publishRequest = new PublishRequest
                {
                    TopicArn          = topicArn,
                    Subject           = "Test Subject",
                    Message           = "Test Message",
                    MessageAttributes = new Dictionary <string, SNSMessageAttributeValue>
                    {
                        { "Color", new SNSMessageAttributeValue {
                              StringValue = "Red", DataType = "String"
                          } },
                        { "Binary", new SNSMessageAttributeValue {
                              DataType = "Binary", BinaryValue = new MemoryStream(Encoding.UTF8.GetBytes("Yes please"))
                          } },
                        { "Prime", new SNSMessageAttributeValue {
                              StringValue = "31", DataType = "Number"
                          } },
                    }
                };
                Client.Publish(publishRequest);

                var messages = sqsClient.ReceiveMessage(new ReceiveMessageRequest
                {
                    QueueUrl        = queueUrl,
                    WaitTimeSeconds = 20
                }).Messages;

                Assert.AreEqual(1, messages.Count);
                var message = messages[0];

                string bodyJson;
                // Handle some accounts returning message body as base 64 encoded.
                if (message.Body.Trim()[0] == '{')
                {
                    bodyJson = message.Body;
                }
                else
                {
                    bodyJson = Encoding.UTF8.GetString(Convert.FromBase64String(message.Body));
                }

                var json           = ThirdParty.Json.LitJson.JsonMapper.ToObject(bodyJson);
                var messageText    = json["Message"];
                var messageSubject = json["Subject"];
                Assert.AreEqual(publishRequest.Message, messageText.ToString());
                Assert.AreEqual(publishRequest.Subject, messageSubject.ToString());
                var messageAttributes = json["MessageAttributes"];
                Assert.AreEqual(publishRequest.MessageAttributes.Count, messageAttributes.Count);
                foreach (var ma in publishRequest.MessageAttributes)
                {
                    var name  = ma.Key;
                    var value = ma.Value;
                    Assert.IsTrue(messageAttributes.PropertyNames.Contains(name, StringComparer.Ordinal));
                    var jsonAttribute = messageAttributes[name];
                    var jsonType      = jsonAttribute["Type"].ToString();
                    var jsonValue     = jsonAttribute["Value"].ToString();
                    Assert.IsNotNull(jsonType);
                    Assert.IsNotNull(jsonValue);
                    Assert.AreEqual(value.DataType, jsonType);
                    Assert.AreEqual(value.DataType != "Binary"
                                        ? value.StringValue
                                        : Convert.ToBase64String(value.BinaryValue.ToArray()), jsonValue);
                }

                sqsClient.DeleteMessage(new DeleteMessageRequest
                {
                    QueueUrl      = queueUrl,
                    ReceiptHandle = messages[0].ReceiptHandle
                });

                // This will unsubscribe but leave the policy in place.
                Client.Unsubscribe(new UnsubscribeRequest
                {
                    SubscriptionArn = subscriptionARN
                });

                // Subscribe again to see if this affects the policy.
                Client.SubscribeQueue(topicArn, sqsClient, queueUrl);

                Client.Publish(new PublishRequest
                {
                    TopicArn = topicArn,
                    Message  = "Test Message again"
                });

                messages = sqsClient.ReceiveMessage(new ReceiveMessageRequest
                {
                    QueueUrl        = queueUrl,
                    WaitTimeSeconds = 20
                }).Messages;

                Assert.AreEqual(1, messages.Count);

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

                var policy = Policy.FromJson(getAttributeResponse.Policy);
                Assert.AreEqual(1, policy.Statements.Count);
            }
            finally
            {
                Client.DeleteTopic(new DeleteTopicRequest {
                    TopicArn = topicArn
                });
                sqsClient.DeleteQueue(new DeleteQueueRequest {
                    QueueUrl = queueUrl
                });
            }
        }