Commits

Ian Lewis committed e27c8b6 Merge

Merged in pendletongp/django-storages-dont-eat-my-memory (pull request #69)

Use SpooledTemporaryFile for S3BotoFile

Comments (0)

Files changed (1)

storages/backends/s3boto.py

 import mimetypes
 from gzip import GzipFile
 import datetime
+from tempfile import SpooledTemporaryFile
 
 try:
     from cStringIO import StringIO
 
     def _get_file(self):
         if self._file is None:
-            self._file = StringIO()
+            self._file = SpooledTemporaryFile(
+                max_size=self._storage.max_memory_size,
+                suffix=".S3BotoStorageFile",
+                dir=setting("FILE_UPLOAD_TEMP_DIR", None)
+            )
             if 'r' in self._mode:
                 self._is_dirty = False
                 self.key.get_contents_to_file(self._file)
     use_ssl = setting('AWS_S3_USE_SSL', True)
     port = setting('AWS_S3_PORT', None)
 
+    # The max amount of memory a returned file can take up before being
+    # rolled over into a temporary file on disk. Default is 0: Do not roll over.
+    max_memory_size = setting('AWS_S3_MAX_MEMORY_SIZE', 0)
+
     def __init__(self, acl=None, bucket=None, **settings):
         # check if some of the settings we've provided as class attributes
         # need to be overwritten with values passed in here