0

我在Python 3.5.2上使用最新版本Azure Storgae SDKPython Azure SDK - 无法下载zip文件

我想从Azure存储云上的blob下载zip文件

我的代码:

self.azure_service= BlockBlobService(account_name = ACCOUNT_NAME, 
            account_key = KEY) 

with open(local_path, "wb+") as f: 
    self.azure_service.get_blob_to_stream(blob_container, 
              file_cloud_path, 
              f) 

的错误:

AzureException: ('Received response with content-encoding: gzip, but failed to decode it.,, error('Error -3 while decompressing data: incorrect header check',))

的错误可能是从请求包来了,我似乎并没有把改变访问标题或类似的东西。

究竟是什么问题,我该如何解决它?

+2

你可以检查blob的属性,特别是内容编码属性值吗?如果它设置为gzip,只需通过将其值设置为空字符串来更新blob属性即可。 –

+0

工作!谢谢 –

回答

1

就像总结一样,我试图用Microsoft Azure存储浏览器工具来验证上述例外情况。

当用户上传压缩类型的文件,如果设置编码类型属性gzip的

enter image description here

在下载时,客户端会检查文件类型是否可以是郁闷编码类型,如果dismatch会出现如下异常:

Traceback (most recent call last): 
    File "D:\Python35\lib\site-packages\urllib3\response.py", line 266, in _decode 
    data = self._decoder.decompress(data) 
    File "D:\Python35\lib\site-packages\urllib3\response.py", line 66, in decompress 
    return self._obj.decompress(data) 
zlib.error: Error -3 while decompressing data: incorrect header check 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "D:\Python35\lib\site-packages\requests\models.py", line 745, in generate 
    for chunk in self.raw.stream(chunk_size, decode_content=True): 
    File "D:\Python35\lib\site-packages\urllib3\response.py", line 436, in stream 
    data = self.read(amt=amt, decode_content=decode_content) 
    File "D:\Python35\lib\site-packages\urllib3\response.py", line 408, in read 
    data = self._decode(data, decode_content, flush_decoder) 
    File "D:\Python35\lib\site-packages\urllib3\response.py", line 271, in _decode 
    "failed to decode it." % content_encoding, e) 
urllib3.exceptions.DecodeError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check',)) 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "D:\Python35\lib\site-packages\azure\storage\storageclient.py", line 222, in _perform_request 
    response = self._httpclient.perform_request(request) 
    File "D:\Python35\lib\site-packages\azure\storage\_http\httpclient.py", line 114, in perform_request 
    proxies=self.proxies) 
    File "D:\Python35\lib\site-packages\requests\sessions.py", line 508, in request 
    resp = self.send(prep, **send_kwargs) 
    File "D:\Python35\lib\site-packages\requests\sessions.py", line 658, in send 
    r.content 
    File "D:\Python35\lib\site-packages\requests\models.py", line 823, in content 
    self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes() 
    File "D:\Python35\lib\site-packages\requests\models.py", line 750, in generate 
    raise ContentDecodingError(e) 
requests.exceptions.ContentDecodingError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check',)) 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "D:/PythonWorkSpace/AzureStorage/BlobStorage/CreateContainer.py", line 20, in <module> 
    f) 
    File "D:\Python35\lib\site-packages\azure\storage\blob\baseblobservice.py", line 1932, in get_blob_to_stream 
    _context=operation_context) 
    File "D:\Python35\lib\site-packages\azure\storage\blob\baseblobservice.py", line 1659, in _get_blob 
    operation_context=_context) 
    File "D:\Python35\lib\site-packages\azure\storage\storageclient.py", line 280, in _perform_request 
    raise ex 
    File "D:\Python35\lib\site-packages\azure\storage\storageclient.py", line 252, in _perform_request 
    raise AzureException(ex.args[0]) 
azure.common.AzureException: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check',)) 

Process finished with exit code 1 

解决方案:

作为@Gaurav Mantri帆,您可以将EncodingType属性设置为或确保EncodingType设置与文件本身的类型匹配。

enter image description here

此外,您可以参考SO线程python making POST request with JSON data