Search code examples
pythonazureazure-blob-storageazure-storage-files

Python Azure SDK - Unable to download zip file


I am using the latest version of Azure Storgae SDK on Python 3.5.2.

I want to download a zip file from a blob on Azure storage cloud.

My Code:

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)

The Error:

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

The error is probably coming from the requests package and i don't seem to have access for changing the headers or something like that.

What exactly is the problem and how can i fix it?


Solution

  • Just as summary,I tried to verify the above exception with Microsoft Azure Storage Explorer Tool.

    When user upload a zip type file , if set the EncodingType property for gzip.

    enter image description here

    at the time of download the client will check whether the file type can be to depressed to EncodingType , if dismatch will occur the exception as below:

    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
    

    Solution:

    As @Gaurav Mantri sail, you could set the EncodingType property to None or ensure that the EncodingType setting matches the type of the file itself.

    enter image description here

    Also,you could refer to the SO thread python making POST request with JSON data.