Azure / azure-storage-python

Microsoft Azure Storage Library for Python
https://azure-storage.readthedocs.io
MIT License
338 stars 240 forks source link

create_blob_from_stream does not support string stream (StringIO or file in text mode) #546

Open nitely opened 5 years ago

nitely commented 5 years ago

Which service(blob, file, queue) does this issue concern?

blob

Which version of the SDK was used? Please provide the output of pip freeze.

Azure 4.0.0, blob 1.4.0

What problem was encountered?

create_blob_from_stream says it supports io.IOBase, however it only supports bytes stream, not string/plain text stream or file.

Relevant function and error raised

def _get_data_bytes_only(param_name, param_value):
        '''Validates the request body passed in and converts it to bytes
        if our policy allows it.'''
        if param_value is None:
            return b''

        if isinstance(param_value, bytes):
            return param_value

>       raise TypeError(_ERROR_VALUE_SHOULD_BE_BYTES.format(param_name))
E       TypeError: foobar should be of type bytes.

Have you found a mitigation/solution?

No.

Note: for table service, please post the issue here instead: https://github.com/Azure/azure-cosmosdb-python.

zezha-msft commented 5 years ago

Hi @nitely, thanks for reaching out!

This is related to #536. It seems that the SDK doesn't support string stream in general. We'll investigate and get back to you.

nitely commented 5 years ago

FWIW I don't think the fix is worth it. I'd bet implementing this for multi-thread upload is non-trivial and the solution won't be great performance-wise.

But I think at the very least the docstring should be fixed to accept io.BytesIO as type instead of IOBase actually, is should be io.BytesIO|io.FileIO since it accepts a file descriptor too, but only in binary mode.

My 2c