django-backblazeb2-storage
django-backblazeb2-storage copied to clipboard
Error when using a UTF8-encoded filename
Using a filename encoded as utf8 (i.e. a normal Python3 string) results in an error:
2017-03-21T21:49:40.383023+00:00 app[web.1]: File "/app/apps/donations/views.py", line 29, in get_success_url
2017-03-21T21:49:40.383032+00:00 app[web.1]: self.name = self.storage.save(name, content, max_length=self.field.max_length)
2017-03-21T21:49:40.383026+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/files.py", line 91, in save
2017-03-21T21:49:40.383034+00:00 app[web.1]: File "/app/.heroku/src/django-backblazeb2-storage/b2_storage/backblaze_b2.py", line 59, in upload_file
2017-03-21T21:49:40.383035+00:00 app[web.1]: download_response = requests.post(url, headers=headers, data=content.read())
2017-03-21T21:49:40.383036+00:00 app[web.1]: return request('post', url, data=data, json=json, **kwargs)
2017-03-21T21:49:40.383033+00:00 app[web.1]: File "/app/.heroku/src/django-backblazeb2-storage/b2_storage/storage.py", line 23, in save
2017-03-21T21:49:40.383037+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/requests/api.py", line 56, in request
2017-03-21T21:49:40.383033+00:00 app[web.1]: resp = self.b2.upload_file(name, content)
2017-03-21T21:49:40.383037+00:00 app[web.1]: return session.request(method=method, url=url, **kwargs)
2017-03-21T21:49:40.383035+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/requests/api.py", line 110, in post
2017-03-21T21:49:40.383038+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/requests/sessions.py", line 488, in request
2017-03-21T21:49:40.383039+00:00 app[web.1]: resp = self.send(prep, **send_kwargs)
2017-03-21T21:49:40.383040+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/requests/adapters.py", line 423, in send
2017-03-21T21:49:40.383041+00:00 app[web.1]: timeout=timeout
2017-03-21T21:49:40.383039+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/requests/sessions.py", line 609, in send
2017-03-21T21:49:40.383040+00:00 app[web.1]: r = adapter.send(request, **kwargs)
2017-03-21T21:49:40.383042+00:00 app[web.1]: chunked=chunked)
2017-03-21T21:49:40.383044+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 356, in _make_request
2017-03-21T21:49:40.383045+00:00 app[web.1]: conn.request(method, url, **httplib_request_kw)
2017-03-21T21:49:40.383042+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 600, in urlopen
2017-03-21T21:49:40.383045+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/http/client.py", line 1239, in request
2017-03-21T21:49:40.383046+00:00 app[web.1]: self._send_request(method, url, body, headers, encode_chunked)
2017-03-21T21:49:40.383048+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/http/client.py", line 1212, in putheader
2017-03-21T21:49:40.383049+00:00 app[web.1]: values[i] = one_value.encode('latin-1')
2017-03-21T21:49:40.383047+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/http/client.py", line 1280, in _send_request
2017-03-21T21:49:40.383047+00:00 app[web.1]: self.putheader(hdr, value)
2017-03-21T21:49:40.383053+00:00 app[web.1]: UnicodeEncodeError: 'latin-1' codec can't encode character '\u0107' in position 30: ordinal not in range(256)
This is how I'm using the storage (works when I use an ASCII string):
from io import BytesIO
...
self.barcode.save(
"Maja Majić",
BytesIO(response.content),
save=True
)
Hi , what do you recommend ? can you make a PR ?
I'm not sure how to fix this straight away, unfortunately. I made a workaround to always use ASCII chars in the filename (e.g. just use the object id) to avoid this error.
Ok , I will make something in this days.