How To Retrieve Only The Last_modified Key In S3 With Boto3
Solution 1:
--Update Start--
Probably, it might be better to create the objects in S3 with date prefixes.
{bucket}/yyyy/mm/dd/{object}
Example: myS3bucket/2018/12/29/myfile.txt
With this approach, your query becomes simple to find out if you got any files for that particular day and also the number files list you retrieve becomes short.
prefix="/"+str(today.year)+"/"+str(today.month)+"/"+str(today.day)+"/"
objs = bucket.objects.filter(Prefix=prefix).all()
--Update Complete--
I am not sure you gave full code but there are some indentation issues in above snippet. I just tested below and it works fine and I get correct last_modified
date.
Please make sure you are on correct region as bucket. Also last_modified
is in UTC
timezone so your comparison should consider that.
import boto3
from datetime import date
import botocore
# Get Today's date
today = date.today()
# Get Objects date
s3 = boto3.resource('s3',region_name='us-east-1')
bucket = s3.Bucket('xxxx')
prefix="/"+str(today.year)+"/"+str(today.month)+"/"+str(today.day)+"/"
objs = bucket.objects.filter(Prefix=prefix).all()
defget_object_check_alarm():
try:
for obj in objs:
print(obj)
lastobjectdate = (obj.last_modified).date()
except botocore.exceptions.ClientError as e:
error_code = e.response['Error']['Code']
if error_code == '404':
print("There is no file")
# Compare with defined dateif today == lastobjectdate:
print(today)
print(lastobjectdate)
print("OK, lastest file comes from today")
else:
print(today)
print(lastobjectdate)
print("Mail sent")
get_object_check_alarm()
Below is the the output. I am in EST zone so date is still 12/28 but object creation date came in as 12/29 since its already 12/29 in UTC zone when object was created.
s3.ObjectSummary(bucket_name='xxxx', key='yyyy/')
2018-12-28
2018-12-29
Mail sent
Post a Comment for "How To Retrieve Only The Last_modified Key In S3 With Boto3"