from pymongo import MongoClient from datetime import datetime import boto3 import uuid import os from dotenv import load_dotenv load_dotenv() AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID") AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY") AWS_BUCKET_NAME = os.getenv("AWS_BUCKET_NAME") MONGO_URI = os.getenv("MONGO_URI") DB_NAME = os.getenv("DB_NAME") COLLECTION_NAME = os.getenv("COLLECTION_NAME") mongo_client = MongoClient(MONGO_URI) db = mongo_client[DB_NAME] collection = db[COLLECTION_NAME] s3 = boto3.client( 's3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY ) def upload_file(file,filetype): try: # Generate a unique key for the file using UUID uuid_str = str(uuid.uuid4()) file_name = file.name s3_key = f'MoSPI_files/{uuid_str}-{file_name}' # Upload the image to S3 with ContentType for image files s3.upload_fileobj( file, AWS_BUCKET_NAME, s3_key, ExtraArgs={'ContentType': file.type} # Set the MIME type of the uploaded file ) file_size = file.size upload_time = datetime.now() # Extract date and time separately upload_date = upload_time.strftime('%Y-%m-%d') upload_time_only = upload_time.strftime('%H:%M:%S') # Metadata to MongoDB metadata = { 'name': file_name, 'size': file_size, 'type': filetype, 'status': 'unprocessed', 's3_url': f's3://{AWS_BUCKET_NAME}/{s3_key}', 's3_key': s3_key, 'object_url': f'https://{AWS_BUCKET_NAME}.s3.amazonaws.com/{s3_key}', 'date_uploaded': upload_date, 'time_uploaded': upload_time_only, 'accuracy': None } # Insert metadata into MongoDB collection.insert_one(metadata) return metadata except Exception as e: print(f"An error occurred during upload: {e}") return None