python-3.xaws-sdkamazon-simpledb

Encoding in SimpleDB for batch_put_attributes


I'm using the AWS SDK for python to interact with 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
)

This code works, but if the Value has a special character like ñ, á, é, í, ó, ú then I get an error:

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 to store the data as UTF-8 and I also tried adding the field AlternateValueEncoding to the attributes as documented for the method batch_delete_attributes but still no luck.

Thought on encoding the data to base64 when sending to SDB and decoding when getting the data back, but I'm not sure that is the proper answer. So what am I missing?

python: 3.6.2 boto3: 1.4.5


Solution

  • Seems like it is a reported issue https://github.com/boto/boto3/issues/354

    The problem is that the request sent to sdb needs the value charset=utf-8 in the header Content-Type

    The proposed solution worked for me, just to copy this snippet in my code

    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