Example #1
0
        static async Task MainAsync(IConfigurationRoot configuration)
        {
            var serviceUrl = configuration.GetConnectionString("");
            var queueName  = configuration.GetSection("").Value;
            var sqsConfig  = new AmazonSQSConfig {
                ServiceURL = serviceUrl
            };
            AmazonSQSClient sqsClient = new AmazonSQSClient(sqsConfig);
            var             request   = new CreateQueueRequest
            {
                QueueName  = queueName,
                Attributes = new Dictionary <string, string>
                {
                    { "ReceiveMessageWaitTimeSeconds", "20" },
                }
            };
            var createQueueResponse = await sqsClient.CreateQueueAsync(request);

            if (createQueueResponse.HttpStatusCode == HttpStatusCode.OK)
            {
                var setQueueAttributeRequest = new SetQueueAttributesRequest
                {
                    Attributes = new Dictionary <string, string>
                    {
                        {
                            "RedrivePolicy",
                            @"{ ""deadLetterTargetArn"" : ""DEAD_LETTER_QUEUE_ARN"", ""maxReceiveCount"" : ""10""}"
                        }
                    },
                    QueueUrl = createQueueResponse.QueueUrl
                };
                await sqsClient.SetQueueAttributesAsync(setQueueAttributeRequest);
                await ConsumeQueue(sqsClient, createQueueResponse.QueueUrl);
            }
        }
Example #2
0
        static async Task CreateQueue(AmazonSQSClient client, string queueName, bool fifo)
        {
            var createQueueRequest = new CreateQueueRequest
            {
                QueueName = queueName
            };

            var createQueueResponse = await client.CreateQueueAsync(createQueueRequest);

            queueUrls.TryAdd(queueName, createQueueResponse.QueueUrl);

            var setQueueAttributesRequest = new SetQueueAttributesRequest
            {
                QueueUrl = createQueueResponse.QueueUrl
            };

            setQueueAttributesRequest.Attributes.Add(QueueAttributeName.MessageRetentionPeriod, TimeSpan.FromDays(4).TotalSeconds.ToString(CultureInfo.InvariantCulture));

            await client.SetQueueAttributesAsync(setQueueAttributesRequest);

            if (fifo)
            {
                var fifoQueueName = queueName + "-delay.fifo";

                var createFifoQueueRequest = new CreateQueueRequest
                {
                    QueueName  = fifoQueueName,
                    Attributes = new Dictionary <string, string> {
                        { "FifoQueue", "true" }
                    }
                };

                var createFifoQueueResponse = await client.CreateQueueAsync(createFifoQueueRequest);

                queueUrls.TryAdd(fifoQueueName, createFifoQueueResponse.QueueUrl);

                var setFifoQueueAttributesRequest = new SetQueueAttributesRequest
                {
                    QueueUrl = createFifoQueueResponse.QueueUrl
                };

                setFifoQueueAttributesRequest.Attributes.Add(QueueAttributeName.MessageRetentionPeriod, TimeSpan.FromDays(4).TotalSeconds.ToString(CultureInfo.InvariantCulture));
                setFifoQueueAttributesRequest.Attributes.Add(QueueAttributeName.DelaySeconds, delaySeconds.ToString(CultureInfo.InvariantCulture));

                await client.SetQueueAttributesAsync(setFifoQueueAttributesRequest);
            }
        }
        /// <summary>
        ///     Creates the queue with the given name
        /// </summary>
        public void CreateQueue(string address)
        {
            m_log.Info("Creating queue {queueName} on region {regionEndpoint}", address, m_AmazonInternalSettings.AmazonSqsConfig.RegionEndpoint);

            var amazonSqsConfig = m_AmazonInternalSettings.AmazonSqsConfig;
            var awsCredentials  = m_AmazonInternalSettings.AmazonCredentialsFactory.Create();

            using (var client = new AmazonSQSClient(awsCredentials, amazonSqsConfig))
            {
                var queueName = GetQueueNameFromAddress(address);

                // Check if queue exists
                try
                {
                    var request = new GetQueueUrlRequest(queueName);
                    // See http://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/SQS/TSQSGetQueueUrlRequest.html for options
                    var getQueueUrlTask = client.GetQueueUrlAsync(request);
                    AsyncHelpers.RunSync(() => getQueueUrlTask);
                    var getQueueUrlResponse = getQueueUrlTask.Result;

                    if (getQueueUrlResponse.HttpStatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception($"Could not check for existing queue '{queueName}' - got HTTP {getQueueUrlResponse.HttpStatusCode}");
                    }

                    var visibilityTimeout = ((int)m_AmazonInternalSettings.AmazonPeekLockDuration.PeekLockDuration.TotalSeconds).ToString(CultureInfo.InvariantCulture);

                    // See http://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/SQS/TSQSSetQueueAttributesRequest.html for options
                    var setAttributesTask = client.SetQueueAttributesAsync(getQueueUrlResponse.QueueUrl, new Dictionary <string, string> {
                        ["VisibilityTimeout"] = visibilityTimeout
                    });
                    AsyncHelpers.RunSync(() => setAttributesTask);
                    var setAttributesResponse = setAttributesTask.Result;

                    if (setAttributesResponse.HttpStatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception($"Could not set attributes for queue '{queueName}' - got HTTP {setAttributesResponse.HttpStatusCode}");
                    }
                }
                catch (QueueDoesNotExistException)
                {
                    // See http://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/SQS/TSQSCreateQueueRequest.html for options
                    var createQueueRequest = new CreateQueueRequest(queueName)
                    {
                        Attributes = { ["VisibilityTimeout"] = ((int)m_AmazonInternalSettings.AmazonPeekLockDuration.PeekLockDuration.TotalSeconds).ToString(CultureInfo.InvariantCulture) }
                    };
                    var task = client.CreateQueueAsync(createQueueRequest);
                    AsyncHelpers.RunSync(() => task);
                    var response = task.Result;

                    if (response.HttpStatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception($"Could not create queue '{queueName}' - got HTTP {response.HttpStatusCode}");
                    }
                }
            }
        }
