Repository navigation
Expand file tree
/
Copy pathupload_example.py
More file actions
111 lines (81 loc) · 4.71 KB
/
Copy pathupload_example.py
File metadata and controls
111 lines (81 loc) · 4.71 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
"""CloudUploader SDK — basic usage examples.
"""
import sys
import os
from cloud_uploader import CloudUploader, CloudUploaderError
# ── Configuration ───────────────────────────────────────────────────
API_KEY = os.environ.get("CLOUD_UPLOADER_API_KEY", "")
BASE_URL = os.environ.get("CLOUD_UPLOADER_URL", "http://localhost:8080")
# ── 1. Simple upload ───────────────────────────────────────────────
def simple_upload(file_path: str) -> None:
"""Upload a file with the simplest possible API."""
uploader = CloudUploader(api_key=API_KEY, base_url=BASE_URL)
result = uploader.upload_file(file_path)
print(f"✅ Upload complete!")
print(f" upload_id : {result.upload_id}")
print(f" mode : {result.mode}")
print(f" key : {result.key}")
print(f" storage_path: {result.storage_path}")
# ── 2. Upload with progress bar ────────────────────────────────────
def upload_with_progress(file_path: str) -> None:
"""Upload a file with a real-time progress bar."""
def progress(uploaded: int, total: int) -> None:
pct = (uploaded / total * 100) if total else 0
bar_len = 40
filled = int(bar_len * uploaded / total) if total else 0
bar = "█" * filled + "░" * (bar_len - filled)
print(f"\r [{bar}] {pct:5.1f}% ({uploaded:,}/{total:,} bytes)", end="", flush=True)
uploader = CloudUploader(
api_key=API_KEY,
base_url=BASE_URL
)
result = uploader.upload_file(file_path, progress_callback=progress)
print() # newline after progress bar
print(f"✅ Done → {result.storage_path}")
# ── 3. Upload to a specific storage backend ────────────────────────
def upload_to_s3(file_path: str) -> None:
"""Upload explicitly to S3 instead of the default R2."""
uploader = CloudUploader(api_key=API_KEY, base_url=BASE_URL)
result = uploader.upload_file(file_path, storage="s3")
print(f"✅ Uploaded to S3: {result.storage_path}")
# ── 4. Error handling ──────────────────────────────────────────────
def safe_upload(file_path: str) -> None:
"""Upload with comprehensive error handling."""
try:
with CloudUploader(api_key=API_KEY, base_url=BASE_URL) as uploader:
result = uploader.upload_file(file_path)
print(f"✅ {result.storage_path}")
except CloudUploaderError as e:
print(f"❌ Upload failed: {e.message}")
if e.error_code:
print(f" Error code : {e.error_code}")
if e.status_code:
print(f" HTTP status: {e.status_code}")
sys.exit(1)
# ── 5. Check upload status ─────────────────────────────────────────
def check_status(upload_id: str) -> None:
"""Query the status of a previous upload."""
uploader = CloudUploader(api_key=API_KEY, base_url=BASE_URL)
status = uploader.get_upload_status(upload_id)
print(f"Upload {upload_id}:")
for k, v in status.items():
print(f" {k}: {v}")
# ── 6. Upload a whole folder ───────────────────────────────────────
def uploadWholeFolder(folder_path: str) -> None:
"""Upload all files in a folder."""
uploader = CloudUploader(api_key=API_KEY, base_url=BASE_URL)
result = uploader.upload_folder(folder_path)
print(f"Succeeded: {result.succeeded}/{result.total_files}")
if result.failures:
print(f"Failed files: {len(result.failures)}")
# ── Main ────────────────────────────────────────────────────────────
if __name__ == "__main__":
# simple upload with default settings
simple_upload('/home/rafeeque/Rafeeque/Free-programming-books/Web Development/Eloquent_JavaScript.pdf')
# upload with a progress bar and debug logging
upload_with_progress('/home/rafeeque/Rafeeque/Free-programming-books/Web Development/Learning-web-4ed_Robbins.pdf')
# safe upload with detailed error handling
safe_upload('/home/rafeeque/Rafeeque/Free-programming-books/Web Development/Web Standards.pdf')
# upload whole folder
uploadWholeFolder("/home/rafeeque/Rafeeque/Free-programming-books/fulllist")