Last active
May 30, 2023 13:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Well it does say that in the script description "AWS Lambda function for deleting files from S3 bucket, filename contains timestamp"