-
-
Notifications
You must be signed in to change notification settings - Fork 859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ContentFile with str content throws TypeError #708
Comments
Try this: |
Yeah, https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#model |
|
See #707 |
@sww314 This issue should be re-opened. It is perfectly valid Django to initialize a This can be seen in the Django docs: https://docs.djangoproject.com/en/dev/ref/files/file/#django.core.files.base.ContentFile So this is a bug in django-storages that should be fixed. |
Note that the same error occurs when saving a Django
|
@jschneier Can this issue be re-opened? As per my above comment I believe this is a valid bug in django-storages. |
Hi @LincolnPuzey, I've implemented a workaround for this issue in module druids/django-chamber. You might want to take a look at it. Nevertheless I agree with you that this behavior in django-storages is wrong and should be fixed. |
2 of these tests currently fail due to issue jschneier#708. They use moto to mock s3 at a lower level and actually get the right error to surface.
2 of these tests currently fail due to issue jschneier#708. They use moto to mock s3 at a lower level and actually get the right error to surface.
…per to handle file objects that are open in text/string mode. This is done before gzip compressing, so also removed force_bytes() call in _compress_content(). Fix test that failed because of this.
2 of these tests currently fail due to issue jschneier#708. They use moto to mock s3 at a lower level and actually get the right error to surface.
…per to handle file objects that are open in text/string mode. This is done before gzip compressing, so also removed force_bytes() call in _compress_content(). Fix test that failed because of this.
2 of these tests currently fail due to issue jschneier#708. They use moto to mock s3 at a lower level and actually get the right error to surface.
…per to handle file objects that are open in text/string mode. This is done before gzip compressing, so also removed force_bytes() call in _compress_content(). Fix test that failed because of this.
…per. This correctly handles file-like-objects that are open in text/string mode by converting to strings returned by read() to bytes, which is what upload_fileobj() requires. This is done before gzip compressing, so also removed force_bytes() call in _compress_content().
Any update on this? |
…per. This correctly handles file-like-objects that are open in text/string mode by converting to strings returned by read() to bytes, which is what upload_fileobj() requires. This is done before gzip compressing, so also removed force_bytes() call in _compress_content().
* Add alternate .venv to gitignore * Add moto as test dependency. * Add ReadBytesWrapper utility class for wrapping a file-like object. This makes .read() always return bytes. If .read() returns a string, it will be encoded to bytes before being returned. The encoding to use can be specified, otherwise will use the .encoding property of the original file, otherwise will use utf-8. * Fix issue #708: Make _save() wrap content in a ReadBytesWrapper. This correctly handles file-like-objects that are open in text/string mode by converting to strings returned by read() to bytes, which is what upload_fileobj() requires. This is done before gzip compressing, so also removed force_bytes() call in _compress_content(). * Add tests for saving both File/ContentFile with string/bytes. Add these tests in a new test class that uses moto. Remove old test for saving ContentFile Move test for detecting content-type to this new class. Add some more tests around this. Fix tests that fail because settings.AWS_STORAGE_BUCKET_NAME is now defined. Fix tests that fail because content is always wrapped. Fix test for gzipped file since that now only takes bytes. * tweaks * add close to ReadBytesWrapper * add readable to ReadBytesWrapper --------- Co-authored-by: Josh Schneier <josh.schneier@gmail.com>
Fixed by #911 |
Hello, I've run into problems when I tried saving Django
ContentFile
. When I save the file with, for example,'hello'
, it throws following exception:TypeError: Unicode-objects must be encoded before hashing
.However everything works fine with
bytes
. Below is traceback from Django shell. I've tried it on Django 1.11 and 2.2 with latest versions of both boto3 and django-storages.Traceback:
The text was updated successfully, but these errors were encountered: