Permalink
Browse files

S3Boto StorageMixin Class provided.

  • Loading branch information...
1 parent 9214dce commit 342281f4c0176d36ea012048d4a5ab31d59420cb @vam vam committed Nov 14, 2011
Showing with 51 additions and 1 deletion.
  1. +50 −0 filebrowser/storage.py
  2. +1 −1 filebrowser/tests/sites.py
View
50 filebrowser/storage.py
@@ -60,3 +60,53 @@ def makedirs(self, name):
def rmtree(self, name):
shutil.rmtree(self.path(name))
+
+class S3BotoStorageMixin(StorageMixin):
+
+ def isfile(self, name):
+ return self.exists(name)
+
+ def isdir(self, name):
+ # That's some inefficient implementation...
+ # If there are some files having 'name' as their prefix, then
+ # the name is considered to be a directory
+ if not name: # Empty name is a directory
+ return True
+
+ if self.isfile(name):
+ return False
+
+ name = self._normalize_name(self._clean_name(name))
+ dirlist = self.bucket.list(self._encode_name(name))
+
+ # Check whether the iterator is empty
+ for item in dirlist:
+ return True
+ return False
+
+ def move(self, old_file_name, new_file_name, allow_overwrite=False):
+
+ if self.exists(new_file_name):
+ if allow_overwrite:
+ self.delete(new_file_name)
+ else:
+ raise "The destination file '%s' exists and allow_overwrite is False" % new_file_name
+
+ old_key_name = self._encode_name(self._normalize_name(self._clean_name(old_file_name)))
+ new_key_name = self._encode_name(self._normalize_name(self._clean_name(new_file_name)))
+
+ k = self.bucket.copy_key(new_key_name, self.bucket.name, old_key_name)
+
+ if not k:
+ raise "Couldn't copy '%s' to '%s'" % (old_file_name, new_file_name)
+
+ self.delete(old_file_name)
+
+ def makedirs(self, name):
+ pass
+
+ def rmtree(self, name):
+ name = self._normalize_name(self._clean_name(name))
+ dirlist = self.bucket.list(self._encode_name(name))
+ for item in dirlist:
+ item.delete()
View
2 filebrowser/tests/sites.py
@@ -216,7 +216,7 @@ def setUp(self):
def tearDown(self):
# Delete a left-over tmp directories, if there's any
- if self.tmpdir:
+ if hasattr(self, 'tmpdir') and self.tmpdir:
print "Removing left-over tmp dir:", self.tmpdir.path
self.site.storage.rmtree(self.tmpdir.path)

0 comments on commit 342281f

Please sign in to comment.