Files
AI-upflux-docprocessor/code/services/result_store.py
2026-03-12 15:02:26 -03:00

30 lines
843 B
Python

import asyncio
import json
from datetime import datetime
import boto3
from utils.config import AWS_REGION, OUTPUT_BUCKET, API_VERSION
_s3_output = boto3.client("s3", region_name=AWS_REGION)
async def save_results(results: list[dict]) -> None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
async def _save_one(guia_result: dict):
numero_guia = guia_result.get("guia", {}).get("codigoGuiaLocal", "unknown")
key = f"{API_VERSION}/{numero_guia}_{timestamp}.json"
await asyncio.to_thread(
_s3_output.put_object,
Bucket=OUTPUT_BUCKET,
Key=key,
Body=json.dumps(guia_result, ensure_ascii=False),
ContentType="application/json",
)
try:
await asyncio.gather(*[_save_one(g) for g in results])
except Exception:
pass