【问题标题】:Python Azure SDK - Unable to download zip filePython Azure SDK - 无法下载 zip 文件
【发布时间】:2017-08-17 12:46:29
【问题描述】:

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

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

错误可能来自请求包,我似乎无权更改标头或类似的东西。

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

【问题讨论】:

  • 您能检查 blob 的属性,尤其是 content-encoding 属性值吗?如果它设置为 gzip,只需通过将其值设置为空字符串来更新 blob 属性。
  • 成功了!谢谢

标签: python azure azure-blob-storage azure-storage-files


【解决方案1】:

作为总结,我尝试使用 Microsoft Azure Storage Explorer Tool 验证上述异常。

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

下载的时候客户端会检查文件类型是否可以压到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 航行时,您可以将 EncodingType 属性设置为 None 或确保 EncodingType 设置与文件本身的类型匹配。

另外,你可以参考SO线程python making POST request with JSON data。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多