Commits

Gert Van Gool committed f8d31c0

Use a StringIO object internally (so we have self.file again)

Comments (0)

Files changed (1)

backends/s3boto.py

 import os
+try:
+    from cStringIO import StringIO
+except ImportError:
+    from StringIO import StringIO
 
 from django.conf import settings
 from django.core.files.base import File
 class S3BotoStorageFile(File):
     def __init__(self, name, mode, storage):
         self._storage = storage
-        self._name = name
+        self.name = name
         self._mode = mode
         self.key = storage.bucket.get_key(name)
+        self._is_dirty = False
+        self.file = StringIO()
 
+    @property
     def size(self):
         return self.key.size
 
     def read(self, *args, **kwargs):
-        return self.key.read(*args, **kwargs)
+        self.file = StringIO()
+        self._is_dirty = False
+        self.key.get_contents_to_file(self.file)
+        return self.file.getvalue()
 
     def write(self, content):
-        self.key.set_contents_from_string(content, headers=self._storage.headers, acl=self._storage.acl)
+        if 'w' not in self._mode:
+            raise AttributeError("File was opened for read-only access.")
+        self.file = StringIO(content)
+        self._is_dirty = True
 
     def close(self):
+        if self._is_dirty:
+            self.key.set_contents_from_string(self.file.getvalue(), headers=self._storage.headers, acl=self._storage.acl)
         self.key.close()