Tests de PDFs con Minio los pdfs se guardan una sola vez ahora
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
|
import os
|
||||||
# Variables de conexión S3/MinIO
|
# 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_ACCESS_KEY = os.getenv('MINIO_ACCESS_KEY', 'minioadmin')
|
||||||
MINIO_SECRET_KEY = os.getenv('MINIO_SECRET_KEY', 'minioadmin')
|
MINIO_SECRET_KEY = os.getenv('MINIO_SECRET_KEY', 'minioadmin')
|
||||||
MINIO_IMAGE_BUCKET = os.getenv('MINIO_IMAGE_BUCKET', 'images')
|
MINIO_IMAGE_BUCKET = os.getenv('MINIO_IMAGE_BUCKET', 'images')
|
||||||
MINIO_PDF_BUCKET = os.getenv('MINIO_PDF_BUCKET', 'pdfs')
|
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
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
|
|||||||
@@ -8,12 +8,13 @@ import os
|
|||||||
import boto3
|
import boto3
|
||||||
from botocore.client import Config
|
from botocore.client import Config
|
||||||
import uuid
|
import uuid
|
||||||
|
from app.core import config as app_config
|
||||||
# S3/MinIO configuration
|
# S3/MinIO configuration
|
||||||
S3_ENDPOINT = os.getenv('MINIO_ENDPOINT', 'http://localhost:9000')
|
S3_ENDPOINT = app_config.MINIO_ENDPOINT
|
||||||
S3_ACCESS_KEY = os.getenv('MINIO_ACCESS_KEY', 'minioadmin')
|
S3_ACCESS_KEY = app_config.MINIO_ACCESS_KEY
|
||||||
S3_SECRET_KEY = os.getenv('MINIO_SECRET_KEY', 'minioadmin')
|
S3_SECRET_KEY = app_config.MINIO_SECRET_KEY
|
||||||
S3_IMAGE_BUCKET = os.getenv('MINIO_IMAGE_BUCKET', 'images')
|
S3_IMAGE_BUCKET = app_config.MINIO_IMAGE_BUCKET
|
||||||
S3_PDF_BUCKET = os.getenv('MINIO_PDF_BUCKET', 'pdfs')
|
S3_PDF_BUCKET = app_config.MINIO_PDF_BUCKET
|
||||||
|
|
||||||
s3_client = boto3.client(
|
s3_client = boto3.client(
|
||||||
's3',
|
's3',
|
||||||
@@ -1696,14 +1697,17 @@ def export_inspection_to_pdf(
|
|||||||
# ...existing code for PDF generation...
|
# ...existing code for PDF generation...
|
||||||
doc.build(elements)
|
doc.build(elements)
|
||||||
buffer.seek(0)
|
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()
|
now = datetime.now()
|
||||||
folder = f"{now.year}/{now.month:02d}"
|
folder = f"{now.year}/{now.month:02d}"
|
||||||
filename = f"inspeccion_{inspection_id}_{inspection.vehicle_plate or 'sin-patente'}.pdf"
|
filename = f"inspeccion_{inspection_id}_{inspection.vehicle_plate or 'sin-patente'}.pdf"
|
||||||
s3_key = f"{folder}/{filename}"
|
s3_key = f"{folder}/{filename}"
|
||||||
# Subir PDF a S3/MinIO
|
# 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"})
|
s3_client.upload_fileobj(buffer, S3_PDF_BUCKET, s3_key, ExtraArgs={"ContentType": "application/pdf"})
|
||||||
pdf_url = f"{S3_ENDPOINT}/{S3_PDF_BUCKET}/{s3_key}"
|
pdf_url = f"{S3_ENDPOINT}/{S3_PDF_BUCKET}/{s3_key}"
|
||||||
# Guardar pdf_url en la inspección
|
|
||||||
inspection.pdf_url = pdf_url
|
inspection.pdf_url = pdf_url
|
||||||
db.commit()
|
db.commit()
|
||||||
return {"pdf_url": pdf_url}
|
return {"pdf_url": pdf_url}
|
||||||
|
|||||||
@@ -15,3 +15,4 @@ google-generativeai==0.3.2
|
|||||||
Pillow==10.2.0
|
Pillow==10.2.0
|
||||||
reportlab==4.0.9
|
reportlab==4.0.9
|
||||||
python-dotenv==1.0.0
|
python-dotenv==1.0.0
|
||||||
|
boto3==1.34.89
|
||||||
52
backend/s3test.py
Normal file
52
backend/s3test.py
Normal file
@@ -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()
|
||||||
30
backend/test_minio.py
Normal file
30
backend/test_minio.py
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user