-
Notifications
You must be signed in to change notification settings - Fork 0
/
Download File Bytea Postgresql.py
62 lines (49 loc) · 1.91 KB
/
Download File Bytea Postgresql.py
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
import psycopg2
import requests
import os
import base64
def download_files_from_database():
# Informasi koneksi ke database PostgreSQL
db_host = 'localhost'
db_name = 'sister_db'
db_user = 'postgres'
db_password = '4dm1n'
# Membuat koneksi ke database
conn = psycopg2.connect(
host=db_host,
dbname=db_name,
user=db_user,
password=db_password
)
# Membuat cursor
cursor = conn.cursor()
try:
# Query untuk mendapatkan data file dari tabel 'dokumen'
cursor.execute("SELECT id_dok, file_name, media_type, file_dok FROM dok.dokumen")
rows = cursor.fetchall()
# Loop melalui hasil query dan mendownload file
for row in rows:
doc_id, nama_file, tipe_file, data_file = row
if data_file is not None: # Cek apakah data_file berisi data yang valid
# Konversi data BYTEA menjadi bentuk bytes yang benar
data_file_bytes = base64.b64decode(data_file)
# Tentukan direktori penyimpanan untuk file
save_dir = 'downloads'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# Tentukan nama file yang akan disimpan
save_path = os.path.join(save_dir, nama_file)
# Mendownload file dari URL dan menyimpannya di direktori yang telah ditentukan
with open(save_path, 'wb') as f:
f.write(data_file_bytes)
print(f"File '{nama_file}' (ID: {doc_id}) berhasil diunduh.")
else:
print(f"File '{nama_file}' (ID: {doc_id}) tidak dapat diunduh karena data kosong.")
except Exception as e:
print("Terjadi kesalahan saat mengunduh file:", e)
finally:
# Tutup cursor dan koneksi
cursor.close()
conn.close()
if __name__ == "__main__":
download_files_from_database()