在 SimpleDB 中编码 batch_put_attributes

Encoding in SimpleDB for batch_put_attributes

我正在使用 python 的 AWS SDK 与 SimpleDB 进行交互。

client = boto3.client('sdb')

example = [
    {'Name': 'test1', 'Attributes': [
        {'Name': 'speaker', 'Value': 'DIEGO BASSANTE'}]},
    {'Name': 'test2', 'Attributes': [
        {'Name': 'speaker', 'Value': 'SERGIO JOSE'}]}]

response = client.batch_put_attributes(
    DomainName='activities',
    Items=example
)

此代码有效,但如果 Value 具有特殊字符,如 ñ, á, é, í, ó, ú,那么我会收到错误消息:

botocore.exceptions.ClientError: An error occurred (SignatureDoesNotMatch) when calling the BatchPutAttributes operation: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

SDB suposes 将数据存储为 UTF-8,我还尝试将字段 AlternateValueEncoding 添加到方法 batch_delete_attributes 记录的属性中,但仍然没有成功。

想过在发送到 SDB 时将数据编码为 base64,在取回数据时进行解码,但我不确定这是正确的答案。那我错过了什么?

python:3.6.2 boto3: 1.4.5

似乎这是一个已报告的问题 https://github.com/boto/boto3/issues/354

问题是发送到 sdb 的请求需要 header Content-Type

中的值 charset=utf-8

建议的解决方案对我有用,只需将此片段复制到我的代码中

from botocore import endpoint


def make_request(self, operation_model, request_dict):
    if self._endpoint_prefix == 'sdb':
        request_dict['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
    return self._send_request(request_dict, operation_model)
endpoint.Endpoint.make_request = make_request