diff --git a/backend/app/core/config.py b/backend/app/core/config.py index 1c370ac..fe9082c 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -1,9 +1,13 @@ +import os # Variables de conexión S3/MinIO -MINIO_ENDPOINT = os.getenv('MINIO_ENDPOINT', 'http://localhost:9000') +MINIO_HOST = os.getenv('MINIO_HOST', 'localhost') +MINIO_SECURE = os.getenv('MINIO_SECURE', 'false').lower() == 'true' +MINIO_PORT = int(os.getenv('MINIO_PORT', '9000')) MINIO_ACCESS_KEY = os.getenv('MINIO_ACCESS_KEY', 'minioadmin') MINIO_SECRET_KEY = os.getenv('MINIO_SECRET_KEY', 'minioadmin') MINIO_IMAGE_BUCKET = os.getenv('MINIO_IMAGE_BUCKET', 'images') MINIO_PDF_BUCKET = os.getenv('MINIO_PDF_BUCKET', 'pdfs') +MINIO_ENDPOINT = f"{'https' if MINIO_SECURE else 'http'}://{MINIO_HOST}:{MINIO_PORT}" from pydantic_settings import BaseSettings class Settings(BaseSettings): diff --git a/backend/app/main.py b/backend/app/main.py index cd26c0d..b6b5466 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -8,12 +8,13 @@ import os import boto3 from botocore.client import Config import uuid +from app.core import config as app_config # S3/MinIO configuration -S3_ENDPOINT = os.getenv('MINIO_ENDPOINT', 'http://localhost:9000') -S3_ACCESS_KEY = os.getenv('MINIO_ACCESS_KEY', 'minioadmin') -S3_SECRET_KEY = os.getenv('MINIO_SECRET_KEY', 'minioadmin') -S3_IMAGE_BUCKET = os.getenv('MINIO_IMAGE_BUCKET', 'images') -S3_PDF_BUCKET = os.getenv('MINIO_PDF_BUCKET', 'pdfs') +S3_ENDPOINT = app_config.MINIO_ENDPOINT +S3_ACCESS_KEY = app_config.MINIO_ACCESS_KEY +S3_SECRET_KEY = app_config.MINIO_SECRET_KEY +S3_IMAGE_BUCKET = app_config.MINIO_IMAGE_BUCKET +S3_PDF_BUCKET = app_config.MINIO_PDF_BUCKET s3_client = boto3.client( 's3', @@ -1696,14 +1697,17 @@ def export_inspection_to_pdf( # ...existing code for PDF generation... doc.build(elements) buffer.seek(0) + # Guardar localmente para depuración + with open(f"/tmp/test_inspeccion_{inspection_id}.pdf", "wb") as f: + f.write(buffer.getvalue()) now = datetime.now() folder = f"{now.year}/{now.month:02d}" filename = f"inspeccion_{inspection_id}_{inspection.vehicle_plate or 'sin-patente'}.pdf" s3_key = f"{folder}/{filename}" # Subir PDF a S3/MinIO + buffer.seek(0) # Asegura que el puntero esté al inicio s3_client.upload_fileobj(buffer, S3_PDF_BUCKET, s3_key, ExtraArgs={"ContentType": "application/pdf"}) pdf_url = f"{S3_ENDPOINT}/{S3_PDF_BUCKET}/{s3_key}" - # Guardar pdf_url en la inspección inspection.pdf_url = pdf_url db.commit() return {"pdf_url": pdf_url} diff --git a/backend/requirements.txt b/backend/requirements.txt index 2347e38..5f30879 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -15,3 +15,4 @@ google-generativeai==0.3.2 Pillow==10.2.0 reportlab==4.0.9 python-dotenv==1.0.0 +boto3==1.34.89 \ No newline at end of file diff --git a/backend/s3test.py b/backend/s3test.py new file mode 100644 index 0000000..606be5a --- /dev/null +++ b/backend/s3test.py @@ -0,0 +1,52 @@ +import boto3 +from botocore.client import Config +from botocore.exceptions import ClientError + +MINIO_ENDPOINT = "minioapi.rshtech.com.py" +MINIO_ACCESS_KEY = "6uEIJyKR2Fi4UXiSgIeG" +MINIO_SECRET_KEY = "8k0kYuvxD9ePuvjdxvDk8WkGhhlaaee8BxU1mqRW" +MINIO_IMAGE_BUCKET = "images" +MINIO_PDF_BUCKET = "pdfs" +MINIO_SECURE = True # HTTPS +MINIO_PORT = 443 + + +def main(): + try: + endpoint_url = f"https://{MINIO_ENDPOINT}:{MINIO_PORT}" if MINIO_SECURE \ + else f"http://{MINIO_ENDPOINT}:{MINIO_PORT}" + + # Crear cliente S3 compatible para MinIO + s3 = boto3.client( + "s3", + endpoint_url=endpoint_url, + aws_access_key_id=MINIO_ACCESS_KEY, + aws_secret_access_key=MINIO_SECRET_KEY, + config=Config(signature_version="s3v4"), + region_name="us-east-1" + ) + + print("🔍 Probando conexión…") + + # Listar buckets + response = s3.list_buckets() + print("✅ Conexión exitosa. Buckets disponibles:") + for bucket in response.get("Buckets", []): + print(f" - {bucket['Name']}") + + # Verificar acceso a buckets específicos + for bucket_name in [MINIO_IMAGE_BUCKET, MINIO_PDF_BUCKET]: + try: + s3.head_bucket(Bucket=bucket_name) + print(f"✔ Acceso OK al bucket: {bucket_name}") + except ClientError: + print(f"❌ No se pudo acceder al bucket: {bucket_name}") + + print("🎉 Test finalizado correctamente.") + + except Exception as e: + print("❌ Error:", e) + + +if __name__ == "__main__": + main() diff --git a/backend/test_minio.py b/backend/test_minio.py new file mode 100644 index 0000000..93ff372 --- /dev/null +++ b/backend/test_minio.py @@ -0,0 +1,30 @@ +import os +from app.core import config as app_config +import boto3 +from botocore.client import Config + +scheme = 'https' if app_config.MINIO_SECURE else 'http' +endpoint = f"{scheme}://{os.getenv('MINIO_ENDPOINT', 'localhost')}:{app_config.MINIO_PORT}" +access_key = os.getenv('MINIO_ACCESS_KEY', 'minioadmin') +secret_key = os.getenv('MINIO_SECRET_KEY', 'minioadmin') +bucket = os.getenv('MINIO_IMAGE_BUCKET', 'images') + +s3 = boto3.client( + 's3', + endpoint_url=endpoint, + aws_access_key_id=access_key, + aws_secret_access_key=secret_key, + config=Config(signature_version='s3v4'), + region_name='us-east-1' +) + +try: + # List buckets + response = s3.list_buckets() + print('Buckets:', [b['Name'] for b in response['Buckets']]) + # Upload test file + with open('test_minio.py', 'rb') as f: + s3.upload_fileobj(f, bucket, 'test_minio.py') + print(f'Archivo subido a bucket {bucket} correctamente.') +except Exception as e: + print('Error:', e)