Home > database >  AWS S3 - Get folder count inside a particular folder using python's boto3
AWS S3 - Get folder count inside a particular folder using python's boto3

Time:05-26

I am using Python's boto3 library.

In AWS S3, I have a folder. I want to know the number of subfolders present under that folder.

The structure can be like this:

a/x
a/y
a/z

So, I want to know the number of subfolders under 'a'. Here the count is 3.

I am trying the below way of getting the count:

count = 0
response = client.list_objects_v2(Bucket=bucket_name, Prefix=object_name)
    if "Contents" in response:
        for object in response['Contents']:
            count  = 1

Is there any better way of doing this? Maybe a method that returns some information in its metadata.

CodePudding user response:

S3 doesn't have physical folders and the ones we see on the console is just a representation of objects. The folder feature in S3 is just logical and is there for better management of objects only. You can't query them or do any operations on folders. https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-folders.html

There are alternate ways to achieve what you want.

  • Get all the files in the bucket
  • Parse the name and count any trailing / (slashes) in the name and maintain a counter
  • Group the number the way you want

CodePudding user response:

I am able to get the number of subfolders under a folder using Delimiter:

count = 0

response = client.list_objects_v2(Bucket=bucket_name,
                                      Prefix=object_name,
                                      Delimiter='/')
    if "CommonPrefixes" in response:
        count = len(response['CommonPrefixes'])

Considering the example given in question the object_name is 'a/'. The CommonPrefixes contains the list of key names of all the objects that match the Delimiter

  • Related