I have one bucket in AWS that has files regularly uploaded to it. There is a policy that this bucket cannot have a lifecycle rules attached.
I'm looking for a lambda that will remove objects older than 2 weeks. I know the timedelta library can be used for comparing dates, but I can't figure out how I can use this to check if an object is over 2 weeks old (I'm new to python).
So far I have:
import boto3
import datetime
s3 = boto3.resource('s3')
now = datetime.datetime.now()
now_format = int(now.strftime("%d%m%Y"))
print(f'it is now {now_format}')
# Get bucket object
my_bucket = s3.Bucket('cost-reports')
all_objects = my_bucket.objects.all()
for each_object in all_objects:
obj_int = int(each_object.last_modified.strftime('%d%m%Y'))
print("The object {} was last modified on the {}".format(
each_object.key, obj_int))
so this is just using the strftime comparison, but will this actually work as well? or do I have to use the timedelta module and how would this look?