Exemplo n.º 1
0
        private static void RetrieveAllTopics(AmazonSimpleNotificationServiceClient sns)
        {
            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);
        }
Exemplo n.º 2
0
        private static void ListSNSTopics(AmazonSimpleNotificationServiceClient sns)
        {
            // List all topics
            try
            {
                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);
            }
            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.ReadLine();
        }
Exemplo n.º 3
0
        public static void SNSListTopics()
        {
            #region SNSListTopics
            var snsClient = new AmazonSimpleNotificationServiceClient();
            var request   = new ListTopicsRequest();
            var response  = new ListTopicsResponse();

            do
            {
                response = snsClient.ListTopics(request);

                foreach (var topic in response.Topics)
                {
                    Console.WriteLine("Topic: {0}", topic.TopicArn);

                    var attrs = snsClient.GetTopicAttributes(
                        new GetTopicAttributesRequest
                    {
                        TopicArn = topic.TopicArn
                    }).Attributes;

                    if (attrs.Count > 0)
                    {
                        foreach (var attr in attrs)
                        {
                            Console.WriteLine(" -{0} : {1}", attr.Key, attr.Value);
                        }
                    }

                    Console.WriteLine();
                }

                request.NextToken = response.NextToken;
            } while (!string.IsNullOrEmpty(response.NextToken));
            #endregion

            Console.ReadLine();
        }
        public void SetTopicConfigurationTests()
        {
            var s3Config = new AmazonS3Config();

            using (var s3Client = new AmazonS3Client(s3Config))
                using (var snsClient = new AmazonSimpleNotificationServiceClient())
                    using (var stsClient = new AmazonSecurityTokenServiceClient())
                    {
                        var snsCreateResponse = snsClient.CreateTopic("events-test-" + DateTime.Now.Ticks);
                        var bucketName        = S3TestUtils.CreateBucketWithWait(s3Client);

                        try
                        {
                            snsClient.AuthorizeS3ToPublish(snsCreateResponse.TopicArn, bucketName);

                            PutBucketNotificationRequest putRequest = new PutBucketNotificationRequest
                            {
                                BucketName          = bucketName,
                                TopicConfigurations = new List <TopicConfiguration>
                                {
                                    new TopicConfiguration
                                    {
                                        Id     = "the-topic-test",
                                        Topic  = snsCreateResponse.TopicArn,
                                        Events = new List <EventType> {
                                            EventType.ObjectCreatedPut
                                        }
                                    }
                                }
                            };

                            s3Client.PutBucketNotification(putRequest);

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

                            var getAttributeResponse = snsClient.GetTopicAttributes(new GetTopicAttributesRequest
                            {
                                TopicArn = snsCreateResponse.TopicArn
                            });

                            var policy = Policy.FromJson(getAttributeResponse.Attributes["Policy"]);

                            // SNS topics already have a default statement. We need to evaluate the second statement that the SDK appended.
                            var conditions = policy.Statements[1].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.TopicConfigurations.Count);
                            Assert.AreEqual(1, getResponse.TopicConfigurations[0].Events.Count);
                            Assert.AreEqual(EventType.ObjectCreatedPut, getResponse.TopicConfigurations[0].Events[0]);

#pragma warning disable 618
                            Assert.AreEqual("s3:ObjectCreated:Put", getResponse.TopicConfigurations[0].Event);
#pragma warning restore 618
                            Assert.AreEqual("the-topic-test", getResponse.TopicConfigurations[0].Id);
                            Assert.AreEqual(snsCreateResponse.TopicArn, getResponse.TopicConfigurations[0].Topic);
                        }
                        finally
                        {
                            snsClient.DeleteTopic(snsCreateResponse.TopicArn);
                            AmazonS3Util.DeleteS3BucketWithObjects(s3Client, bucketName);
                        }
                    }
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
0
        public static void Main(string[] args)
        {
            var    sns          = new AmazonSimpleNotificationServiceClient();
            string emailAddress = string.Empty;
            var    topicArn     = "arn:aws:sns:us-east-1:546163609422:OrdersTopic";

            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
                Orders        ord = new Orders();
                ShipToAddress st  = new ShipToAddress();
                st.Addr1          = "testAddr1";
                st.Addr2          = "testAddr2";
                st.City           = "MyCity";
                st.Zip            = "00000";
                ord.OrderId       = "12345";
                ord.OrderLocation = "Aurora";
                ord.OrderQty      = "100";
                ord.OrderDate     = "10222019";
                ord.ShiptoAdr     = st;
                string strJason = JsonConvert.SerializeObject(ord);
                Console.WriteLine();
                Console.WriteLine("Publishing message to topic...");
                sns.Publish(new PublishRequest
                {
                    Subject          = "Test",
                    Message          = strJason,
                    MessageStructure = strJason,
                    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();
        }