Boto3 S3: Get files without getting folders

后端 未结 4 796
别那么骄傲
别那么骄傲 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:59

    There are no folders in S3. What you have is four files named:

    file_1.txt
    folder_1/file_2.txt
    folder_1/file_3.txt
    folder_1/folder_2/folder_3/file_4.txt
    

    Those are the actual names of the objects in S3. If what you want is to end up with:

    file_1.txt
    file_2.txt
    file_3.txt
    file_4.txt
    

    all sitting in the same directory on a local file system you would need to manipulate the name of the object to strip out just the file name. Something like this would work:

    import os.path
    
    full_name = 'folder_1/folder_2/folder_3/file_4.txt'
    file_name = os.path.basename(full_name)
    

    The variable file_name would then contain 'file_4.txt'.

提交回复
热议问题