Open ceefour opened 6 years ago
Workaround: Instead of using BytesIO I am now using ContentFile
and it works:
# Fetch photo if we're missing and Firebase has it
if not profile.photo and claims.get('picture'):
logger.info('Fetching photo for %s: %s ...', profile, claims['picture'])
r = requests.get(claims['picture'])
logger.debug('Fetched %s (%s bytes)', claims['picture'], len(r.content))
photo_name = '%s-%s-%s.jpg' % (profile.id, slugify(profile.name), random.randint(1000, 9999))
logger.debug('Saving %s into S3 %s ...', claims['picture'], photo_name)
profile.photo.save(photo_name, ContentFile(r.content))
logger.debug('Saved %s into S3 %s', claims['picture'], profile.photo.name)
My question is:
From my experience with working with Django's storage system your original usage was incorrect. You need to use the right file class from Django for every particular case.
The problem is not that you are using BytesIO
(internally ContentFile
will also use BytesIO
when you pass binary data) but actually the problem is using of File
to wrap BytesIO
. By default File
represent a file from the file system. In some cases it can works with BytesIO
but is not designed to work in all cases with it. For that reason in Django there are other classes which subclass File
in order to make it work correctly. One of them is ContentFile
and other (I'm not sure if it is documented) is InMemoryUploadedFile
(located in 'django.core.files.uploadedfile').
I'm not sure if some changes can be made in ImageKit in order to raise appropriate exception or even to make it work even if used as you make it in the first place.
I'll leave the issue for now open if someone want to try to investigate if we can improve ImageKit behaviour somehow in this direction.
Model:
Code:
Error:
This seems to be similar to #339 however I'm not calling
.url
at all. This happens duringsave()