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); } } }
public void SimpleUpload() { var client = Client; using (var tu = new Amazon.S3.Transfer.TransferUtility(client)) { tu.Upload(testFilePath, testBucketName); var response = WaitUtils.WaitForComplete( () => { return(client.GetObjectMetadataAsync(new GetObjectMetadataRequest { BucketName = testBucketName, Key = TEST_FILENAME }).Result); }); Assert.True(response.ETag.Length > 0); var downloadPath = testFilePath + ".download"; var downloadRequest = new Amazon.S3.Transfer.TransferUtilityDownloadRequest { BucketName = testBucketName, Key = TEST_FILENAME, FilePath = downloadPath }; tu.Download(downloadRequest); TestDownloadedFile(downloadPath); // empty out file, except for 1 byte File.WriteAllText(downloadPath, TEST_CONTENT.Substring(0, 1)); Assert.True(File.Exists(downloadPath)); tu.Download(downloadRequest); TestDownloadedFile(downloadPath); } }
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 = WaitUtils.WaitForComplete( () => { return(Client.GetBucketNotificationAsync(bucketName).Result); }, (r) => { return(r.QueueConfigurations.Count > 0); }); 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)); // make sure the queue is really clear WaitUtils.WaitForComplete( () => { return(sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest() { QueueUrl = createResponse.QueueUrl }).Result); }, (r) => { return(r.Messages.Count == 0); }); var putObjectRequest = new PutObjectRequest { BucketName = bucketName, Key = "test/data.txt", ContentBody = "Important Data" }; await Client.PutObjectAsync(putObjectRequest); var response = WaitUtils.WaitForComplete( () => { return(sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = createResponse.QueueUrl, WaitTimeSeconds = 20 }).Result); }, (r) => { return(r.Messages.Count > 0); }); var evnt = S3EventNotification.ParseJson(response.Messages[0].Body); 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); } } }
public async void TestQueueSubscription() { // create new topic var topicName = UtilityMethods.GenerateName("TestQueueSubscription"); var createTopicRequest = new CreateTopicRequest { Name = topicName }; var createTopicResult = await Client.CreateTopicAsync(createTopicRequest); var topicArn = createTopicResult.TopicArn; _topicArns.Add(topicArn); var queueName = UtilityMethods.GenerateName("TestQueueSubscription"); var queueUrl = (await sqsClient.CreateQueueAsync(new CreateQueueRequest { QueueName = queueName })).QueueUrl; _queueUrl.Add(queueUrl); ICoreAmazonSQS coreSqs = sqsClient as ICoreAmazonSQS; var subscriptionARN = await Client.SubscribeQueueAsync(topicArn, coreSqs, queueUrl); // Sleep to wait for the subscribe to complete. Thread.Sleep(TimeSpan.FromSeconds(5)); var publishRequest = new PublishRequest { TopicArn = topicArn, Subject = "Test Subject", Message = "Test Message", MessageAttributes = new Dictionary <string, SNSMessageAttributeValue> { { "Color", new SNSMessageAttributeValue { StringValue = "Red", DataType = "String" } }, { "Binary", new SNSMessageAttributeValue { DataType = "Binary", BinaryValue = new MemoryStream(Encoding.UTF8.GetBytes("Yes please")) } }, { "Prime", new SNSMessageAttributeValue { StringValue = "31", DataType = "Number" } }, } }; await Client.PublishAsync(publishRequest); var messages = (await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = queueUrl, WaitTimeSeconds = 20 })).Messages; Assert.Equal(1, messages.Count); var message = messages[0]; string bodyJson; // Handle some accounts returning message body as base 64 encoded. if (message.Body.Trim()[0] == '{') { bodyJson = message.Body; } else { bodyJson = Encoding.UTF8.GetString(Convert.FromBase64String(message.Body)); } var json = ThirdParty.Json.LitJson.JsonMapper.ToObject(bodyJson); var messageText = json["Message"]; var messageSubject = json["Subject"]; Assert.Equal(publishRequest.Message, messageText.ToString()); Assert.Equal(publishRequest.Subject, messageSubject.ToString()); var messageAttributes = json["MessageAttributes"]; Assert.Equal(publishRequest.MessageAttributes.Count, messageAttributes.Count); foreach (var ma in publishRequest.MessageAttributes) { var name = ma.Key; var value = ma.Value; Assert.True(messageAttributes.PropertyNames.Contains(name, StringComparer.Ordinal)); var jsonAttribute = messageAttributes[name]; var jsonType = jsonAttribute["Type"].ToString(); var jsonValue = jsonAttribute["Value"].ToString(); Assert.NotNull(jsonType); Assert.NotNull(jsonValue); Assert.Equal(value.DataType, jsonType); Assert.Equal(value.DataType != "Binary" ? value.StringValue : Convert.ToBase64String(value.BinaryValue.ToArray()), jsonValue); } await sqsClient.DeleteMessageAsync(new DeleteMessageRequest { QueueUrl = queueUrl, ReceiptHandle = messages[0].ReceiptHandle }); // This will unsubscribe but leave the policy in place. await Client.UnsubscribeAsync(new UnsubscribeRequest { SubscriptionArn = subscriptionARN }); // Subscribe again to see if this affects the policy. await Client.SubscribeQueueAsync(topicArn, coreSqs, queueUrl); await Client.PublishAsync(new PublishRequest { TopicArn = topicArn, Message = "Test Message again" }); messages = (await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = queueUrl, WaitTimeSeconds = 20 })).Messages; Assert.Equal(1, messages.Count); var response = WaitUtils.WaitForComplete( () => { return(sqsClient.GetQueueAttributesAsync(new GetQueueAttributesRequest { AttributeNames = new List <string> { "All" }, QueueUrl = queueUrl }).Result); }, (r) => { return(!string.IsNullOrEmpty(r.Policy)); }); var policy = Policy.FromJson(response.Policy); Assert.Equal(1, policy.Statements.Count); }
public async void TestQueueSubscription() { var topicArn = await CreateTopic(); var queueUrl = await CreateQueue(); var subscriptionArn = await SubscribeQueue(topicArn, queueUrl); var publishRequest = GetPublishRequest(topicArn); List <Message> messages = await PublishToSNSAndReceiveMessages(publishRequest, topicArn, queueUrl); Assert.Equal(1, messages.Count); var message = messages[0]; string bodyJson = GetBodyJson(message); var json = ThirdParty.Json.LitJson.JsonMapper.ToObject(bodyJson); var messageText = json["Message"]; var messageSubject = json["Subject"]; Assert.Equal(publishRequest.Message, messageText.ToString()); Assert.Equal(publishRequest.Subject, messageSubject.ToString()); var messageAttributes = json["MessageAttributes"]; Assert.Equal(publishRequest.MessageAttributes.Count, messageAttributes.Count); foreach (var ma in publishRequest.MessageAttributes) { var name = ma.Key; var value = ma.Value; Assert.True(messageAttributes.PropertyNames.Contains(name, StringComparer.Ordinal)); var jsonAttribute = messageAttributes[name]; var jsonType = jsonAttribute["Type"].ToString(); var jsonValue = jsonAttribute["Value"].ToString(); Assert.NotNull(jsonType); Assert.NotNull(jsonValue); Assert.Equal(value.DataType, jsonType); Assert.Equal(value.DataType != "Binary" ? value.StringValue : Convert.ToBase64String(value.BinaryValue.ToArray()), jsonValue); } await sqsClient.DeleteMessageAsync(new DeleteMessageRequest { QueueUrl = queueUrl, ReceiptHandle = messages[0].ReceiptHandle }); // This will unsubscribe but leave the policy in place. await Client.UnsubscribeAsync(new UnsubscribeRequest { SubscriptionArn = subscriptionArn }); // Subscribe again to see if this affects the policy. await Client.SubscribeQueueAsync(topicArn, sqsClient as ICoreAmazonSQS, queueUrl); await Client.PublishAsync(new PublishRequest { TopicArn = topicArn, Message = "Test Message again" }); messages = (await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = queueUrl, WaitTimeSeconds = 20 })).Messages; Assert.Equal(1, messages.Count); var response = WaitUtils.WaitForComplete( () => { return(sqsClient.GetQueueAttributesAsync(new GetQueueAttributesRequest { AttributeNames = new List <string> { "All" }, QueueUrl = queueUrl }).Result); }, (r) => { return(!string.IsNullOrEmpty(r.Policy)); }); var policy = Policy.FromJson(response.Policy); Assert.Equal(1, policy.Statements.Count); }