import os import json import boto3 import psycopg2 from psycopg2.extras import RealDictCursor import logging log = logging.getLogger() log.setLevel(logging.INFO) REGION = os.environ["AWS_REGION"] if "AWS_REGION" in os.environ else "eu-west-1" S3_BUCKET_DEVEL = "frontend-api-devel-filestore" def get_secret(secret_name): # Create a Secrets Manager client session = boto3.session.Session() client = session.client( service_name='secretsmanager', region_name=REGION ) get_secret_value_response = json.loads(client.get_secret_value(SecretId=secret_name)['SecretString']) return get_secret_value_response DB_CONN = {'fansifter': None, 'sandbox': None} def get_rds_connection_fansifter(): global DB_CONN if DB_CONN['fansifter'] is None: try: secret = get_secret('fansifter-rds') DB_CONN['fansifter'] = psycopg2.connect(host=secret['host'], port=secret['port'], database=secret['dbname'], user=secret['username'], password=secret['password'], cursor_factory=RealDictCursor) except Exception as e: log.exception(e) return DB_CONN['fansifter'] def get_rds_connection_sandbox(): global DB_CONN if DB_CONN['sandbox'] is None: try: secret = get_secret('sandbox_pass') DB_CONN['sandbox'] = psycopg2.connect(host='localhost', # secret['host'], port='5431', # secret['port'], database=secret['dbname'], user=secret['username'], password=secret['password'], cursor_factory=RealDictCursor) except Exception as e: log.exception(e) return DB_CONN['sandbox'] rds_connectors = {'fansifter': get_rds_connection_fansifter, 'sandbox': get_rds_connection_sandbox} def rds_query(connection_name, query, vars=None): with rds_connectors[connection_name]() as conn: with conn.cursor() as cur: cur.execute(query, vars=vars) return cur.fetchall() bucket_name = S3_BUCKET_DEVEL all_sources = [] connection = 'fansifter' other_buckets = set() all_chemas = rds_query(connection, "select table_schema, table_name from information_schema.tables where table_name ='collection'") print(f"Found {len(all_chemas)} schemas in database {connection}") for schema, table in [sch.values() for sch in all_chemas]: sources = rds_query(connection, f"select distinct source from {schema}.{table}") for source in [src['source'] for src in sources]: sp = source.split('/') if sp[0] == bucket_name: all_sources.append('/'.join(sp[1:])) else: other_buckets.add(sp[0]) print(f"Collected {len(all_sources)} sources for {bucket_name}") s3 = boto3.resource('s3') all_buckets = [b.name for b in s3.buckets.iterator()] other_buckets = [buck for buck in other_buckets if buck in all_buckets] print(f'Found {len(other_buckets)} other buckets: {other_buckets}') bucket = s3.Bucket(bucket_name) all_s3_objects = [obj.key for obj in bucket.objects.iterator()] print(f"Found {len(all_s3_objects)} objects in {bucket_name}") trash_objects = [obj for obj in all_s3_objects if obj not in all_sources] print(f'Found {len(trash_objects)} trash') refined_trash = [] filtered_trash = [obj for obj in trash_objects if obj.find('json') == -1] print(f'Found {len(filtered_trash)} filtered trash') for trash in filtered_trash: for schema, table in [sch.values() for sch in all_chemas]: if schema[1:] in trash: refined_trash.append(trash) break else: if len(trash.split('/')) == 2: refined_trash.append(trash) print(f'Found {len(refined_trash)} refined_trash') safe_to_delete = ['c2c39528d40007daacf7b0633f0eaf8307c64b6f0c2021bad30a7a21a', 'ce68cc4221118e031936848539c2d93e8fd5c8a068a319bf1741c0c7b', 'ca7a460d855e229dd0384001e1c9f5d6fb59381f967ba1b7ec36a7591'] for obj in refined_trash: # print(obj) # print(repr(refined_trash)) for safe in safe_to_delete: if safe in obj or obj.startswith('c'): res = bucket.Object(obj).delete() print(f"{res['ResponseMetadata']['HTTPStatusCode']} : {obj}") break else: if '/uploads/' in obj: print(f"? : {obj}") # elif len(obj.split('/')) == 2: # res = bucket.Object(obj).delete() # print(f"{res['ResponseMetadata']['HTTPStatusCode']} : {obj}")