4/13/2023

Count object(file) amount in certain s3 folder. python and cli example

 refer to code.

.

import boto3

def count_files_in_bucket(bucket_name, prefix=''):
s3 = boto3.client('s3')
paginator = s3.get_paginator('list_objects_v2')
file_count = 0

for page in paginator.paginate(Bucket=bucket_name, Prefix=prefix):
for obj in page['Contents']:
if not obj['Key'].endswith('/'):
file_count += 1
return file_count


bucket_name = 'your_bucket_name'
prefix = 'sub_folder_name'
file_count = count_files_in_bucket(bucket_name, prefix)
print(f'There are {file_count} files in the "{prefix}" folder of the "{bucket_name}" bucket.')

..


๐Ÿ™‡๐Ÿป‍♂️

Thank you.

www.marearts.com



No comments:

Post a Comment