Пример #1
0
        private static string CreateSNSTopic(AmazonSimpleNotificationServiceClient sns, string name, string displayName)
        {
            string topicArn = "";

            try
            {
                // Create topic
                Console.WriteLine("Creating topic...");
                topicArn = sns.CreateTopic(new CreateTopicRequest
                {
                    Name = name
                }).TopicArn;

                // Set display name to a friendly value
                Console.WriteLine();
                Console.WriteLine("Setting topic attributes...");
                sns.SetTopicAttributes(new SetTopicAttributesRequest
                {
                    TopicArn       = topicArn,
                    AttributeName  = "DisplayName",
                    AttributeValue = displayName
                });
            }
            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);
            }
            return(topicArn);
        }
Пример #2
0
        public string CreateTopic(string topicName, string topicDisplayName)
        {
            string topicArn = null;

            try
            {
                Console.WriteLine("Creating topic...");
                topicArn = sns.CreateTopic(new CreateTopicRequest
                {
                    Name = topicName
                }).TopicArn;
                Console.WriteLine($"Topic created with Arn: {topicArn}");


                // Set display name to a friendly value
                Console.WriteLine();
                Console.WriteLine("Setting topic attributes...");
                sns.SetTopicAttributes(new SetTopicAttributesRequest
                {
                    TopicArn       = topicArn,
                    AttributeName  = "DisplayName",
                    AttributeValue = topicDisplayName
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
            return(topicArn);
        }
        public static void Main(string[] args)
        {
            var sns_object = new AmazonSimpleNotificationServiceClient();

            string email_id = "*****@*****.**";

            try
            {
                // Create an SNS topic called "PACKT-PUB"
                Console.WriteLine("Creating topic...");
                var Topic_Arn_id = sns_object.CreateTopic(new CreateTopicRequest
                {
                    Name = "PACKT-PUB"
                }).TopicArn;

                // Set attributes for the topic
                sns_object.SetTopicAttributes(new SetTopicAttributesRequest
                {
                    TopicArn       = Topic_Arn_id,
                    AttributeName  = "DisplayName",
                    AttributeValue = "PACKT-PUB"
                });

                // Add an email subsciption
                sns_object.Subscribe(new SubscribeRequest
                {
                    TopicArn = Topic_Arn_id,
                    Protocol = "email",
                    Endpoint = email_id
                });

                // Publish a topic
                sns_object.Publish(new PublishRequest
                {
                    Subject  = "Test",
                    Message  = "Testing testing 1 2 3",
                    TopicArn = Topic_Arn_id
                });

                Console.WriteLine("Waiting 60 seconds before deleting the topic");
                System.Threading.Thread.Sleep(30000);
                //Delete the topic
                sns_object.DeleteTopic(new DeleteTopicRequest
                {
                    TopicArn = Topic_Arn_id
                });
            }
            catch (AmazonSimpleNotificationServiceException exception)
            {
                Console.WriteLine("Error !");
                Console.WriteLine(exception.ErrorCode);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            var    sns         = new AmazonSimpleNotificationServiceClient();
            string httpAddress = "http://34.217.101.161:3018/redisServer";

            try
            {
                // Create topic
                Console.WriteLine("Creating topic...");
                var topicArn = sns.CreateTopic(new CreateTopicRequest
                {
                    Name = "SampleSNSTopic"
                }).TopicArn;

                // Set display name to a friendly value
                Console.WriteLine();
                Console.WriteLine("Setting topic attributes...");
                sns.SetTopicAttributes(new SetTopicAttributesRequest
                {
                    TopicArn       = topicArn,
                    AttributeName  = "DisplayName",
                    AttributeValue = "Sample Notifications"
                });

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

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

                // Publish message
                Console.WriteLine();
                _ = DespatchAsync(@"[ '967813618981',
                                'BPNYDCFEE-6592',
                                '2/2/2018 20:59:00',
                                'Ops Hold',
                                '2018-02-02T20:59:38',
                                'Hold',
                                'Hold',
                                '',
                                '',
                                '' ]", topicArn);
                _ = DespatchAsync(@"[ '967813618981',
                                'BPNYDCFEE-6592',
                                '2/2/2018 20:59:00',
                                'Ops Hold',
                                '2018-02-02T20:59:38',
                                'Hold',
                                'Hold',
                                '',
                                '',
                                '' ]", topicArn);
                // Verify http endpoint receieved
                Console.WriteLine();
                Console.WriteLine("Please check your http endpoint and press enter when you receive the message...");
                Console.ReadLine();

                // Delete topic
                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();
        }
Пример #5
0
        public static void SNSCreateSubscribePublish()
        {
            #region SNSCreateSubscribePublish
            var snsClient = new AmazonSimpleNotificationServiceClient();

            var topicRequest = new CreateTopicRequest
            {
                Name = "CodingTestResults"
            };

            var topicResponse = snsClient.CreateTopic(topicRequest);

            var topicAttrRequest = new SetTopicAttributesRequest
            {
                TopicArn       = topicResponse.TopicArn,
                AttributeName  = "DisplayName",
                AttributeValue = "Coding Test Results"
            };

            snsClient.SetTopicAttributes(topicAttrRequest);

            snsClient.Subscribe(new SubscribeRequest
            {
                Endpoint = "*****@*****.**",
                Protocol = "email",
                TopicArn = topicResponse.TopicArn
            });

            // Wait for up to 2 minutes for the user to confirm the subscription.
            DateTime latest = DateTime.Now + TimeSpan.FromMinutes(2);

            while (DateTime.Now < latest)
            {
                var subsRequest = new ListSubscriptionsByTopicRequest
                {
                    TopicArn = topicResponse.TopicArn
                };

                var subs = snsClient.ListSubscriptionsByTopic(subsRequest).Subscriptions;

                var sub = subs[0];

                if (!string.Equals(sub.SubscriptionArn,
                                   "PendingConfirmation", StringComparison.Ordinal))
                {
                    break;
                }

                // Wait 15 seconds before trying again.
                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(15));
            }

            snsClient.Publish(new PublishRequest
            {
                Subject = "Coding Test Results for " +
                          DateTime.Today.ToShortDateString(),
                Message  = "All of today's coding tests passed.",
                TopicArn = topicResponse.TopicArn
            });
            #endregion
        }
Пример #6
0
        public static void Main(string[] args)
        {
            var    sns          = new AmazonSimpleNotificationServiceClient();
            string emailAddress = string.Empty;

            while (string.IsNullOrEmpty(emailAddress))
            {
                Console.Write("Please enter an email address to use: ");
                emailAddress = Console.ReadLine();
            }

            try
            {
                // Create topic
                Console.WriteLine("Creating topic...");
                var topicArn = sns.CreateTopic(new CreateTopicRequest
                {
                    Name = "SampleSNSTopic"
                }).TopicArn;

                // Set display name to a friendly value
                Console.WriteLine();
                Console.WriteLine("Setting topic attributes...");
                sns.SetTopicAttributes(new SetTopicAttributesRequest
                {
                    TopicArn       = topicArn,
                    AttributeName  = "DisplayName",
                    AttributeValue = "Sample Notifications"
                });

                // List all topics
                Console.WriteLine();
                Console.WriteLine("Retrieving all topics...");
                var listTopicsRequest = new ListTopicsRequest();
                ListTopicsResponse listTopicsResponse;
                do
                {
                    listTopicsResponse = sns.ListTopics(listTopicsRequest);
                    foreach (var topic in listTopicsResponse.Topics)
                    {
                        Console.WriteLine(" Topic: {0}", topic.TopicArn);

                        // Get topic attributes
                        var topicAttributes = sns.GetTopicAttributes(new GetTopicAttributesRequest
                        {
                            TopicArn = topic.TopicArn
                        }).Attributes;
                        if (topicAttributes.Count > 0)
                        {
                            Console.WriteLine(" Topic attributes");
                            foreach (var topicAttribute in topicAttributes)
                            {
                                Console.WriteLine(" -{0} : {1}", topicAttribute.Key, topicAttribute.Value);
                            }
                        }
                        Console.WriteLine();
                    }
                    listTopicsRequest.NextToken = listTopicsResponse.NextToken;
                } while (listTopicsResponse.NextToken != null);

                // 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();

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

                // Verify email receieved
                Console.WriteLine();
                Console.WriteLine("Please check your email and press enter when you receive the message...");
                Console.ReadLine();

                // Delete topic
                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();
        }
Пример #7
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();
        }