简体   繁体   English

boto3 SNS:订阅主题时添加过滤策略

[英]boto3 SNS: Add a filter policy while subscribing to a topic

I built a Python API that subscribes people to an SNS topic to receive emails about their Spotify accounts.我构建了一个 Python API 订阅人们的 SNS 主题以接收关于他们的 Spotify 帐户的电子邮件。 I have a single topic that I publish all the emails to.我有一个主题,我将所有电子邮件发布到该主题。

I am aware that I can set a subscription filter policy when I create a subscription from console.我知道我可以在从控制台创建订阅时设置subscription filter policy However, boto3 needs the subscription ARN to set a filter, and the ARN needs the email to be confirmed.但是,boto3 需要订阅 ARN 来设置过滤器,而 ARN 需要 email 来确认。

How to set the filter when creating the subscription from boto3, like in the console?从 boto3 创建订阅时如何设置过滤器,就像在控制台中一样?

EDIT编辑

>>> import boto3
>>> sns = boto3.client('sns')
>>> sns.subscribe(TopicArn='{the_arn}', Protocol='EMAIL', Endpoint='{an_email}')
{'SubscriptionArn': 'pending confirmation', ...

According to boto3 sns documentation , in order to set the FilterPolicy attribute, I need the subscription ARN.根据 boto3 sns 文档,为了设置FilterPolicy属性,我需要订阅 ARN。

The example in the docs:文档中的示例:

import boto3

sns = boto3.resource('sns')
subscription = sns.Subscription('arn')

response = subscription.set_attributes(
    AttributeName='FilterPolicy',
    AttributeValue={policy json}
)

I ended up replacing SNS with SQS and a Lambda function that sends the emails instead.我最终用 SQS 和 Lambda function 代替了发送电子邮件的 SNS。 The filtering is done inside the lambda, plus it sends a pretty text/html instead of the SNS json emails.过滤是在 lambda 内部完成的,另外它发送漂亮的文本/html 而不是 SNS json 电子邮件。 Will keep the question open if someone comes up with an answer.如果有人提出答案,将保持问题开放。

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

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