我正在尝试根据主题名称验证主题是否存在。
你知道这是否可行吗?
例如,我想验证名称为“test”的主题是否已存在。
下面是我正在尝试但不起作用的内容,因为 topicList 包含 topicArns 而不是 topicNames...
topics = sns.get_all_topics()
topicsList = topics['ListTopicsResponse']['ListTopicsResult'['Topics']
if "test" in topicsList:
print("true")
最佳答案
如果您 try catch 调用 GetTopicAttributes 操作时发生错误(NotFound):主题不存在
异常,该怎么办?
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"
关于python - 根据主题名称验证主题是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30243687/