aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjason2010-11-13 00:58:56 -0700
committerjason2010-11-13 00:58:56 -0700
commitdb95070a37f70a00f80a1a2a9ba732c55f6bee1b (patch)
tree04b739dccd5122b9f161643491c9fe96aec3eb22
parent5e040ecd4902d7c46856419368e2e14a685f068b (diff)
parent3d03ea6a85a3d324087019601bd933fa0319a103 (diff)
downloadamazons3-py-db95070a37f70a00f80a1a2a9ba732c55f6bee1b.tar.gz
amazons3-py-db95070a37f70a00f80a1a2a9ba732c55f6bee1b.zip
Merge branch 'master' of dev.delong.neutroninteractive.com:/git/amazons3
-rw-r--r--django/__init__.py28
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 @@
1import os
2from StringIO import StringIO
1from django.conf import settings 3from django.conf import settings
2from amazons3 import S3 4from amazons3 import S3
3 5
4from django.core.files.storage import Storage 6from django.core.files.storage import Storage
5 7
8class 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
7class S3Error(Exception): 20class 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