question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Value Error when uploading resized images to Google Cloud Storage

See original GitHub issue

My 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:open
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
bgracecommented, May 19, 2019

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:

>>> iobuffer = io.BytesIO()  # empty buffer (0 bytes)
>>> sys.getsizeof(iobuffer)
88
>>> len(iobuffer.getbuffer())
0
>>> 

This following code works for me with django-storages and GCS when the returned thumbnail is saved to the model:

def generate_thumbnail(src):
    image = Image.open(src)  # in memory
    image.thumbnail(settings.THUMBNAIL_SIZE, Image.ANTIALIAS)
    buffer = BytesIO()
    image.save(buffer, 'JPEG')
    file_name = Path(src.name).name

    temp_file = InMemoryUploadedFile(buffer, None, file_name, 'image/jpeg', len(buffer.getbuffer()), None)
    return temp_file
0reactions
Daniil0891commented, Jul 28, 2022

@bgrace Thanks a lot for a working solution!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found