Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the list of only folders in amazon S3 using python boto?

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] 
like image 732
user1958218 Avatar asked Jun 28 '13 23:06

user1958218


1 Answers

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.

like image 77
j1m Avatar answered Sep 29 '22 17:09

j1m