Search code examples
amazon-web-servicesboto3

list one file (any file) from S3


I have an S3 bucket where files get dumped every minute or so. Except for the data in them, all files are identical. For testing some scripts, I want to grab any file from the S3 and run my code. What is the fastest/best way to list just one file from the S3 bucket? It can be any file. I could list all files and grab the first one (like below), but is there a better way?

import boto3
s3 = boto3.resource('s3')
my_bucket = s3.Bucket('my_bucket')

for my_bucket_object in my_bucket.objects.all():
    print(my_bucket_object.key)
    break

Solution

  • You can use:

    import boto3
    s3 = boto3.client('s3')
    obj = s3.list_objects_v2(Bucket="mybucket", MaxKeys=1)
    

    and for older versions of boto3:

    obj = s3.list_objects(Bucket="mybucket", MaxKeys=1)
    

    This is self-explanatory and anyone reading the code will understand that you are only fetching one object.

    From https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/list_objects_v2.html

    MaxKeys (integer) – Sets the maximum number of keys returned in the response. By default, the action returns up to 1,000 key names. The response might contain fewer keys but will never contain more.