File size: 1,879 Bytes
6160c3a
 
 
ca06d0d
 
 
 
 
 
 
6160c3a
cc5650b
6160c3a
ca06d0d
 
 
 
 
cc5650b
ca06d0d
 
cc5650b
ca06d0d
cc5650b
8c876a6
6160c3a
cc5650b
ca06d0d
 
 
cc5650b
 
ca06d0d
 
 
cc5650b
ca06d0d
 
 
6160c3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
962cd6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from shared_data.supabase_client import supabase, BUCKET


import os
import traceback

def upload_file_to_supabase(local_file_path: str):

    filename = os.path.basename(local_file_path)
    remote_path = f"uploads/{filename}"

    try:

        print("=" * 60)
        print("Uploading document...")
        print("LOCAL :", local_file_path)
        print("REMOTE:", remote_path)
        print("=" * 60)

        with open(local_file_path, "rb") as f:
            file_data = f.read()

        response = supabase.storage.from_(BUCKET).upload(
            path=remote_path,
            file=data
        )

        print("UPLOAD RESPONSE:")
        print(response)
        print("UPLOAD SUCCESS")

    except Exception as e:

        print("=" * 60)
        print("UPLOAD FAILED")
        print(traceback.format_exc())
        print("=" * 60)

        raise

def download_file(remote_path, local_path):

    data = supabase.storage.from_(BUCKET).download(
        remote_path
    )

    with open(local_path, "wb") as f:

        f.write(data)


def list_files(folder):

    return supabase.storage.from_(BUCKET).list(
        folder
    )


def delete_file(remote_path):

    supabase.storage.from_(BUCKET).remove(
        [remote_path]
    )


def delete_all(folder):

    files = list_files(folder)

    if len(files) == 0:
        return

    paths = []

    for file in files:

        paths.append(
            folder + "/" + file["name"]
        )

    supabase.storage.from_(BUCKET).remove(
        paths
    )
def upload_file_to_supabase(local_file_path: str):

    filename = os.path.basename(local_file_path)

    remote_path = f"uploads/{filename}"

    with open(local_file_path, "rb") as f:
        supabase.storage.from_(BUCKET).upload(
            remote_path,
            f,
            {"upsert": True}
        )

    print(f"Uploaded {filename} to Supabase")