2
votes

I can not send messages with accented characters for SQS in python with the AWS SDK (boto).

Versions

Python: 2.7.6 boto: 2.20.1

CODE

   #!/usr/bin/env python
   # -*- coding: utf-8 -*-

import boto.sqs
from boto.sqs.message import RawMessage

    # print boto.Version
sqs_conn = boto.sqs.connect_to_region(
'my_region',
aws_access_key_id='my_kye',
aws_secret_access_key='my_secret_ky')
queue = sqs_conn.get_queue('my_queue')
queue.set_message_class(RawMessage)

msg = RawMessage()

body = '1 café, 2 cafés, 3 cafés ...'
msg.set_body(body)
queue.write(msg)
1

1 Answers

1
votes

One solution:

import sys
reload(sys)                           
sys.setdefaultencoding('utf-8')

Full code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import boto.sqs
from boto.sqs.message import RawMessage
import sys                               #  <== added this line

reload(sys)                              #  <== added this line
sys.setdefaultencoding('utf-8')          #  <== added this line

# print boto.Version
sqs_conn = boto.sqs.connect_to_region(
    'my_region',
    aws_access_key_id='my_kye',
    aws_secret_access_key='my_secret_ky')
queue = sqs_conn.get_queue('my_queue')
queue.set_message_class(RawMessage)

msg = RawMessage()

body = '1 café, 2 cafés, 3 cafés ...'
msg.set_body(body)
queue.write(msg)

Source: https://pythonadventures.wordpress.com/2012/03/29/ascii-codec-cant-encode-character/#comment-4672