Spaces:
Sleeping
Sleeping
File size: 2,098 Bytes
eef9e83 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
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
|