又拍云python sdk的一个bug

upyun的pysdk存在bug,会导致部分文件上传失败。测试图片

问题原因

部分图像处理软件会去掉图片的description,而python的fileno如果没有取到description会抛出一个异常,程序没有处理这个异常,从而导致了无法上传。 pysdk有判断这个方法是否存在,但是没有处理这个异常。 https://docs.python.org/3/library/io.html#io.IOBase.fileno

解决办法

打开sdk的upyun.py中的173行处,修改

    if hasattr(value, 'fileno'):
        length = os.fstat(value.fileno()).st_size
    elif hasattr(value, '__len__'):
        length = len(value)
        headers['Content-Length'] = length
    elif value is not None:
        raise UpYunClientException('object type error')

    if hasattr(value, 'getvalue'):
        length = len(value.getvalue())
        headers['Content-Length'] = length
    elif hasattr(value, '__len__'):
        length = len(value)
        headers['Content-Length'] = length
    elif hasattr(value, 'fileno'):
        length = os.fstat(value.fileno()).st_size
        headers['Content-Length'] = length
    elif value is not None:
        raise UpYunClientException('object type error')