Skip to content

Commit

Permalink
implement Storage.listdir
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasf committed Oct 17, 2019
1 parent 5084b95 commit 9300d3d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
22 changes: 17 additions & 5 deletions minio_storage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,27 @@ def exists(self, name):
except Exception as error:
logger.error(error)

def listdir(self, prefix):
def listdir(self, path):
if path in [None, "", "."]:

This comment has been minimized.

Copy link
@thomasf

thomasf Oct 17, 2019

Author Collaborator

Some other packages supported None as an argument.. I'm not sure if that is a good idea but let's go with the flow

path = ""
else:
path += "/"

dirs = []
files = []
try:
# TODO: break the path
objects = self.client.list_objects(self.bucket_name, prefix)
return objects
objects = self.client.list_objects_v2(self.bucket_name, prefix=path)
for o in objects:
p = posixpath.relpath(o.object_name, path)
if o.is_dir:
dirs.append(p)
else:
files.append(p)
return dirs, files
except merr.NoSuchBucket:
raise
except merr.ResponseError as error:
raise minio_error("Could not list directory {}".format(prefix), error)
raise minio_error("Could not list directory {}".format(path), error)

def size(self, name):
# type: (str) -> int
Expand Down
50 changes: 42 additions & 8 deletions tests/test_app/tests/retrieve_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,48 @@ def test_modified_time_of_non_existent_throws(self):
with self.assertRaises(NoSuchKey):
self.media_storage.modified_time("nonexistent.jpg")

def test_list_dir_base(self):
# Pre-condition
self.assertIsNotNone(self.new_file)

test_dir = self.media_storage.listdir(None)
files = [elem for elem in test_dir]
self.assertIsInstance(files, list)
self.assertGreaterEqual(len(files), 1)
def _listdir_root(self, root):
self.media_storage.save("dir1/file2.txt", ContentFile(b"meh"))
test_dir = self.media_storage.listdir(root)
(dirs, files) = test_dir
self.assertEqual(dirs, ["dir1"])
self.assertEqual(files, sorted([self.new_file, self.second_file]))
self.assertEqual(len(files), 2)
self.assertEqual(len(test_dir), 2)

def test_listdir_emptystr(self):
self._listdir_root("")

def test_listdir_dot(self):
self._listdir_root(".")

def test_listdir_none(self):
self._listdir_root(None)

def test_listdir_slash(self):
self._listdir_root("/")

def _listdir_sub(self, path):
self.media_storage.save("dir/file.txt", ContentFile(b"meh"))
self.media_storage.save("dir/file2.txt", ContentFile(b"meh"))
self.media_storage.save("dir/dir3/file3.txt", ContentFile(b"meh"))
dirs, files = self.media_storage.listdir("dir/")
self.assertEqual((dirs, files), (["dir3"], ["file.txt", "file2.txt"]))

def test_listdir_subdir_slash(self):
self._listdir_sub("dir/")

def test_listdir_subdir_noslash(self):
self._listdir_sub("dir")

def test_list_nonexist(self):
test_dir = self.media_storage.listdir("nonexist")
self.assertEqual(test_dir, ([], []))

def test_list_prefix(self):
self.media_storage.save("dir/file.txt", ContentFile(b"meh"))
test_dir = self.media_storage.listdir("di")
self.assertEqual(test_dir, ([], []))

def test_file_exists(self):
existent = self.media_storage.save("existent.txt", ContentFile(b"meh"))
Expand Down

0 comments on commit 9300d3d

Please sign in to comment.