使用 boto3 列出 SNS 主题

List the SNS topic using boto3

我正在尝试使用 boto3 列出 sns 主题

我正在使用此代码

import boto3
import pprint

response = client.list_topics(
    NextToken='string'
)


list_topics=[]
for each_reg in response['topic']:
    print(each_reg['topic])

但我遇到了这个错误

 File "kri.py", line 14
    print(each_reg['topic])
                          ^
SyntaxError: EOL while scanning string literal

应该是

  print(each_reg['topic'])

您缺少一个单引号。

应该是:

list_topics=[]
for each_reg in response['topic']:
    print(each_reg['topic'])

你还需要导入 sns

client = boto3.client('sns', region_name='us-east-1') # add your region_name here

已更新:

import boto3
client = boto3.client('sns', region_name='us-east-1')
response = client.list_topics()

for each_reg in response['Topics']:
    print(each_reg['TopicArn'])