سؤال

I can't determine from the docs/examples how to store/read binary data from DynamoDB using boto's dynamodb2. How is it done?

My guess was with an item value like { 'B': binary-data } but that causes an error in the JSON encoder.

هل كانت مفيدة؟

المحلول 2

It needs to be base 64 encoded into a string.

from base64 import b64encode
data = {'B': b64encode(binary_data)}

There is a library that can do this for you: PynamoDB. The code which handles serialization to and from binary for Python 2 and 3 can be found here.

Disclaimer: I am the author of PynamoDB.

نصائح أخرى

boto provides the Binary class to do this automatically:

from boto.dynamodb2.table import Table
from boto.dynamodb.types import Binary

Table('mytable').put_item({'hashkey': Binary('\x01\x02')})
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top