diff options
| -rw-r--r-- | django/__init__.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/django/__init__.py b/django/__init__.py index 7386594..2ae3040 100644 --- a/django/__init__.py +++ b/django/__init__.py | |||
| @@ -1,9 +1,21 @@ | |||
| 1 | import os | 1 | import os |
| 2 | from StringIO import StringIO | ||
| 2 | from django.conf import settings | 3 | from django.conf import settings |
| 3 | from amazons3 import S3 | 4 | from amazons3 import S3 |
| 4 | 5 | ||
| 5 | from django.core.files.storage import Storage | 6 | from django.core.files.storage import Storage |
| 6 | 7 | ||
| 8 | class S3OpenFile(StringIO): | ||
| 9 | """ | ||
| 10 | Wrapper for StringIO which allows open() to be called on it. | ||
| 11 | |||
| 12 | This is for FileField form fields, which expect to be able to call open() | ||
| 13 | and then retrieve data from the file. | ||
| 14 | ** NOTE: The behavior of calling open() and then writing to the file is | ||
| 15 | currently unknown. ** | ||
| 16 | """ | ||
| 17 | def open(self, *args, **kwargs): | ||
| 18 | self.seek(0) | ||
| 7 | 19 | ||
| 8 | class S3Error(Exception): | 20 | class S3Error(Exception): |
| 9 | "Misc. S3 Service Error" | 21 | "Misc. S3 Service Error" |
| @@ -119,7 +131,15 @@ class S3Storage(Storage): | |||
| 119 | 131 | ||
| 120 | def open(self, filename, mode): | 132 | def open(self, filename, mode): |
| 121 | from urllib import urlopen | 133 | from urllib import urlopen |
| 122 | return urlopen(self.url(filename)) | 134 | # Download data from S3 and save |
| 135 | # into a file wrapper, which allows its | ||
| 136 | # use as normal in FileFields. | ||
| 137 | # | ||
| 138 | # Note: This saves the file data into memory. | ||
| 139 | data = urlopen(self.url(filename)) | ||
| 140 | openfile = S3OpenFile() | ||
| 141 | openfile.write(data.read()) | ||
| 142 | return openfile | ||
| 123 | 143 | ||
| 124 | def get_available_name(self, filename): | 144 | def get_available_name(self, filename): |
| 125 | import os | 145 | import os |