예제 #1
0
        protected override void Dispose(bool disposing)
        {
            if (_bucketNames.Count != 0)
            {
                using (AmazonS3Client client = new AmazonS3Client(s3ClientConfig))
                {
                    foreach (string bucketName in _bucketNames)
                    {
                        UtilityMethods.DeleteBucketWithObjectsAsync(client, bucketName).Wait();
                    }
                }
            }

            foreach (string url in _queueUrls)
            {
                try
                {
                    Client.DeleteQueueAsync(new DeleteQueueRequest {
                        QueueUrl = url
                    }).Wait();
                }
                catch { };
            }

            base.Dispose(disposing);
        }
예제 #2
0
        public async Task TestRestXml()
        {
            using (var client = UtilityMethods.CreateClient <AmazonS3Client>())
            {
                var bucketName = await UtilityMethods.CreateBucketAsync(client, "TestRestXml_Async");

                try
                {
                    var buckets = (await client.ListBucketsAsync(new ListBucketsRequest())).Buckets;
                    Assert.NotNull(buckets);
                    Assert.NotEqual(0, buckets.Count);
                    Assert.Equal(1, buckets
                                 .Count(b =>
                                        string.Equals(bucketName, b.BucketName, StringComparison.OrdinalIgnoreCase)));

                    var fakeBucketName = "really-fake-bucket-that-shout-not-exist" + DateTime.Now.ToFileTime();
                    var as3e           = await AssertExtensions.ExpectExceptionAsync <AmazonS3Exception>(client.DeleteBucketAsync(new DeleteBucketRequest
                    {
                        BucketName = fakeBucketName
                    }));

                    Assert.NotNull(as3e);
                    Assert.NotNull(as3e.Message);
                    //Assert.NotNull(aete.ErrorCode);
                    Assert.Equal(as3e.ErrorType, ErrorType.Sender);
                }
                finally
                {
                    await UtilityMethods.DeleteBucketWithObjectsAsync(client, bucketName);
                }
            }
        }
예제 #3
0
        public async Task SetTopicConfigurationTests()
        {
            using (var snsClient = new AmazonSimpleNotificationServiceClient())
            {
                string topicName         = UtilityMethods.GenerateName("events-test");
                var    snsCreateResponse = await snsClient.CreateTopicAsync(topicName);

                var bucketName = await UtilityMethods.CreateBucketAsync(Client, "SetTopicConfigurationTests");

                try
                {
                    await snsClient.AuthorizeS3ToPublishAsync(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
                                }
                            }
                        }
                    };
                    await Client.PutBucketNotificationAsync(putRequest);

                    var getResponse = WaitUtils.WaitForComplete(
                        () =>
                    {
                        return(Client.GetBucketNotificationAsync(bucketName).Result);
                    },
                        (r) =>
                    {
                        return(r.TopicConfigurations.Count > 0);
                    });

                    Assert.Equal(1, getResponse.TopicConfigurations.Count);
                    Assert.Equal(1, getResponse.TopicConfigurations[0].Events.Count);
                    Assert.Equal(EventType.ObjectCreatedPut, getResponse.TopicConfigurations[0].Events[0]);

#pragma warning disable 618
                    Assert.Equal("s3:ObjectCreated:Put", getResponse.TopicConfigurations[0].Event);
#pragma warning restore 618
                    Assert.Equal("the-topic-test", getResponse.TopicConfigurations[0].Id);
                    Assert.Equal(snsCreateResponse.TopicArn, getResponse.TopicConfigurations[0].Topic);
                }
                finally
                {
                    await snsClient.DeleteTopicAsync(snsCreateResponse.TopicArn);

                    await UtilityMethods.DeleteBucketWithObjectsAsync(Client, bucketName);
                }
            }
        }
예제 #4
0
        public async Task SetQueueConfigurationTests()
        {
            var filterRule = new FilterRule("Prefix", "test/");

            using (var sqsClient = new AmazonSQSClient())
            {
                string topicName      = UtilityMethods.GenerateName("events-test");
                var    createResponse = await sqsClient.CreateQueueAsync(topicName);

                var bucketName = await UtilityMethods.CreateBucketAsync(Client, "SetQueueConfigurationTests");

                try
                {
                    var queueArn = await sqsClient.AuthorizeS3ToSendMessageAsync(createResponse.QueueUrl, bucketName);

                    PutBucketNotificationRequest putRequest = new PutBucketNotificationRequest
                    {
                        BucketName          = bucketName,
                        QueueConfigurations = new List <QueueConfiguration>
                        {
                            new QueueConfiguration
                            {
                                Id     = "the-queue-test",
                                Queue  = queueArn,
                                Events = { EventType.ObjectCreatedPut },
                                Filter = new Filter
                                {
                                    S3KeyFilter = new S3KeyFilter
                                    {
                                        FilterRules = new List <FilterRule>
                                        {
                                            filterRule
                                        }
                                    }
                                }
                            }
                        }
                    };

                    await Client.PutBucketNotificationAsync(putRequest);

                    var getResponse = await Client.GetBucketNotificationAsync(bucketName);

                    Assert.Equal(1, getResponse.QueueConfigurations.Count);
                    Assert.Equal(1, getResponse.QueueConfigurations[0].Events.Count);
                    Assert.Equal(EventType.ObjectCreatedPut, getResponse.QueueConfigurations[0].Events[0]);

                    Assert.NotNull(getResponse.QueueConfigurations[0].Filter);
                    Assert.NotNull(getResponse.QueueConfigurations[0].Filter.S3KeyFilter);
                    Assert.NotNull(getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules);
                    Assert.Equal(1, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules.Count);
                    Assert.Equal(filterRule.Name, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules[0].Name);
                    Assert.Equal(filterRule.Value, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules[0].Value);

                    Assert.Equal("the-queue-test", getResponse.QueueConfigurations[0].Id);
                    Assert.Equal(queueArn, getResponse.QueueConfigurations[0].Queue);

                    // Purge queue to remove test message sent configuration was setup.
                    await sqsClient.PurgeQueueAsync(createResponse.QueueUrl);

                    Thread.Sleep(TimeSpan.FromSeconds(1));

                    var putObjectRequest = new PutObjectRequest
                    {
                        BucketName  = bucketName,
                        Key         = "test/data.txt",
                        ContentBody = "Important Data"
                    };

                    await Client.PutObjectAsync(putObjectRequest);

                    string messageBody = null;
                    for (int i = 0; i < 5 && messageBody == null; i++)
                    {
                        var receiveResponse = await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = createResponse.QueueUrl, WaitTimeSeconds = 20 });

                        if (receiveResponse.Messages.Count != 0)
                        {
                            messageBody = receiveResponse.Messages[0].Body;
                        }
                    }


                    var evnt = S3EventNotification.ParseJson(messageBody);

                    Assert.Equal(1, evnt.Records.Count);
                    Assert.Equal(putObjectRequest.BucketName, evnt.Records[0].S3.Bucket.Name);
                    Assert.Equal(putObjectRequest.Key, evnt.Records[0].S3.Object.Key);
                    Assert.Equal(putObjectRequest.ContentBody.Length, evnt.Records[0].S3.Object.Size);
                }
                finally
                {
                    await sqsClient.DeleteQueueAsync(createResponse.QueueUrl);

                    await UtilityMethods.DeleteBucketWithObjectsAsync(Client, bucketName);
                }
            }
        }