Saqib
Saqib

Reputation: 2480

Fetch list of first level directories in AWS S3 bucket

In my S3 buckets there are many directories at first level. Each containing data of it's own.

- Bucket
   -- dir_1
       -- file1
        -- file2
   -- dir_2
       -- somefile

I want to fetch total size (including all the subobjects under it) of each first level object (dir_1, dir_2) in the bucket. I tried both ListObjects and ListObjectsV2 with different Prefix parameter values but either it's giving no data or all the data including object within objects

Upvotes: 0

Views: 1457

Answers (1)

Marcin
Marcin

Reputation: 238837

Sadly, there is no API call that can give you size of your folders/prefixes.

Normally for that you have too iterate over all the objects yourself, get their data and calculate the size.

Since this is a common request, there are many recipes already made for that. For example, here or here.

The alternative is to use Amazon S3 inventory. The inventory would produce a csv file daily with the metadata of your objects. Then you could parse it to calculate the sizes based on the prefixes.

Upvotes: 2

Related Questions