简体   繁体   English

使用boto3在dynamodb put_item中的Unicode解码错误

[英]Unicode decode error in dynamodb put_item using boto3

I'm trying to store API passwords and the like in a dynamodb table, encrypted with AES, and I'm having trouble storing it. 我试图将API密码等存储在用AES加密的dynamodb表中,但无法存储它。 I have a dict with data that I'm writing. 我对正在写的数据有意见。

for key in endpoint:
        if 'password' in key or 'security' in key or 'secret' in key:
            #encrypt the value
            obj = AES.new(
                landlord_registry['default_crypt_salt'],
                AES.MODE_CFB,
                landlord_registry['default_crypt_iv'])
            endpoint[key] = {'B' : obj.encrypt(endpoint[key])}
        else:
            endpoint[key] = {'S' : endpoint[key]}

The resulting dict looks like this: 结果字典如下所示:

{'mvp_version': {'S': '0.3'}, 'endpoint_details': [{u'time_interval': {'S': u'30'}, u'username': {'S': u'blahblah@blahblah.org'}, u'primary_key': {'S': u'Id'}, u'security_token': {'B': '&\\xd9\\t\\x7b\\x9...'}, u'service_name': {'S': u'blahblah'}, u'enabled': {'S': u'1'}, u'endpoint_id': {'S': u'SFORG0'}, u'password': {'B': 'K9\\x958,\\x31.... '}}, {u'username': {'S': u'usernamelala'}, u'primary_key': {'S': u'ID__'}, u'database': {'S': u'z8_dev1'}, u'service_name': {'S': u'sdasda'}, u'enabled': {'S': u'1'}, u'host_name': {'S': u'lalala.rds.amazonaws.com'}, u'endpoint_id': {'S': u'MYSQL1'}, u'password': {'B': 'E##\\xe2n....'}}]}

The error is: Dynamo Insert Error: (, UnicodeDecodeError('utf8', ... , 1, 2, 'invalid continuation byte') 错误是:Dynamo插入错误:(,UnicodeDecodeError('utf8',...,1,2,'无效的继续字节')

But from what I can tell, since I've used {'B' : 'encoded text'} it should try to store this as bytes not unicode text. 但是据我所知,由于我使用过{'B':'encoded text'},因此应尝试将其存储为字节而不是unicode文本。

I needed to use the Binary(text) wrapper in Python 2 in order to store the binary text in dynamodb. 我需要在Python 2中使用Binary(text)包装器,以便将二进制文本存储在dynamodb中。 So the line: 所以这行:

endpoint[key] = {'B' : obj.encrypt(endpoint[key])}

should look like: 应该看起来像:

endpoint[key] = {'B' : Binary(obj.encrypt(endpoint[key]))}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM