简体   繁体   中英

Extracting Byte values from Azure Table Storage

I am writing a python script to query data from an Azure Table Storage.

The data which I want to extract is of type Binary (The info is serialized and stored)

When I run the code using entity['BodyChunk01] I get the value back as

'BodyChunk01': <azure.data.tables._entity.EntityProperty object at 0x0DDFFB10>

(The name of the column is BodyChunk01)

and when I use entity['BodyChunk01'].value as mentioned in another question on stackoverflow, it returns the deserialized json data but I want the actual serialized value that is stored as you can see in the screenshot below

样本字节数据

I believe what you're trying to accomplish is not possible. I looked up the SDK code here and it seems the SDK is forcefully doing the conversion based on the code here .

def _decode_base64_to_bytes(data):
    if isinstance(data, six.text_type):
        data = data.encode("utf-8")
    return base64.b64decode(data)

You may want to raise an issue here: https://github.com/Azure/azure-sdk-for-python/issues and bring this to SDK team's attention.

Binary data is converted into a `Base64 encoded string` and is then saved. What you would need to do is do a Base64 decode to get the byte array.

I am not well versed with Python but you can do something like this:

 import base64 decodedBytes = base64.b64decode(entity['BodyChunk01'].value) decodedStr = str(decodedBytes, "utf-8")

If you want the entity in a raw format you can include a header to not return OData types and this will pass by the deserialization. Ints, floats, booleans are converted to their respective types, but a binary entity property will be returned as the raw string. Here's a code snippet:

headers = {'Accept': 'application/json;odata=nometadata'}

received_entity = table_client.get_entity(
    row_key=entity['RowKey'],
    partition_key=entity['PartitionKey'],
    headers=headers
)

print(received_entity['BodyChunk01'])

Disclaimer: I work on the Azure SDK for Python team

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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