Boto - 如何提取AWS SNS主题的ARN号
当你在创建一个 AWS SNS 主题的时候:
a = conn.create_topic(topicname)
或者在获取已经创建的主题的时候:
a = conn.get_all_topics()
结果是:
{u'CreateTopicResponse': {u'ResponseMetadata': {u'RequestId': u'42b46710-degf-52e6-7d86-2ahc8e1c738c'}, u'CreateTopicResult': {u'TopicArn': u'arn:aws:sns:eu-west-1:467741034465:exampletopic'}}}
问题是,如何把主题的 ARN(亚马逊资源名称)获取成字符串,比如:arn:aws:sns:eu-west-1:467741034465:exampletopic
?
2 个回答
7
import boto
def get_account_id():
# suggested by https://groups.google.com/forum/#!topic/boto-users/QhASXlNBm40
return boto.connect_iam().get_user().arn.split(':')[4]
def topic_arn_from_name(self, region, name):
return ":".join(["arn", "aws", "sns", region, get_account_id(), name])
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。
4
当你创建一个新主题时,boto会返回一个包含你上面描述的数据的Python字典。要获取主题的ARN(亚马逊资源名称),你只需要在字典中引用那个键,像这样:
a = conn.create_topic(topicname)
a_arn = a['CreateTopicResponse']['CreateTopicResult']['TopicArn']
虽然这个方法有点笨拙,但确实能用。
而list_topics
这个调用返回的是另一种结构,基本上是这样的:
{u'ListTopicsResponse':
{u'ListTopicsResult':
{u'NextToken': None,
u'Topics': [
{u'TopicArn': u'arn:aws:sns:us-east-1:467741034465:exampletopic'},
{u'TopicArn': u'arn:aws:sns:us-east-1:467741034465:footopic'}
]
},
u'ResponseMetadata': {u'RequestId': u'aef821f6-d595-55e1-af14-6d3a8064536a'}}}
在这种情况下,如果你想获取第一个主题的ARN,你可以这样做:
a = conn.list_topics()
a_arn = a['ListTopicsResponse']['ListTopicsResult']['Topics'][0]['TopicArn']