1 django amazon-web-services amazon-sns python-3.x
我尝试使用 Django 中的“signal”在 AWS 中发送 SNS 电子邮件,我的代码是:
import boto3
from properties.models import PropertyList
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
@receiver(post_save, sender=PropertyList)
def send_property_details(sender, instance, created, **kwargs):
if created:
sns = boto3.client('sns')
response = sns.publish(
TopicArn='',# I write value of TopicArn
Message={
"name": instance.title,
"description": instance.description
},
MessageStructure='json',
Subject='New Property Created',
MessageAttributes={
'default':{
'DataType':'String',
'StringValue':instance.title
}
},
)
print(response['MessageId'])
Run Code Online (Sandbox Code Playgroud)
我收到错误如下:
参数验证失败:参数消息类型无效,值:{'name': 'aws', 'description': 'test'},类型: ,有效类型:
在AWS文档中说,如果我想为每个传输协议发送不同的消息,请将参数的值设置MessageStructure为JSON并使用JSON对象作为消息参数。我的代码有什么问题?
注意:我想发送的不仅仅是值,所以我需要发送 JSON
在示例中,您粘贴的消息是一本字典。这可能是错误的原因。尝试修改Message如下:
import boto3, json
...
mesg = json.dumps({
"default": "defaultfield", # added this
"name": instance.title,
"description": instance.description
})
response = sns.publish(
TopicArn='topicARNvalue',
Message=mesg,
MessageStructure='json',
Subject='New Property Created',
MessageAttributes={}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9589 次 |
| 最近记录: |