Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceClient.AuthorizeS3ToPublish C# (CSharp) Метод

AuthorizeS3ToPublish() публичный Метод

This is a utility method which updates the policy of a topic to allow the S3 bucket to publish events to it.
public AuthorizeS3ToPublish ( string topicArn, string bucket ) : void
topicArn string The topic that will have its policy updated.
bucket string The bucket that will be given access to publish from.
Результат void
        public void AuthorizeS3ToPublish(string topicArn, string bucket)
        {
            var attributes = this.GetTopicAttributes(new GetTopicAttributesRequest
                {
                    TopicArn = topicArn
                }).Attributes;

            Policy policy;
            Statement newStatement;
            GetNewPolicyAndStatementForTopicAttributes(attributes, topicArn, bucket, out policy, out newStatement);

            if (!policy.CheckIfStatementExists(newStatement))
            {
                policy.Statements.Add(newStatement);

                var policyString = policy.ToJson();
                this.SetTopicAttributes(new SetTopicAttributesRequest
                {
                    TopicArn = topicArn,
                    AttributeName = "Policy",
                    AttributeValue = policyString
                });
            }
        }
    }

Usage Example

Пример #1
0
        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);
                }
            }
        }
AmazonSimpleNotificationServiceClient