Python Azure SDK - 无法下载zip文件

时间:2017-08-17 12:46:30

标签: python azure python-requests azure-storage-blobs azure-storage-files

我在Azure Storgae SDK上使用了最新版本的Python 3.5.2

我想从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',))

错误可能来自请求包,我似乎无法更改标题或类似内容。

究竟是什么问题以及如何解决?

1 个答案:

答案 0 :(得分:1)

就像摘要一样,我尝试使用Microsoft Azure存储资源管理器工具验证上述异常。

当用户上传 zip 类型文件时,如果为 gzip 设置 EncodingType 属性。

enter image description here

在下载时,客户端将检查文件类型是否可以按下 EncodingType ,如果不匹配将发生如下异常:

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 sail,您可以将EncodingType属性设置为,或者确保EncodingType设置与文件本身的类型匹配。

enter image description here

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