Example #4
0
        public async Task OnExistingQueue()
        {
            // Create service client using the SDK's default logic for determining AWS credentials and region to use.
            // For information configuring service clients checkout the .NET developer guide: https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config.html
            AmazonSQSClient client = new AmazonSQSClient();

            var request = new SetQueueAttributesRequest
            {
                Attributes = new Dictionary <string, string>
                {
                    { "ReceiveMessageWaitTimeSeconds", "20" }
                },
                QueueUrl = "SQS_QUEUE_URL"
            };

            var response = await client.SetQueueAttributesAsync(request);
        }
Example #5
0
        public async Task DeadLetterQueueExample()
        {
            // Create service client using the SDK's default logic for determining AWS credentials and region to use.
            // For information configuring service clients checkout the .NET developer guide: https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config.html
            AmazonSQSClient client = new AmazonSQSClient();

            var setQueueAttributeRequest = new SetQueueAttributesRequest
            {
                Attributes = new Dictionary <string, string>
                {
                    { "RedrivePolicy", @"{ ""deadLetterTargetArn"" : ""DEAD_LETTER_QUEUE_ARN"", ""maxReceiveCount"" : ""10""}" }
                },
                QueueUrl = "SOURCE_QUEUE_URL"
            };

            await client.SetQueueAttributesAsync(setQueueAttributeRequest);
        }
Example #6
0
        public async Task <ISubscription> SubscribeAsync(String topic)
        {
            var amazonTopic = await amazonSnsClient.CreateTopicAsync(new CreateTopicRequest()
            {
                Name = topic
            });

            var queue = await amazonSqsClient.CreateQueueAsync(new CreateQueueRequest()
            {
                QueueName = Guid.NewGuid().ToString()
            });

            var queueAttributes = await amazonSqsClient.GetQueueAttributesAsync(new GetQueueAttributesRequest()
            {
                AttributeNames = new List <String>(new String[] { "QueueArn" }),
                QueueUrl       = queue.QueueUrl
            });

            var policy = new Policy()
                         .WithStatements(
                new Statement(Statement.StatementEffect.Allow)
                .WithPrincipals(Principal.AllUsers)
                .WithConditions(ConditionFactory.NewSourceArnCondition(amazonTopic.TopicArn))
                .WithResources(new Resource(queueAttributes.QueueARN))
                .WithActionIdentifiers(SQSActionIdentifiers.SendMessage));

            await amazonSqsClient.SetQueueAttributesAsync(queue.QueueUrl, new Dictionary <String, String>()
            {
                ["Policy"] = policy.ToJson()
            });

            await amazonSnsClient.SubscribeAsync(new SubscribeRequest()
            {
                Endpoint = queueAttributes.QueueARN,
                Protocol = "sqs",
                TopicArn = amazonTopic.TopicArn
            });

            var subscription = amazonSubscriptionFactory.Create(queue.QueueUrl);

            return(subscription);
        }
        // Queue settings
        private async Task SetQueueAttributes(string queueUrl)
        {
            var queueSettings = new Dictionary <string, string>();

            queueSettings.Add(QueueAttributeName.DelaySeconds,
                              MESSAGE_SEND_DELAY.TotalSeconds.ToString());
            queueSettings.Add(QueueAttributeName.MaximumMessageSize, MAX_MESSAGE_SIZE.ToString());
            queueSettings.Add(QueueAttributeName.MessageRetentionPeriod,
                              MESSAGE_RETENTION_PERIOD.TotalSeconds.ToString());
            queueSettings.Add(QueueAttributeName.ReceiveMessageWaitTimeSeconds,
                              RECEIVED_MESSAGE_WAIT_TIME.TotalSeconds.ToString());
            queueSettings.Add(QueueAttributeName.VisibilityTimeout,
                              VISIBILITY_TIMEOUT.TotalSeconds.ToString());

            var queuePolicy = await GetQueuePolicy(queueUrl);

            if (queuePolicy != "")
            {
                queueSettings.Add(QueueAttributeName.Policy, queuePolicy);
            }

            var request = new SetQueueAttributesRequest
            {
                QueueUrl   = queueUrl,
                Attributes = queueSettings,
            };

            try
            {
                await sqsClient.SetQueueAttributesAsync(request);
            }
            catch (Exception e)
            {
                //Function.info.Log("Failed to set queue attributes: " + e);
            }
        }
