参数消息的类型无效



我尝试在 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'])

我收到错误为:

参数

验证失败:参数消息的类型无效, 值: {'名称': 'AWS', '描述': '测试'}, 类型: , 有效类型:

在 AWS 文档中说,如果我想为每个传输协议发送不同的消息,请将MessageStructure参数的值设置为 JSON,并将 JSON 对象用于消息参数。我的代码出了什么问题?

注意:我想发送的不仅仅是值,所以我需要发送 JSON

在粘贴的示例中,消息是字典。这可能是错误的原因。尝试按如下方式修改消息:

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={}
)

最新更新