Skip to content

Commit

Permalink
Fixing existence check for directories. Fixes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
etianen committed Feb 1, 2016
1 parent efcaa6b commit 027dcdf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion django_s3_storage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ def exists(self, name):
Returns True if a file referenced by the given name already exists in the
storage system, or False if the name is available for a new file.
"""
return self._get_key(name).exists()
# We also need to check for directory existence, so we'll list matching
# keys and return success if any match.
for _ in self.bucket.list(prefix=self._get_key_name(name)):

This comment has been minimized.

Copy link
@leonsmith

leonsmith Jul 4, 2016

Contributor

This causes false positives for keys that have the same prefix.

"css/main.css.map" existing on s3 for "css/main.css" would cause this to return true.

Do we need to check for directory existence in this method? the docs state this is just for files?

This comment has been minimized.

Copy link
@etianen

etianen Jul 4, 2016

Author Owner

Yes, directory existence also needs to work. See #16.

However, broken behaviour is not ideal! I suspect adding the "delimiter" argument will fix this problem.

http://boto.cloudhackers.com/en/latest/ref/s3.html#boto.s3.bucket.Bucket.list

return True
return False

def listdir(self, path):
"""
Expand Down
3 changes: 3 additions & 0 deletions django_s3_storage/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def testExists(self):
self.assertTrue(self.storage.exists(self.upload_path))
self.assertFalse(self.storage.exists(self.generateUploadPath()))

def testDirExists(self):
self.assertTrue(self.storage.exists(""))

def testDelete(self):
# Make a new file to delete.
upload_path = self.generateUploadPath()
Expand Down

0 comments on commit 027dcdf

Please sign in to comment.