forked from mtusi2026/mtusi2026lab4
45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Report or remove old S3 objects that have no PostgreSQL attachment row."""
|
|
import argparse
|
|
import datetime as dt
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
import app
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--grace-seconds", type=int, default=3600)
|
|
parser.add_argument("--apply", action="store_true")
|
|
args = parser.parse_args()
|
|
if args.grace_seconds < 0:
|
|
parser.error("--grace-seconds must be non-negative")
|
|
|
|
with app.db() as conn:
|
|
known = {
|
|
row["object_key"]
|
|
for row in conn.execute("SELECT object_key FROM attachments").fetchall()
|
|
}
|
|
|
|
now = dt.datetime.now(dt.timezone.utc)
|
|
client = app.s3()
|
|
paginator = client.get_paginator("list_objects_v2")
|
|
candidates = []
|
|
for page in paginator.paginate(Bucket=app.S3_BUCKET):
|
|
for item in page.get("Contents", []):
|
|
key = item["Key"]
|
|
modified = item["LastModified"]
|
|
if key not in known and (now - modified).total_seconds() >= args.grace_seconds:
|
|
candidates.append(key)
|
|
|
|
for key in candidates:
|
|
if args.apply:
|
|
client.delete_object(Bucket=app.S3_BUCKET, Key=key)
|
|
print(("deleted" if args.apply else "candidates") + f"={len(candidates)}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|