Skip to content

Instantly share code, notes, and snippets.

@asmodeus70
Forked from pwilken/cleanup_s3_policy.py
Created February 6, 2020 11:38
Show Gist options
  • Save asmodeus70/79a33a4222b4305e9236a020272fd5a3 to your computer and use it in GitHub Desktop.
Save asmodeus70/79a33a4222b4305e9236a020272fd5a3 to your computer and use it in GitHub Desktop.
AWS Lambda function for deleting files from S3 bucket, filename contains timestamp. Clean up for everything older then 7 days. Triggered once a week by a CloudWatch Event.
import logging
import boto3
import datetime
from botocore.exceptions import ClientError
def lambda_handler(event, context):
# Set up logging
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)s: %(asctime)s: %(message)s')
bucket_name = 'bucket_name'
s3 = boto3.client('s3')
objects = list_bucket_objects(bucket_name)
if objects is not None:
# List the object names
logging.info(f'Objects in {bucket_name}')
count = len(objects)
print(f'Backup count: {count}')
last = '0'
for obj in objects:
if count <= 56: # For not deleting from the last 7 days! (Backup every 3 hours per day, 24/3 * 7 = 56)
return True
count = count - 1 # For not deleting from the last 7 days!
print(f'{count}')
# Start Policy for everything older then one week
date = obj["Key"].replace('prefix_before_time', '')
dateArray = date.split("T")
date = dateArray[0]
hour = dateArray[1].split(":")[0]
print(f' {date} - {hour}')
if last == '0':
last = date
print(f'last==0')
elif last == date:
print(f'delete: {obj["Key"]}')
try:
s3.delete_object(Bucket=bucket_name, Key='prefix' + obj["Key"])
except ClientError as e:
logging.error(e)
else:
print(f'last = {obj["Key"]}')
last = date
return True
def list_bucket_objects(bucket_name):
# Retrieve the list of bucket objects
s3 = boto3.client('s3')
try:
response = s3.list_objects_v2(Bucket=bucket_name)
except ClientError as e:
logging.error(e)
return None
return response['Contents']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment