简体   繁体   English

AWS SNS - 如何通过主题名称获取主题 arn

[英]AWS SNS - How to get topic arn by topic name

In my application i need to send messages to the given topic name.在我的应用程序中,我需要将消息发送到给定的主题名称。 The topic is already created by other person and in a config file they give only the topic Name.该主题已经由其他人创建,并且在配置文件中他们只提供了主题名称。 My work is to push the messages in the given topic Name.我的工作是推送给定主题名称中的消息。 Is there any way to get topic ARN by topic NAME in java? java有没有办法通过topic NAME获取topic ARN?

As stated in this answer using createTopic(topicName) is more direct approach.正如这个答案中所述,使用 createTopic(topicName) 是更直接的方法。 In case topic have been created before, it will just return you topic ARN.如果主题之前已创建,它只会返回主题 ARN。

I've done this one of two ways.我已经做到了这两种方式之一。 The ARN is always the same pattern. ARN 始终是相同的模式。 So, you could just subscribe to "arn:aws:sns:<region>:<account>:<name>" where:因此,您可以订阅“arn:aws:sns:<region>:<account>:<name>”,其中:

region is from Regions.getCurrentRegion().区域来自 Regions.getCurrentRegion()。 Be careful with this as it is a bit expensive of a call and you'll need to handle not being on an EC2/Elastic Beanstalk instance.请注意这一点,因为它的调用成本有点​​高,而且您需要处理不在 EC2/Elastic Beanstalk 实例上的情况。

account is from AmazonIdentityManagementClient.getUser().getUser().getArn().帐户来自 AmazonIdentityManagementClient.getUser().getUser().getArn()。 You'll have to parse the account number from that.您必须从中解析帐号。 Same warning about not being in an EC2 environment.关于不在 EC2 环境中的相同警告。

name is what you have. name 就是你所拥有的。

A simpler way is loop through the the topics and look for the name you want in the ARN.更简单的方法是遍历主题并在 ARN 中查找您想要的名称。 You will use the AmazonSNSClient listTopics method to do this.您将使用 AmazonSNSClient listTopics 方法来执行此操作。 Remember that that method only returns the first 100 topics - you'll want to properly loop over the entire topic list.请记住,该方法仅返回前 100 个主题 - 您需要正确循环整个主题列表。

This might be what you need.这可能是您所需要的。 Supply the topic and get it from available topics.提供主题并从可用主题中获取它。

import json
import boto3

def lambda_handler(event, context):
    try:

        topic =  event['topic'] 
        subject = event['subject']
        body = event['body']
        subscriber = event['subscriber']
        
        sns_client = boto3.client('sns')
        
        sns_topic_arn = [tp['TopicArn'] for tp in sns_client.list_topics()['Topics'] if topic in tp['TopicArn']]
        
        sns_client.publish(TopicArn = sns_topic_arn[0], Message=body,
            Subject=subject)
        
    except Exception as e:
        print(e)

If you have create topic permission, you can try this: link如果你有创建主题权限,你可以试试这个:链接

If you don't have permission, you can try this:如果你没有权限,你可以试试这个:

private static Optional<Topic> getTopicByName(SnsClient snsClient, 
                                              String topicName) {
    for (val topicsResponse : snsClient.listTopicsPaginator()) {
        if (topicsResponse.hasTopics()) {
            for (val topic : topicsResponse.topics()) {
                // Arn format => arn:aws:sns:region:account-id:topic-name
                val arn = topic.topicArn();
                val sp = arn.split(":");
                if (topicName.equals(sp[sp.length - 1])) {
                    return Optional.of(topic);
                }
            }
        }
    }
    return Optional.empty();
}

What you can do, is to create a table that contains topic and its topicArn.您可以做的是创建一个包含主题及其 topicArn 的表。

The topic arn can be retrieve by the console or using the api when you create a topic.创建主题时,可以通过控制台或使用 api 检索主题 arn。

This way no need to loop or try to match a pattern.这样就不需要循环或尝试匹配模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM