在关于如何存储大的数据类型的说明中,文档:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-use-s3-too.html 提到了:
Compressing large attribute values can let them fit within item limits
in DynamoDB and reduce your storage costs. Compression algorithms such
as GZIP or LZO produce binary output that you can then store in a
Binary attribute type.
它的意思是自行选用GZIP或者 LZO压缩吧。python的话,其实就二句话的事:
import gzip s = 'Hi,this is Linc Hu' b = gzip.compress(bytes(s),encoding='utf8'))
不过需要注意的事,当你post这个item到DynamoDB存为Binary类型的时候。又被再次base64加密了哦。确切说不算是加密吧。算是编码而已。取回来后decode一下先即可。
工作测试需要,某个Attribute 打算压缩了存储成Binary类型。
下面是字符被转换被压缩被存储的各个格式的正向的过程
s=str(d) # dict/json to string b=bytes(s,encoding='utf8') #string to bytes z=gzip.compress(b) #compress bytes ddbb=base64.b64encode(z) # base64 encode when put item to ddb
下面是逆向解密过程:
## from ddbb to dict gzip_bytes = base64.b64decode(binary_str_in_ddb) orginal_bytes = gzip.decompress(gzip_bytes) orginal_json =ast.literal_eval(orginal_bytes.decode('utf-8'))