How can i get list of only folders in amazon S3 using python boto

user1958218 picture user1958218 · Jun 29, 2013 · Viewed 53.3k times · Source

I am using boto and python and amazon s3.

If i use

[key.name for key in list(self.bucket.list())]

then i get all the keys of all the files.

mybucket/files/pdf/abc.pdf
mybucket/files/pdf/abc2.pdf
mybucket/files/pdf/abc3.pdf
mybucket/files/pdf/abc4.pdf
mybucket/files/pdf/new/
mybucket/files/pdf/new/abc.pdf
mybucket/files/pdf/2011/

what is the best way to

1. either get all folders from s3
2. or from that list just remove the file from the last and get the unique keys of folders

I am thinking of doing like this

set([re.sub("/[^/]*$","/",path) for path in mylist]

Answer

j1m picture j1m · Oct 17, 2013

building on sethwm's answer:

To get the top level directories:

list(bucket.list("", "/"))

To get the subdirectories of files:

list(bucket.list("files/", "/")

and so on.