Skip to content

Instantly share code, notes, and snippets.

Created February 28, 2017 17:28
Show Gist options
  • Save anonymous/86330fbaee4fee6fa5e0bd03bf2c0fa9 to your computer and use it in GitHub Desktop.
Save anonymous/86330fbaee4fee6fa5e0bd03bf2c0fa9 to your computer and use it in GitHub Desktop.
SQS vs SNS
using Amazon.Auth.AccessControlPolicy;
using Amazon.Auth.AccessControlPolicy.ActionIdentifiers;
using Amazon.SimpleNotificationService;
using Amazon.SQS;
using Amazon.SQS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AwsTest
{
class Program
{
static void Main(string[] args)
{
var sqs = new AmazonSQSClient();
var sns = new AmazonSimpleNotificationServiceClient();
var queueUrl = sqs.CreateQueue("testQueue8").QueueUrl;
var queueArn = sqs.GetQueueAttributes(queueUrl, new List<string> { "QueueArn" }).QueueARN;
var topicArn = sns.CreateTopic("testTopic8").TopicArn;
var subscriptionArn = sns.Subscribe(topicArn, "sqs", queueArn).SubscriptionArn;
var policy = new Policy()
{
Statements = new List<Statement>() {
new Statement(Statement.StatementEffect.Allow) {
Principals = new List<Principal>() { Principal.AllUsers},
Actions = new List<ActionIdentifier>() { SQSActionIdentifiers.SendMessage },
Conditions = new List<Condition>
{
ConditionFactory.NewCondition(ConditionFactory.ArnComparisonType.ArnEquals, "aws:SourceArn", topicArn)
}
}
}
};
var request = new SetQueueAttributesRequest()
{
QueueUrl = queueUrl,
Attributes = new Dictionary<string, string>
{
{ QueueAttributeName.Policy, policy.ToJson()}
}
};
sqs.SetQueueAttributes(request);
sns.Publish(topicArn, "this is a test");
Console.WriteLine($"{sqs.ReceiveMessage(queueUrl).Messages.Count}");
Console.WriteLine("End");
Console.ReadKey(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment