AWS SNS get topic by name

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I think that it's a very inefficient way to get a topic ARN, and I'd like to know if there is a most efficient way to get this information, like a function getTopicByName or something else.

If not, I'd like to know if ARN is immutable, and if can I store the topic ARN on my database?

Thanks.

回答1:

I don't know of any way to look up an SNS topic by name.

The ARN is immutable. It will not change for the life of the SNS topic. In particular, according to the documentation an ARN for an SNS topic is in the following format:

arn:aws:sns:region:account-id:topicname

So the only way for the SNS topic's ARN to change would be to change the name (in which case your lookup by name would also break), or delete the topic and recreate it in a new region or in an entirely different account, in which case it would not really the same topic anymore.



回答2:

If you know the topic already exists or you don't care if it gets created, then a much more direct approach is simply to call CreateTopic. Given a name, CreateTopic will return the existing topic, including the ARN.



回答3:

If the topic is already available,then you can use the "createTopic" method as follows.With this if the requester already owns a topic with the specified name, that topic's ARN is returned without creating a new topic. In AWS Java SDK, the code will looks like this.

    AWSCredentialsProvider provider = new ProfileCredentialsProvider();     AmazonSNS sns = AmazonSNSClientBuilder.standard().withCredentials(provider).build();     CreateTopicResult createRes = sns.createTopic("HelloTopic"); 

Then by using CreateTopicResult you can obtain the Topic ARN and publish the message

sns.publish(new PublishRequest(createRes.getTopicArn(), "Hello World")); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!