可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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"));