how to get last modified filename using boto3 from s3

10,972

There you have a simple snippet. In short you have to iterate over files to find the last modified date in all files. Then you have print files with this date (might be more than one).

from datetime import datetime

import boto3

s3 = boto3.resource('s3',aws_access_key_id='demo', aws_secret_access_key='demo')

my_bucket = s3.Bucket('demo')

last_modified_date = datetime(1939, 9, 1).replace(tzinfo=None)
for file in my_bucket.objects.all():
    file_date = file.last_modified.replace(tzinfo=None)
    if last_modified_date < file_date:
        last_modified_date = file_date

print(last_modified_date)

# you can have more than one file with this date, so you must iterate again
for file in my_bucket.objects.all():
    if file.last_modified.replace(tzinfo=None) == last_modified_date:
        print(file.key)
        print(last_modified_date)
Share:
10,972

Related videos on Youtube

sam
Author by

sam

lets learn new things.

Updated on June 04, 2022

Comments

  • sam
    sam almost 2 years

    i want to get the last modified file from a directory from amazon s3. i had tried to print just that file date only now, but i am getting this error.

    TypeError: 'datetime.datetime' object is not iterable

    import boto3
    s3 = boto3.resource('s3',aws_access_key_id='demo', aws_secret_access_key='demo')
    
    my_bucket = s3.Bucket('demo')
    
    for file in my_bucket.objects.all():
        # print(file.key)
        print(max(file.last_modified))
    
    • Ankit Jaiswal
      Ankit Jaiswal over 5 years
      It's the max() function which might be causing the issue. What do you want to achieve by using max on a datetime object.
    • sam
      sam over 5 years
      i am new to boto3 and also for the s3 i want to get the last modified file. i just tried that max() function, can you suggest me any other way how to get that file.
    • kofemann
      kofemann over 5 years
      use max(my_bucket.objects.all(), key=lambda x: x.last_modified)) or so
    • sam
      sam over 5 years
      in the directory there are more than 30k .csv files. i just want the last modified filename. @AnkitJaiswal
    • Ankit Jaiswal
      Ankit Jaiswal over 5 years
      If there is no way to sort by last_modified field in boto library, you'll need to iterate and find out the file with max last_modified value, similar to what @kofemann has suggested.
    • sam
      sam over 5 years
      ok i got this output s3.ObjectSummary(bucket_name='demo', key='ETFHoldingData/kraneshares/KCCB-KCCB-10-23-2018.xls') , how can i print this key(filename) only.
  • bolec_kolec
    bolec_kolec over 5 years
    I know it's not python way. To make it short, then use lambda function in Python
  • sam
    sam over 5 years
    i am getting this TypeError: an integer is required (got type datetime.datetime)
  • sam
    sam over 5 years
    yes with the lambda i got the result but just to try i had used your code. thank for the help man can you tell me how to resolve this i hade change vale of last_modfied_date to datetime.datetime.now()