|
import os |
|
from supabase import create_client, Client |
|
|
|
|
|
SUPABASE_URL: str = os.getenv("SUPABASE_URL") |
|
SUPABASE_KEY: str = os.getenv("SUPABASE_KEY") |
|
|
|
|
|
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) |
|
|
|
|
|
BUCKET_NAME = "csvcharts" |
|
|
|
async def upload_image_to_supabase(file_path: str, file_name: str) -> str: |
|
""" |
|
Uploads an image to Supabase Storage and returns the public URL. |
|
|
|
:param file_path: Path to the image file on your local machine. |
|
:param file_name: Name to save the file as in Supabase Storage. |
|
:return: Public URL of the uploaded image. |
|
""" |
|
|
|
if not os.path.exists(file_path): |
|
raise FileNotFoundError(f"The file {file_path} does not exist.") |
|
|
|
|
|
with open(file_path, "rb") as f: |
|
file_data = f.read() |
|
|
|
|
|
try: |
|
res = supabase.storage.from_(BUCKET_NAME).upload(file_name, file_data) |
|
|
|
except Exception as e: |
|
print(f"Error uploading file: {e}") |
|
raise Exception(f"Failed to upload file: {e}") |
|
|
|
|
|
public_url = supabase.storage.from_(BUCKET_NAME).get_public_url(file_name) |
|
|
|
|
|
return public_url |
|
|
|
|