eshan6704 commited on
Commit
b4ad726
Β·
verified Β·
1 Parent(s): f45ab65

Create backblaze.py

Browse files
Files changed (1) hide show
  1. app/backblaze.py +158 -0
app/backblaze.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import boto3
2
+ from io import BytesIO
3
+ import pandas as pd
4
+ import json
5
+ import os
6
+
7
+ # ===========================
8
+ # Backblaze B2 Client Setup
9
+ # ===========================
10
+ S3_ENDPOINT = "https://s3.us-east-005.backblazeb2.com"
11
+ AWS_KEY_ID = "005239ca03b31af0000000001"
12
+ AWS_SECRET_KEY = "K005uGFZkrtYa4Hg1GliFUQohs/BTk4"
13
+
14
+ s3 = boto3.client(
15
+ "s3",
16
+ endpoint_url=S3_ENDPOINT,
17
+ aws_access_key_id=AWS_KEY_ID,
18
+ aws_secret_access_key=AWS_SECRET_KEY,
19
+ )
20
+
21
+ # ===========================
22
+ # Helper to get extension
23
+ # ===========================
24
+ def get_ext(file_name):
25
+ return os.path.splitext(file_name)[1].lower().replace(".", "")
26
+
27
+ # ===========================
28
+ # Upload any file (UNCHANGED)
29
+ # ===========================
30
+ def upload_file(bucket_name, file_name, file_content):
31
+ """
32
+ Upload any file to Backblaze B2.
33
+ Auto-detect type from file_name extension.
34
+ - str β†’ txt
35
+ - dict β†’ json
36
+ - pd.DataFrame β†’ csv or excel
37
+ - bytes β†’ raw files (pdf, png, etc.)
38
+ """
39
+ ext = get_ext(file_name)
40
+
41
+ if isinstance(file_content, pd.DataFrame):
42
+ buffer = BytesIO()
43
+ if ext in ["csv"]:
44
+ file_content.to_csv(buffer, index=False)
45
+ elif ext in ["xlsx", "xls"]:
46
+ file_content.to_excel(buffer, index=False)
47
+ else:
48
+ raise ValueError(f"Unsupported dataframe extension: {ext}")
49
+ buffer.seek(0)
50
+ s3.put_object(Bucket=bucket_name, Key=file_name, Body=buffer.getvalue())
51
+ return
52
+
53
+ if isinstance(file_content, dict) and ext == "json":
54
+ file_content = json.dumps(file_content)
55
+
56
+ if isinstance(file_content, str) and ext in ["txt", "csv", "json", "html"]:
57
+ file_content = file_content.encode("utf-8")
58
+
59
+ if isinstance(file_content, bytes):
60
+ s3.put_object(Bucket=bucket_name, Key=file_name, Body=file_content)
61
+ return
62
+
63
+ # fallback for str
64
+ s3.put_object(Bucket=bucket_name, Key=file_name, Body=file_content)
65
+
66
+ # ===========================
67
+ # Read any file (UNCHANGED)
68
+ # ===========================
69
+ def read_file(bucket_name, file_name):
70
+ """
71
+ Read a file from B2.
72
+ Auto-detect type from file_name extension.
73
+ Returns:
74
+ - str for txt, html, csv
75
+ - dict for json
76
+ - bytes for pdf, images, etc.
77
+ """
78
+ ext = get_ext(file_name)
79
+ try:
80
+ obj = s3.get_object(Bucket=bucket_name, Key=file_name)
81
+ data = obj['Body'].read()
82
+
83
+ if ext in ["txt", "html"]:
84
+ return data.decode("utf-8")
85
+ elif ext == "csv":
86
+ return pd.read_csv(BytesIO(data))
87
+ elif ext in ["xlsx", "xls"]:
88
+ return pd.read_excel(BytesIO(data))
89
+ elif ext == "json":
90
+ return json.loads(data)
91
+ else:
92
+ return data
93
+ except s3.exceptions.NoSuchKey:
94
+ return None
95
+ except Exception as e:
96
+ print(f"Error reading {file_name} from B2: {e}")
97
+ return None
98
+
99
+
100
+ # ============================================================
101
+ # NEW: Image compression + upload (SEPARATE, SAFE)
102
+ # ============================================================
103
+ from PIL import Image
104
+ import io
105
+
106
+
107
+ def compress_image(image_bytes, ext, quality=85):
108
+ """
109
+ Compress image bytes.
110
+ - PNG β†’ optimized (lossless)
111
+ - JPEG β†’ quality-based (lossy)
112
+ """
113
+ img = Image.open(io.BytesIO(image_bytes))
114
+ out = io.BytesIO()
115
+
116
+ if ext == "png":
117
+ img.save(out, format="PNG", optimize=True)
118
+
119
+ elif ext in ("jpg", "jpeg"):
120
+ img = img.convert("RGB")
121
+ img.save(
122
+ out,
123
+ format="JPEG",
124
+ quality=quality,
125
+ optimize=True,
126
+ progressive=True
127
+ )
128
+ else:
129
+ # Unsupported image type β†’ return original
130
+ return image_bytes
131
+
132
+ return out.getvalue()
133
+
134
+
135
+ def upload_image_compressed(bucket_name, file_name, image_bytes, quality=85):
136
+ """
137
+ Compress image and upload to B2 with correct Content-Type.
138
+ Does NOT affect existing upload_file().
139
+ """
140
+ ext = get_ext(file_name)
141
+
142
+ compressed = compress_image(image_bytes, ext, quality)
143
+
144
+ content_type_map = {
145
+ "png": "image/png",
146
+ "jpg": "image/jpeg",
147
+ "jpeg": "image/jpeg",
148
+ "webp": "image/webp",
149
+ }
150
+
151
+ content_type = content_type_map.get(ext, "application/octet-stream")
152
+
153
+ s3.put_object(
154
+ Bucket=bucket_name,
155
+ Key=file_name,
156
+ Body=compressed,
157
+ ContentType=content_type
158
+ )