diff --git a/django_gcloud_storage/__init__.py b/django_gcloud_storage/__init__.py index 2278067..57ae8fa 100644 --- a/django_gcloud_storage/__init__.py +++ b/django_gcloud_storage/__init__.py @@ -6,6 +6,7 @@ import re from tempfile import SpooledTemporaryFile +import django from django.conf import settings from django.core.exceptions import SuspiciousFileOperation from django.core.files.base import File @@ -17,6 +18,8 @@ from gcloud.exceptions import NotFound from gcloud.storage.bucket import Bucket +DJANGO_17 = django.get_version().startswith('1.7.') + try: # For Python 3.0 and later from urllib import parse as urlparse @@ -78,7 +81,13 @@ def __init__(self, blob, maxsize=1000): def _update_blob(self): # Specify explicit size to avoid problems with not yet spooled temporary files # Djangos File.size property already knows how to handle cases like this - self._blob.upload_from_file(self._tmpfile, size=self.size, rewind=True) + + if DJANGO_17 and self._tmpfile.name is None: # Django bug #22307 + size = self._tmpfile.tell() + else: + size = self.size + + self._blob.upload_from_file(self._tmpfile, size=size, rewind=True) def write(self, content): self._dirty = True