This commit is contained in:
Kenneth Kehl
2024-09-06 11:13:13 -07:00
parent 671cad66b5
commit fefdd297ea
3 changed files with 37 additions and 0 deletions

View File

@@ -82,6 +82,33 @@ def list_s3_objects():
)
def cleanup_old_s3_objects():
bucket_name = current_app.config["CSV_UPLOAD_BUCKET"]["bucket"]
s3_client = get_s3_client()
# Our reports only support 7 days, but can be scheduled 3 days in advance
# Use 14 day for the v1.0 version of this behavior
time_limit = aware_utcnow() - datetime.timedelta(days=14)
try:
response = s3_client.list_objects_v2(Bucket=bucket_name)
while True:
for obj in response.get("Contents", []):
if obj["LastModified"] >= time_limit:
print(f"{obj['LastModified']} {obj['Key']}")
if "NextContinuationToken" in response:
response = s3_client.list_objects_v2(
Bucket=bucket_name,
ContinuationToken=response["NextContinuationToken"],
)
else:
break
except Exception:
current_app.logger.error(
f"An error occurred while cleaning up old s3 objects #notify-api-1303",
exc_info=True,
)
def get_s3_files():
bucket_name = current_app.config["CSV_UPLOAD_BUCKET"]["bucket"]