Example #8
0
        public void MonitorEmail(string emailaddr)
        {
            if (Exists(emailaddr) && !monitors.ContainsKey(emailaddr))
            {
                FastEmailMailbox mbx     = Mailboxes[emailaddr];
                FastEmailMonitor monitor = new FastEmailMonitor();
                monitor.mbx           = mbx;
                monitor.sqsqueue_name = mbx.S3Bucket + "-" + Process.GetCurrentProcess().Id + "-" + System.Environment.MachineName;
                create_sqsclient();
                try
                {
                    // create the queue
                    var sqscreateresponse = sqsclient.CreateQueueAsync(new CreateQueueRequest
                    {
                        QueueName = monitor.sqsqueue_name
                    });

                    CreateQueueResponse sqsresult = sqscreateresponse.Result;
                    monitor.sqsqueue_url = sqsresult.QueueUrl;
                }
                catch (AmazonSQSException e)
                {
                    Console.WriteLine("Exception while creating SQS Queue for {0}: {1}", emailaddr, e.Message);
                }

                // get the queue arn
                try
                {
                    List <string> attr = new List <string>()
                    {
                        "QueueArn"
                    };
                    var sqsattrresponse = sqsclient.GetQueueAttributesAsync(new GetQueueAttributesRequest
                    {
                        QueueUrl       = monitor.sqsqueue_url,
                        AttributeNames = attr
                    });
                    GetQueueAttributesResponse sqsresponse = sqsattrresponse.Result;
                    monitor.sqsqueue_arn = sqsresponse.QueueARN;
                }
                catch (AmazonSQSException e)
                {
                    Console.WriteLine("Exception while getting QueueARN SQS Queue for {0}: {1}", emailaddr, e.Message);
                }

                // add permission
                string perm = @"{
                ""Version"":""2012-10-17"",
                ""Statement"":[
                {
                    ""Sid"":""Policy-"
                              + monitor.mbx.TopicArn;
                perm += @""", 
                ""Effect"":""Allow"", 
                ""Principal"":""*"",
                ""Action"":""sqs:SendMessage"",
                ""Resource"":"""
                        + monitor.sqsqueue_arn;
                perm += @""", 
                ""Condition"":{ 
                    ""ArnEquals"":{
                        ""aws:SourceArn"":"""
                        + monitor.mbx.TopicArn;
                perm += @""" 
                                }
                            }
                        }
                    ]
                }";
                var policy = new Dictionary <string, string>();
                policy.Add("Policy", perm);
                try
                {
                    var qsattrrequest = sqsclient.SetQueueAttributesAsync(new SetQueueAttributesRequest
                    {
                        QueueUrl   = monitor.sqsqueue_url,
                        Attributes = policy
                    });
                    var qsattrresponse = qsattrrequest.Result;
                }
                catch (AmazonSQSException e)
                {
                    Console.WriteLine("Exception while adding permission policy to queue for {0}: {1}", emailaddr, e.Message);
                }
                create_snsclient();
                try
                {
                    var snsresponse = snsclient.SubscribeAsync(new SubscribeRequest
                    {
                        Protocol = "sqs",
                        Endpoint = monitor.sqsqueue_arn,
                        TopicArn = monitor.mbx.TopicArn
                    });
                    var subresult = snsresponse.Result;
                    monitor.subscription_arn = subresult.SubscriptionArn;
                }
                catch (AmazonSimpleNotificationServiceException e)
                {
                    Console.WriteLine("Exception while subscribing to queue for {0}: {1}", emailaddr, e.Message);
                }

                monitors.Add(emailaddr, monitor);
            }
        }
Example #9
0
        private static async Task <Topic> SetupTopicAndSubscriptionsAsync(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 = GetSettingsAsync().Result;

            #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.CreateTopicAsync(new CreateTopicRequest {
                Name = "GlacierDownload-" + ticks
            }).Result.TopicArn;
            //Debug.WriteLine($"topicArn: {topicArn}");
            topic.TopicARN = topicArn;
            #endregion

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

            var getQueueAttributesRequest = new GetQueueAttributesRequest
            {
                AttributeNames = new List <string> {
                    "QueueArn"
                },
                QueueUrl = queueUrl
            };
            var response = await sqsClient.GetQueueAttributesAsync(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
            await snsClient.SubscribeAsync(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);

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

            return(topic);
        }