Google-cloud-python: blob.download_as_string failure (google-cloud-storage 1.3.2)

Created on 23 Sep 2017  路  2Comments  路  Source: googleapis/google-cloud-python

I'm having trouble using the blob.download_as_string and the related blob.download_to_file functionality of google.cloud.storage. I noticed a similar error has come up on the Google Cloud Developers slack.

I installed google-cloud more recently than any other module, so if dependencies are broken, they are not picked up by the pip3 install google-cloud invocation.

google-cloud-storage is version 1.3.2. Downgrading to version 1.2.0 fixed my problems.

  1. OS type and version: OS X 10.12.6
  2. Python version and virtual environment information: python 3.6.0
  3. google-cloud-python version:
Name: google-cloud
Version: 0.27.0
Summary: API Client library for Google Cloud
Home-page: https://github.com/GoogleCloudPlatform/google-cloud-python
Author: Google Cloud Platform
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python3.6/site-packages
Requires: google-cloud-resource-manager, google-cloud-videointelligence, google-cloud-runtimeconfig, google-cloud-bigtable, google-cloud-datastore, google-cloud-translate, google-cloud-core, google-cloud-language, google-cloud-dns, google-cloud-error-reporting, google-cloud-pubsub, google-cloud-bigquery, google-cloud-speech, google-cloud-storage, google-cloud-monitoring, google-cloud-vision, google-cloud-spanner, google-cloud-logging
  1. Stacktrace:
> blob.download_as_string()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-46f3cd070ec1> in <module>()
----> 1 blob.download_as_string()

/usr/local/lib/python3.6/site-packages/google/cloud/storage/blob.py in download_as_string(self, client)
    500         """
    501         string_buffer = BytesIO()
--> 502         self.download_to_file(string_buffer, client=client)
    503         return string_buffer.getvalue()
    504

/usr/local/lib/python3.6/site-packages/google/cloud/storage/blob.py in download_to_file(self, file_obj, client)
    462
    463         try:
--> 464             self._do_download(transport, file_obj, download_url, headers)
    465         except resumable_media.InvalidResponse as exc:
    466             _raise_from_invalid_response(exc)

/usr/local/lib/python3.6/site-packages/google/cloud/storage/blob.py in _do_download(self, transport, file_obj, download_url, headers)
    416         if self.chunk_size is None:
    417             download = Download(download_url, stream=file_obj, headers=headers)
--> 418             download.consume(transport)
    419         else:
    420             download = ChunkedDownload(

/usr/local/lib/python3.6/site-packages/google/resumable_media/requests/download.py in consume(self, transport)
     99
    100         if self._stream is not None:
--> 101             self._write_to_stream(result)
    102
    103         return result

/usr/local/lib/python3.6/site-packages/google/resumable_media/requests/download.py in _write_to_stream(self, response)
     60             response (~requests.Response): The HTTP response object.
     61         """
---> 62         with response:
     63             body_iter = response.iter_content(
     64                 chunk_size=_SINGLE_GET_CHUNK_SIZE, decode_unicode=False)

AttributeError: __enter__
  1. Steps to reproduce:
    ```
    > from google.cloud import storage
    > bucket_name = a_real_bucket # insert a real bucket here.
    > blob_name = a_real_blob # insert a real blob here.
    > client = storage.Client()
    > bucket = client.bucket(bucket_name)
    > bucket.exists()
    True
    > blob = bucket.get_blob(blob_name)
    > blob.exists()
    True
    > blob.download_as_string() # produces the above error.
storage packaging

Most helpful comment

Duplicate of #3747: you can work around the issue by upgrading requests, e.g.:

$ /path/to/venv/bin/pip install -U requests

All 2 comments

Duplicate of #3747: you can work around the issue by upgrading requests, e.g.:

$ /path/to/venv/bin/pip install -U requests

That worked for me ^

Was this page helpful?
0 / 5 - 0 ratings