import boto3 from fastapi import UploadFile from botocore.exceptions import NoCredentialsError from app.config import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, AWS_S3_BUCKET_NAME # s3 creds # s3 client setup s3 = boto3.client( "s3", aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_name=AWS_REGION, ) # upload an image file to our amazon S3 bucket, it returns the public URL of # the uploaded file for storage in respective postgres tables. # robust file storage; it can store pngs, jpgs, pdfs, gifs... def upload_image_to_s3(file: UploadFile, filename, folder="uploads/"): try: file_key = f"{folder}{filename}" s3.upload_fileobj( file.file, AWS_S3_BUCKET_NAME, file_key, ExtraArgs={"ContentType": file.content_type} ) return f"https://{AWS_S3_BUCKET_NAME}.s3.{AWS_REGION}.amazonaws.com/{file_key}" except NoCredentialsError: return {"error": "AWS credentials not found"}