Reputation: 152
I want to get the last modified file from an Amazon S3 directory. I tried to only print that file's date only now, but I am getting the 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))
Upvotes: 3
Views: 16182
Reputation: 107
you will get last modified filename and folder names using boto3 from s3
reference for function tools and reduce - link
from functools import reduce
import boto3
# replace with your access key and secret key
#s3 = boto3.resource('s3', aws_access_key_id='demo', aws_secret_access_key='demo')
import boto3
s3_client = boto3.client('s3', aws_access_key_id='demo', aws_secret_access_key='demo')
response = s3_client.list_objects_v2(Bucket='bucket_name', Prefix='subfolder name')
#Replace with your bucket name and subfolder name
all = response['Contents']
latest = max(all, key=lambda x: x['LastModified'])
print(latest)
#print(latest.values())
list(reduce(lambda x, y: x + y, latest.items()))
a = list(reduce(lambda x, y: x + y, latest.items()))
print(a[1])
output
# if you have one sub folder, you will get output in this below format
subfolder/filename.format
# if you having more than one sub folders, you will get output in this below format
subfolder1/subfolder2/filename.format
you need to get last modified folders and filed name based on your bucket name
Then refer this below changes
#instead of this code
response = s3_client.list_objects_v2(Bucket='bucket_name', Prefix='subfolder name')
#remove prefix and subfolder name
response = s3_client.list_objects_v2(Bucket='bucket_name')
#Replace with you bucket name
Upvotes: 0
Reputation: 510
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)
Upvotes: 6