title: Check S3 bucket for new files in last two hours
body: <p>I need to create a monitoring tool, that checks buckets (with 1000+ files each) for new objects, created in last two hours, and if the objects were not created, sends a message.
  My first idea was to create a lambda function, that runs every 20 minutes. So I've created python3 + boto3 code:</p>
  
  <pre><code>import boto3
  from datetime import datetime,timedelta
  import pytz
  import sys
  
  s3 = boto3.resource('s3')
  sns = boto3.client('sns')
  
  buckets = ['bucket1', 'bucket2', 'bucket3']
  check_fail = []
  
  def check_bucket(event, context):
      time_now_UTC = datetime.utcnow().replace(tzinfo=pytz.UTC)
      delta_hours = time_now_UTC - timedelta(hours=2)
  
      for bucket_name in buckets:
          bucket = s3.Bucket(bucket_name)
          for key in bucket.objects.all():
              if key.last_modified &gt;= delta_hours:
                  print("There are new files in the bucket %s" %bucket)
                  break
          else:
              check_fail.append(bucket)
  
      if len(check_fail) &gt;= 1:    
          sns.publish(
          TopicArn='arn:aws:sns:us-east-1:xxxxxxxxxxxxxx:xxxxxx',
          Message="The following buckets didn't receive new files for longer than 2 hours: %s" %check_fail,
          Subject='AWS Notification Message' )
      else: 
          print("All buckets have new files")
  </code></pre>
  
  <p>This approach is not working, due to the high number of objects inside every bucket. Checking by "key.last_modified" is taking too long.</p>
  
  <p>Does anyone have an idea on how I can achieve this?</p>
  
  <p>Thank you!</p>