CreateTopic() public méthode

Creates a topic to which notifications can be published. Users can create at most 100,000 topics. For more information, see http://aws.amazon.com/sns. This action is idempotent, so if the requester already owns a topic with the specified name, that topic's ARN is returned without creating a new topic.
/// Indicates that the user has been denied access to the requested resource. /// /// Indicates an internal service error. /// /// Indicates that a request parameter does not comply with the associated constraints. /// /// Indicates that the customer already owns the maximum allowed number of topics. ///
public CreateTopic ( CreateTopicRequest request ) : CreateTopicResponse
request Amazon.SimpleNotificationService.Model.CreateTopicRequest Container for the necessary parameters to execute the CreateTopic service method.
Résultat Amazon.SimpleNotificationService.Model.CreateTopicResponse
        private void CreatePlatformApplication()
        {
            using (var sns = new Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceClient())
            {
                // Create target topic
                var topicArn = sns.CreateTopic(new CreateTopicRequest
                {
                    Name = "TestTopic" + new Random().Next()
                }).TopicArn;
                topicArns.Add(topicArn);

                var platformAppName = "NetSDKTestApp" + new Random().Next();

                // Create a platform application for GCM.
                platformApplicationArn = sns.CreatePlatformApplication(new CreatePlatformApplicationRequest
                {
                    Name       = platformAppName,
                    Platform   = "GCM",
                    Attributes = new Dictionary <string, string>
                    {
                        { "PlatformCredential", PlatformCredential },
                        { "PlatformPrincipal", "NA" },
                        { "EventEndpointCreated", topicArn },
                        { "EventEndpointDeleted", topicArn },
                        { "EventEndpointUpdated", topicArn },
                        { "EventDeliveryAttemptFailure", topicArn },
                        { "EventDeliveryFailure", topicArn },
                    }
                }).PlatformApplicationArn;
            }
        }
        public static string CreateTopic(string topicName)
        {
            using (var client = new AmazonSimpleNotificationServiceClient(Settings.AccessKey, Settings.Secret))
            {
                var request = new CreateTopicRequest(topicName);

                return client.CreateTopic(request).TopicArn;
            }
        }
        private string EnsureTopic(string topicName, AmazonSimpleNotificationServiceClient client)
        {
            var topic = client.FindTopic(topicName);
            if (topic != null)
                return topic.TopicArn;

            _logger.DebugFormat("Topic with name {0} does not exist. Creating new topic", topicName);
            var topicResult = client.CreateTopic(topicName);
            return topicResult.HttpStatusCode == HttpStatusCode.OK ? topicResult.TopicArn : string.Empty;
        }
        public void SetTopicConfigurationTests()
        {
            var s3Config = new AmazonS3Config();
            using (var s3Client = new AmazonS3Client(s3Config))
            using (var snsClient = new AmazonSimpleNotificationServiceClient())
            {
                var snsCreateResponse = snsClient.CreateTopic("events-test-" + DateTime.Now.Ticks);
                var bucketName = S3TestUtils.CreateBucket(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 = s3Client.GetBucketNotification(bucketName);

                    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);
                }
            }
        }
        public virtual string CreateTopic(AmazonSimpleNotificationServiceClient snsClient, string topicName)
        {
            string topicArn;
            // Create the request
            var createTopicRequest = new CreateTopicRequest
            {
                Name = topicName
            };
            // Submit the request
            CreateTopicResponse topicResponse = snsClient.CreateTopic(createTopicRequest);

            // Return the ARN
            topicArn = topicResponse.TopicArn;
            return topicArn;
        }
        /// <summary>
        /// Creates a user account for SNS
        /// </summary>
        /// <param name="userName">The user to create a SNS account for</param>
        /// <param name="number">The user's phone number</param>
        private void CreateSNSAccount(String userName, String number)
        {
            AmazonSimpleNotificationServiceClient client = new AmazonSimpleNotificationServiceClient();

            //Create topic first.
            CreateTopicRequest request = new CreateTopicRequest
            {
                Name = userName
            };

            try
            {
                CreateTopicResponse response = client.CreateTopic(request);
                CreateTopicResult result = response.CreateTopicResult;
                String[] strings = new String[1];
                strings[0] = "Success! Assigned ARN is: " + result.TopicArn + "\n";
                TempData["result"] = strings;
            }
            catch (Exception e)
            {
                TempData["error"] = e.Message;
            }

            String arn = "arn:aws:sns:us-east-1:727060774285:" + userName;

            SetTopicAttributesRequest request2 = new SetTopicAttributesRequest
            {
                AttributeName = "DisplayName",
                AttributeValue = "Cookbook",
                TopicArn = arn
            };

            try
            {
                SetTopicAttributesResponse response = client.SetTopicAttributes(request2);
                ResponseMetadata result = response.ResponseMetadata;
                Console.WriteLine(result.RequestId);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            //Add SMS number to topic.
            SubscribeRequest request3 = new SubscribeRequest
            {
                TopicArn = arn,
                Endpoint = number,
                Protocol = "sms"
            };

            try
            {
                SubscribeResponse response = client.Subscribe(request3);
                SubscribeResult result = response.SubscribeResult;

                string resultSubscribe = "Success! Subscription Arn is: " + result.SubscriptionArn + "\n";
                Console.WriteLine(resultSubscribe);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemple #7
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
    }
    /// <summary>
    /// Utility method for creating at topic and subscribe the email address to it.
    /// </summary>
    /// <param name="emailAddress"></param>
    /// <returns></returns>
    static string CreateTopic(string emailAddress)
    {
        var snsClient = new AmazonSimpleNotificationServiceClient();
            var topicArn = snsClient.CreateTopic(new CreateTopicRequest
            {
                Name = "TranscodeEvents" + UNIQUE_POSTFIX
            }).TopicArn;

            snsClient.Subscribe(new SubscribeRequest
            {
                TopicArn = topicArn,
                Protocol = "email",
                Endpoint = emailAddress
            });

            return topicArn;
    }