Home > database >  How to unsubscribe email from AWS SNS Topic?
How to unsubscribe email from AWS SNS Topic?

Time:01-30

I have an endpoint that subscribes the specified email to my SNS topic:

        [HttpPost("subscriptions/{email}")]
        public async Task SubscribeEmail(string email)
        {
            try
            {
                var request = new SubscribeRequest()
                {
                    TopicArn = AwsServicesConstants.SenderTopicArn,
                    ReturnSubscriptionArn = true,
                    Protocol = "email",
                    Endpoint = email,
                };

                var response = await _snsClient.SubscribeAsync(request);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unexpected error: {ex}");
            }
        }

How can I unsubscribe given email from that topic with just a specified email like this

[HttpDelete("subscriptions/{email}")]
public async Task<UnsubscribeResponse> UnsubscribeEmail(string email)
{
    var request = new UnsubscribeRequest(email);

    var response = await _snsClient.UnsubscribeAsync(request);

    return response;
}

Actually, unsubscription is not working because UnsubscribeRequest requires only subscriptionArn, and not the email

CodePudding user response:

You will need to identify the subscription (once subscribed) by calling ListSubscriptionsByTopic, looking for the Endpoint that matches the desired email address. You could then extract the ARN and use it when calling Unsubscribe.

CodePudding user response:

You can write app logic to get the ARN value using the email address. Here is a C# example that shows you the logic for this use case using the AWS SDK for .NET.

     public async Task<string> UnSubEmail(string email)
        {
            var client = new AmazonSimpleNotificationServiceClient(RegionEndpoint.USEast2);
            var arnValue = await GetSubArn(client, email);
            await RemoveSub(client, arnValue);
            return $"{email} was successfully deleted!";
        }

  public static async Task<string> GetSubArn(IAmazonSimpleNotificationService client, string email)
        {
            var request = new ListSubscriptionsByTopicRequest();
            request.TopicArn = TopicArn;
            var subArn = string.Empty;

            var response = await client.ListSubscriptionsByTopicAsync(request);
            List<Subscription> allSubs = response.Subscriptions;

            // Get the ARN Value for this subscription.
            foreach (Subscription sub in allSubs)
            {
                if (sub.Endpoint.Equals(email))
                {
                    subArn = sub.SubscriptionArn;
                    return subArn;
                }
            }

            return string.Empty;
        }


 public static async Task<string> RemoveSub(IAmazonSimpleNotificationService client, string subArn)
        {
            var request = new UnsubscribeRequest();
            request.SubscriptionArn = subArn;
            await client.UnsubscribeAsync(request);

            return string.Empty;
        }

You can find full .NET Example in the AWS Code Lib:

Build a publish and subscription application that translates messages

  • Related