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

标签 python amazon-web-services boto

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

你知道这是否可行吗?

例如,我想验证名称为“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/

相关文章:

python - 全新 python 3.3.5 安装时出现 _socket 模块导入错误

database - 如何在aws中创建mysql函数?

django - 使用 django-storages 和 s3boto 后端,如何将缓存信息添加到图像的请求 header 以便浏览器缓存图像?

google-cloud-storage - gsutil 是否支持使用服务帐号信息创建 boto 文件?

python - 如何在不排序的情况下迭代组?

python - 为什么 Django 看不到我的测试?

java - 无法使用 AWS EMR 实例通过 spark 访问 JDBC 驱动程序

java - Amazon Simple Workflow - 给定工作流程ID 列出所有执行

django 应用程序使用rest api - 在哪里放置代码

python - 如何在spark textFile函数中使用自定义换行符?