Skip to content

Instantly share code, notes, and snippets.

@pwilken
Last active May 30, 2023 13:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save pwilken/b548b5899a098b620ebb839ebadae59f to your computer and use it in GitHub Desktop.
Save pwilken/b548b5899a098b620ebb839ebadae59f 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']
@mmorkos
Copy link

mmorkos commented Apr 14, 2022

hmmm.... Line 30.... "date = obj["Key"].replace('prefix_before_time', '')"
Why are we assuming that the key of the object contains the date?

@gek0
Copy link

gek0 commented Jul 13, 2022

hmmm.... Line 30.... "date = obj["Key"].replace('prefix_before_time', '')" Why are we assuming that the key of the object contains the date?

Well it does say that in the script description "AWS Lambda function for deleting files from S3 bucket, filename contains timestamp"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment