1

I want to only print the top level folders in my s3 bucket, how can i go about doing that?

client = boto3.client('s3', region_name='us-west-2')
paginator = client.get_paginator('list_objects')
page_iterator = paginator.paginate(Bucket='my-bucket')

for page in page_iterator:
    print(page['Contents'])

Here is what i have above,

but let say my folder structure is as so

folder1/folder/file
folder2/folder/file
folder2/folder/file2
folder3/file

How can i make it so only the top level folder name prints? So it looks like this below:

folder1
folder2
folder2
folder3

I am also using paginate because i have thousands. otherwise doing a list_objects will only print 1000.

Thanks!

soniccool
  • 5,790
  • 22
  • 60
  • 98
  • To iterate over 1000, use StartAfter (on subsequent calls). – Andy Hayden Apr 13 '18 at 04:09
  • There are no folders in an S3 bucket, just a big flat list of objects. You can put slashes in the names of those objects, but that doesn't create folders. S3 _does_ have `prefix` and `delimiter` arguments for convenience, that let you sort of fake having folders, but that only goes so far—you can't "walk the tree" hierarchically, because there is no tree, just a flat list. – abarnert Apr 13 '18 at 04:09
  • I figured it out `import boto3 s3 = boto3.resource('s3') bucket = s3.Bucket('edsu-test-bucket') result = bucket.meta.client.list_objects(Bucket=bucket.name, Delimiter='/') for o in result.get('CommonPrefixes'): print(o.get('Prefix'))` – soniccool Apr 13 '18 at 04:12

0 Answers0