How can I get only the latest file/files created/modified on S3 location through python
Question: using boto i tried the below code :

    
    
    from boto.s3.connection import S3Connection
    conn = S3Connection('XXX', 'YYYY')
    
    bucket = conn.get_bucket('myBucket')
    
    file_list = bucket.list('just/a/prefix/')
    

but am unable to get the length of the list or the last element of the
file_list as it is a BucketListResultSet type ,please suggest a solution for
this scenario


Answer: You are trying to use `boto` library, which is rather obsolete and not
maintained. The number of issues with this library is growing.

Better use currently developed `boto3`.

First, let us define parameters of our search:

    
    
    >>> bucket_name = "bucket_of_m"
    >>> prefix = "region/cz/"
    

Do import `boto3` and create s3 representing S3 resource:

    
    
    >>> import boto3
    >>> s3 = boto3.resource("s3")
    

Get the bucket:

    
    
    >>> bucket = s3.Bucket(name=bucket_name)
    >>> bucket
    s3.Bucket(name='bucket_of_m')
    

Define filter for objects with given prefix:

    
    
    >>> res = bucket.objects.filter(Prefix=prefix)
    >>> res
    s3.Bucket.objectsCollection(s3.Bucket(name='bucket_of_m'), s3.ObjectSummary)
    

and iterate over it:

    
    
    >>> for obj in res:
    ...     print obj.key
    ...     print obj.size
    ...     print obj.last_modified
    ...
    

Each `obj` is ObjectSummary (not Object itself), but it holds enought to learn
something about it

    
    
    >>> obj
    s3.ObjectSummary(bucket_name='bucket_of_m', key=u'region/cz/Ostrava/Nadrazni.txt')
    >>> type(obj)
    boto3.resources.factory.s3.ObjectSummary
    

You can get Object from it and use it as you need:

    
    
    >>> o = obj.Object()
    >>> o
    s3.Object(bucket_name='bucket_of_m', key=u'region/cz/rodos/fusion/AdvancedDataFusion.xml')
    

There are not so many options for filtering, but prefix is available.

