Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Binary string in Python, zlib

I want to store a large JSON (dict) from Python in dynamoDB.

After some investigation it seems that zlib is the way to go to get compression at a good level. Using below Im able to encode the dict.

ranking_compressed = zlib.compress(simplejson.dumps(response["Item"]["ranking"]).encode('utf-8'))

The (string?) then looks like this: b'x\x9c\xc5Z\xdfo\xd3....

I can directly decompress this and get the dict back with:

ranking_decompressed = simplejson.loads(str(zlib.decompress(ranking_compressed).decode('utf-8')))

All good so far. However, when putting this in dynamoDB and then reading it back using the same decompress code as above. The (string?) now looks like this:

Binary(b'x\x9c\xc5Z\xdf...

The error I get is:

 bytes-like object is required, not 'Binary'

Ive tried accessing the Binary with e.g. .data but I cant reach it.

Any help is appreciated.

like image 455
KalleJuhans Avatar asked Jan 22 '26 05:01

KalleJuhans


1 Answers

Boto3 Binary objects have a value property.

# in general...
binary_obj.value
# for your specific case...
ranking_decompressed = simplejson.loads(str(zlib.decompress(response["Item"]["ranking_compressed"].value).decode('utf-8')))

Oddly, this seems to be documented nowhere except the source code for the Binary class here

like image 199
GreenMachine Avatar answered Jan 23 '26 19:01

GreenMachine