diff options
| -rw-r--r-- | django/__init__.py | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/django/__init__.py b/django/__init__.py index 49317a1..3e57e15 100644 --- a/django/__init__.py +++ b/django/__init__.py | |||
| @@ -1,8 +1,21 @@ | |||
| 1 | import os | ||
| 2 | from StringIO import StringIO | ||
| 1 | from django.conf import settings | 3 | from django.conf import settings |
| 2 | from amazons3 import S3 | 4 | from amazons3 import S3 |
| 3 | 5 | ||
| 4 | from django.core.files.storage import Storage | 6 | from django.core.files.storage import Storage |
| 5 | 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) | ||
| 6 | 19 | ||
| 7 | class S3Error(Exception): | 20 | class S3Error(Exception): |
| 8 | "Misc. S3 Service Error" | 21 | "Misc. S3 Service Error" |
| @@ -39,7 +52,6 @@ class S3Storage(Storage): | |||
| 39 | return True | 52 | return True |
| 40 | 53 | ||
| 41 | def exists(self, filename): | 54 | def exists(self, filename): |
| 42 | import os | ||
| 43 | contents = self.conn.list_bucket(self.options['bucket'], {'prefix': os.path.dirname(filename)}) | 55 | contents = self.conn.list_bucket(self.options['bucket'], {'prefix': os.path.dirname(filename)}) |
| 44 | if filename in [f.key for f in contents.entries]: | 56 | if filename in [f.key for f in contents.entries]: |
| 45 | return True | 57 | return True |
| @@ -47,9 +59,9 @@ class S3Storage(Storage): | |||
| 47 | return False | 59 | return False |
| 48 | 60 | ||
| 49 | def size(self, filename): | 61 | def size(self, filename): |
| 50 | contents = self.conn.list_bucket(self.options['bucket']) | 62 | contents = self.conn.list_bucket(self.options['bucket'], {'prefix': os.path.dirname(filename)} ) |
| 51 | for f in contents.entries: | 63 | for f in contents.entries: |
| 52 | if f.name == filename: | 64 | if f.key == filename: |
| 53 | return f.size | 65 | return f.size |
| 54 | 66 | ||
| 55 | return False | 67 | return False |
| @@ -123,7 +135,15 @@ class S3Storage(Storage): | |||
| 123 | 135 | ||
| 124 | def open(self, filename, mode): | 136 | def open(self, filename, mode): |
| 125 | from urllib import urlopen | 137 | from urllib import urlopen |
| 126 | return urlopen(self.url(filename)) | 138 | # Download data from S3 and save |
| 139 | # into a file wrapper, which allows its | ||
| 140 | # use as normal in FileFields. | ||
| 141 | # | ||
| 142 | # Note: This saves the file data into memory. | ||
| 143 | data = urlopen(self.url(filename)) | ||
| 144 | openfile = S3OpenFile() | ||
| 145 | openfile.write(data.read()) | ||
| 146 | return openfile | ||
| 127 | 147 | ||
| 128 | def get_available_name(self, filename): | 148 | def get_available_name(self, filename): |
| 129 | import os | 149 | import os |