File size: 1,094 Bytes
0a9d3ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from supabase import create_client, Client
from dotenv import load_dotenv

load_dotenv()
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
BUCKET_NAME = 'legal-docs'
LOCAL_FOLDER = 'downloaded_pdfs'

def download_all_files(LOCAL_FOLDER):
    # Create local folder if it doesn't exist
    os.makedirs(LOCAL_FOLDER, exist_ok=True)

    # Create Supabase client
    supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)

    # List all files in the bucket
    files = supabase.storage.from_(BUCKET_NAME).list()

    for file in files:
        filename = file['name']
        if filename == '.emptyFolderPlaceholder':
            continue
        print(f"Downloading {filename} ...")
        res = supabase.storage.from_(BUCKET_NAME).download(filename)
        if res:
            local_path = os.path.join(LOCAL_FOLDER, filename)
            with open(local_path, 'wb') as f:
                f.write(res)
        else:
            print(f"Failed to download {filename}")

if __name__ == "__main__":
    download_all_files(LOCAL_FOLDER)