Skip to content

download_blob_to_file() example does not working #701

@outk

Description

@outk

download_blob_to_file() example does not working. When I change 'w' to 'bw' in open(), that is working. Please check this bug.

Environment details

  • OS type and version: macOS Monterey v12.1
  • Python version: python --version: 3.10.2
  • pip version: pip --version: 21.3.1
  • google-cloud-storage version: pip show google-cloud-storage: 2.1.0

Code example

from google.cloud import storage
client = storage.Client()
uri = '{uri}'
with open('file-to-download-to', 'w') as file_obj:
    client.download_blob_to_file(uri, file_obj)

Reference:

Examples:
Download a blob using a blob resource.
>>> from google.cloud import storage
>>> client = storage.Client()
>>> bucket = client.get_bucket('my-bucket-name')
>>> blob = storage.Blob('path/to/blob', bucket)
>>> with open('file-to-download-to', 'w') as file_obj:
>>> client.download_blob_to_file(blob, file_obj) # API request.
Download a blob using a URI.
>>> from google.cloud import storage
>>> client = storage.Client()
>>> with open('file-to-download-to', 'w') as file_obj:
>>> client.download_blob_to_file(
>>> 'gs://bucket_name/path/to/blob', file_obj)

Stack trace

Traceback (most recent call last):
  File "{file}", line 14, in <module>
    storage_client.download_blob_to_file(uri, file_obj)
  File "{working dir}/.va/lib/python3.10/site-packages/google/cloud/storage/client.py", line 1128, in download_blob_to_file
    blob_or_uri._do_download(
  File "{working dir}/.va/lib/python3.10/site-packages/google/cloud/storage/blob.py", line 1000, in _do_download
    response = download.consume(transport, timeout=timeout)
  File "{working dir}/.va/lib/python3.10/site-packages/google/resumable_media/requests/download.py", line 174, in consume
    return _request_helpers.wait_and_retry(
  File "{working dir}/.va/lib/python3.10/site-packages/google/resumable_media/requests/_request_helpers.py", line 147, in wait_and_retry
    response = func()
  File "{working dir}/.va/lib/python3.10/site-packages/google/resumable_media/requests/download.py", line 170, in retriable_request
    self._write_to_stream(result)
  File "{working dir}/.va/lib/python3.10/site-packages/google/resumable_media/requests/download.py", line 106, in _write_to_stream
    self._stream.write(chunk)
TypeError: write() argument must be str, not bytes

Metadata

Metadata

Assignees

Labels

api: storageIssues related to the googleapis/python-storage API.type: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions