-
Notifications
You must be signed in to change notification settings - Fork 163
Description
PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.
NOTE: Google Cloud Python client libraries are no longer maintained inside this repository. Please visit the python-API repository (e.g., https://github.com/googleapis/python-pubsub/issues) for faster response times.
See all published libraries in the README.
Sample code:
def upload_file(f: io.StringIO, name: str):
f.seek(0)
b = bucket.blob(name)
b.upload_from_file(f)
Then I get this error:
Traceback
File "/opt/project/app.py", line 565, in upload_file
b.upload_from_file(f)
File "/usr/local/lib/python3.10/site-packages/google/cloud/storage/blob.py", line 2567, in upload_from_file
created_json = self._do_upload(
File "/usr/local/lib/python3.10/site-packages/google/cloud/storage/blob.py", line 2384, in _do_upload
response = self._do_resumable_upload(
File "/usr/local/lib/python3.10/site-packages/google/cloud/storage/blob.py", line 2228, in _do_resumable_upload
response = upload.transmit_next_chunk(transport, timeout=timeout)
File "/usr/local/lib/python3.10/site-packages/google/resumable_media/requests/upload.py", line 515, in transmit_next_chunk
return _request_helpers.wait_and_retry(
File "/usr/local/lib/python3.10/site-packages/google/resumable_media/requests/_request_helpers.py", line 148, in wait_and_retry
response = func()
File "/usr/local/lib/python3.10/site-packages/google/resumable_media/requests/upload.py", line 507, in retriable_request
result = transport.request(
File "/usr/local/lib/python3.10/site-packages/google/auth/transport/requests.py", line 549, in request
response = super(AuthorizedSession, self).request(
File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 394, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "/usr/local/lib/python3.10/http/client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/local/lib/python3.10/http/client.py", line 1327, in _send_request
body = _encode(body, 'body')
File "/usr/local/lib/python3.10/http/client.py", line 166, in _encode
raise UnicodeEncodeError(
UnicodeEncodeError: 'latin-1' codec can't encode character '\u200b' in position 202618: Body ('\u200b') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.
Python and Google's python package versions are as follows:
$ python --version
Python 3.10.5
$ pip list | grep google
google-api-core 2.8.2
google-api-python-client 2.51.0
google-auth 2.8.0
google-auth-httplib2 0.1.0
google-cloud-bigquery 3.2.0
google-cloud-bigquery-storage 2.13.2
google-cloud-core 2.3.1
google-cloud-storage 2.4.0
google-crc32c 1.3.0
google-resumable-media 2.3.3
googleapis-common-protos 1.56.2
Interestingly, upload_from_string
works fine (i.e. b.upload_from_string(f.getvalue())
).
This workaround is sufficient for me as my code uploads an in-memory file (io.StringIO
), and the file is not large. However, I think it is necessary to modify the behavior of the upload_from_file
function somehow.