Boto3 S3: Get files without getting folders

后端 未结 4 800
别那么骄傲
别那么骄傲 2020-12-15 12:54

Using boto3, how can I retrieve all files in my S3 bucket without retrieving the folders?

Consider the following file structure:

file_1.txt
folder_1/         


        
4条回答
  •  半阙折子戏
    2020-12-15 13:35

    As stated in the other answers, s3 does not actually have directories trees. But there is a convenient workaround taking advantage of the fact that the s3 "folders" have zero size by using paginators. This code-snippet will print out the desired output if all your files in the bucket have size > 0 (of course you need to adapt your region) :

    bucket_name = "bucketname"
    s3 = boto3.client('s3', region_name='eu-central-1')
    paginator = s3.get_paginator('list_objects')
    [print(page['Key']) for page in paginator.paginate(Bucket=bucket_name).search("Contents[?Size > `0`][]")]
    

    The filtering is done using JMESPath.

    Note: Of course this would also exclude files with size 0, but usually you don't need storage for empty files.

提交回复
热议问题