I'm trying to verify if a topic exists based on topic name.
Do you know if this is possible?
For example I want to verify if topic with name "test" already exist.
Below is what I'm trying but doesn't work because topicsList contains topicArns and not topicNames...
topics = sns.get_all_topics()
topicsList = topics['ListTopicsResponse']['ListTopicsResult'['Topics']
if "test" in topicsList:
print("true")
This code will work if you have more than 100 topics
def get_topic(token=None):
topics = self.sns.get_all_topics(token)
next_token = topics['ListTopicsResponse']['ListTopicsResult']['NextToken']
topic_list = topics['ListTopicsResponse']['ListTopicsResult']['Topics']
for topic in topic_list:
if "your_topic_name" in topic['TopicArn'].split(':')[5]:
return topic['TopicArn']
else:
if next_token:
get_topic(next_token)
else:
return None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With