-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_upload_files.py
More file actions
52 lines (41 loc) · 1.64 KB
/
client_upload_files.py
File metadata and controls
52 lines (41 loc) · 1.64 KB
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
import os
import requests
# Configuration
UPLOAD_SERVER = "https://mail.klaas.dk:18629"
LOCAL_FOLDER = "/var/opt/gitlab/backups"
BEARER_TOKEN = os.environ['TOKEN'] # Replace with your actual token
HEADERS = {"Authorization": f"Bearer {BEARER_TOKEN}"}
def get_remote_files():
"""Fetches the list of files from the upload server."""
try:
response = requests.get(f"{UPLOAD_SERVER}/list", headers=HEADERS)
response.raise_for_status()
data = response.json()
return set(os.path.basename(f) for f in data.get("files", []))
except requests.RequestException as e:
print(f"Error fetching remote file list: {e}")
return None
def upload_file(file_path):
"""Uploads a file to the server."""
try:
with open(file_path, "rb") as f:
files = {"file": f}
response = requests.post(f"{UPLOAD_SERVER}/upload", headers=HEADERS, files=files)
response.raise_for_status()
print(f"Uploaded: {file_path}")
except requests.RequestException as e:
print(f"Failed to upload {file_path}: {e}")
def main():
"""Main function to compare and upload missing files."""
remote_files = get_remote_files()
if remote_files is not None:
local_files = {f for f in os.listdir(LOCAL_FOLDER) if os.path.isfile(os.path.join(LOCAL_FOLDER, f))}
missing_files = local_files - remote_files
if not missing_files:
print("All files are already uploaded.")
return
for file in missing_files:
file_path = os.path.join(LOCAL_FOLDER, file)
upload_file(file_path)
if __name__ == "__main__":
main()