Quick way to list all files in Amazon S3 bucket?

前端 未结 28 1849
星月不相逢
星月不相逢 2020-11-28 01:32

I have an amazon s3 bucket that has tens of thousands of filenames in it. What\'s the easiest way to get a text file that lists all the filenames in the bucket?

28条回答
  •  生来不讨喜
    2020-11-28 02:00

    I know its old topic, but I'd like to contribute too.

    With the newer version of boto3 and python, you can get the files as follow:

    import os
    import boto3
    from botocore.exceptions import ClientError    
    
    client = boto3.client('s3')
    
    bucket = client.list_objects(Bucket=BUCKET_NAME)
    for content in bucket["Contents"]:
        key = content["key"]
    

    Keep in mind that this solution not comprehends pagination.

    For more information: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.list_objects

提交回复
热议问题