根据主题名称验证主题是否存在

tec*_*man 5 python boto amazon-web-services

我正在尝试根据主题名称验证主题是否存在。

你知道这是否可能吗?

例如,我想验证名称为“test”的主题是否已经存在。

以下是我正在尝试但不起作用的内容,因为 topicList 包含 topicArns 而不是 topicNames...

topics = sns.get_all_topics()   
topicsList = topics['ListTopicsResponse']['ListTopicsResult'['Topics']

if "test" in topicsList:
    print("true")
Run Code Online (Sandbox Code Playgroud)

pan*_*ore 5

如果你尝试捕获An error occurred (NotFound) when calling the GetTopicAttributes operation: Topic does not exist异常怎么办?

from botocore.exceptions import ClientError

topic_arn = "arn:aws:sns:us-east-1:999999999:neverFound"

try:
    response = client.get_topic_attributes(
        TopicArn=topic_arn
    )
    print "Exists"
except ClientError as e:
    # Validate if is this:
    # An error occurred (NotFound) when calling the GetTopicAttributes operation: Topic does not exist
    print "Does not exists"
Run Code Online (Sandbox Code Playgroud)


gar*_*aat 3

这是一种黑客攻击,但它应该有效:

topics = sns.get_all_topics()   
topic_list = topics['ListTopicsResponse']['ListTopicsResult']['Topics']
topic_names = [t['TopicArn'].split(':')[5] for t in topic_list]

if 'test' in topic_names:
   print(True)
Run Code Online (Sandbox Code Playgroud)