Skip to content

Instantly share code, notes, and snippets.

@awswithdotnet
Created March 8, 2022 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awswithdotnet/d152de7033eafdcb99d1832fc16d4cd2 to your computer and use it in GitHub Desktop.
Save awswithdotnet/d152de7033eafdcb99d1832fc16d4cd2 to your computer and use it in GitHub Desktop.
sns SNSMessaging SNSMessagePublisher Publish Complete
using System;
using System.Threading.Tasks;
using Abstractions;
using Amazon;
using Amazon.Runtime;
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;
namespace SNSMessaging
{
public class SNSMessagePublisher : IPublisher<String,Task>
{
private readonly IMessageFormatter<String, String> _formatter;
public SNSMessagePublisher(IMessageFormatter<String, String> formatter)
{
_formatter = formatter;
}
public async Task Publish(String message)
{
String topicArn = "<your-aws-topicarn>";
String awsRegion = "<your-aws-region>";
String accessKey = "<your-aws-accesskey>";
String secret = "<your-aws-secret>";
String formattedMessage = _formatter.Format(message);
BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secret);
RegionEndpoint region = RegionEndpoint.GetBySystemName(awsRegion);
var snsClient = new AmazonSimpleNotificationServiceClient(creds, region);
PublishRequest publishRequest = new PublishRequest(topicArn, formattedMessage);
PublishResponse response = await snsClient.PublishAsync(publishRequest);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment