Value Error when uploading resized images to Google Cloud Storage
See original GitHub issueMy image model contains;
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
image_asset = img.open(self.image)
image_name = uuid.uuid4()
image_asset.thumbnail((1200, 1200), img.ANTIALIAS)
fi_io = io.BytesIO()
image_asset.save(fi_io, format='JPEG', quality=90)
self.image = InMemoryUploadedFile(
fi_io,
'ImageField',
'%s.jpg' % image_name,
'image/jpeg',
sys.getsizeof(fi_io), None
)
super(Image, self).save(force_update=force_update)
Whenever I attempt to save an image I get this error (the exact size depends on the file uploaded):
ValueError: Size 3984 was specified but the file-like object only had 3862 bytes remaining.
Without this code, the image saves fine. Also when switching to a Digital Ocean storage this exact code works without a problem.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
ValueError when uploading resized django ... - Stack Overflow
so i changed sys.getsizeof(output) to len(output.getbuffer()) and that's it it works with both google cloud and local media files.
Read more >ValueError when uploading resized django images to google ...
Coding example for the question ValueError when uploading resized django images to google cloud-django.
Read more >Optimize and resize images in Google Cloud Storage | Guide
Delivering resized & optimized images from google cloud storage is now easier than ever. With this guide, Smart crop images, create image ......
Read more >Troubleshooting | Cloud Storage
Issue: While performing a resumable upload, I received this error and the message Failed to parse Content-Range header. Solution: The value you used...
Read more >Display images and documents - AppSheet Help
In your spreadsheet, image or document values are stored as either a URL or a ... Google Drive: Viewing an image on Google...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think this is an error in the end user code which GCS rejects and the other services are more liberal about. The call sys.getsizeof(fi_io) yields the size of the
BytesIO
object, not the size of the buffer:This following code works for me with django-storages and GCS when the returned thumbnail is saved to the model:
@bgrace Thanks a lot for a working solution!