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)
Related videos on Youtube
Comments
-
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 over 5 yearsIt's the
max()
function which might be causing the issue. What do you want to achieve by using max on adatetime
object. -
sam over 5 yearsi 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 over 5 yearsuse max(my_bucket.objects.all(), key=lambda x: x.last_modified)) or so
-
sam over 5 yearsin the directory there are more than 30k .csv files. i just want the last modified filename. @AnkitJaiswal
-
Ankit Jaiswal over 5 yearsIf 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 over 5 yearsok 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 over 5 yearsI know it's not python way. To make it short, then use lambda function in Python
-
sam over 5 yearsi am getting this
TypeError: an integer is required (got type datetime.datetime)
-
sam over 5 yearsyes 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()