-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcloud_storage.py
More file actions
547 lines (457 loc) · 19 KB
/
gcloud_storage.py
File metadata and controls
547 lines (457 loc) · 19 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/usr/bin/env python3
"""
gcloud_storage.py — Hybrid Audio API (HARDENED EDITION)
──────────────────────────────────────────────────────────────
v3.6 NDF-004 — Base GCS integration
v3.9.1 NDF-040 — Structured path + rotational support
v3.9.1-H1 — HARDENING LAYER
• Sanitización estricta de paths
• Validación de bucket / client antes de cada operación
• Manejo detallado de errores (sin comprometer NDF)
• Protección contra folder traversal
• Hardened resolver + file guards
• Upload wrappers reforzados
• Logs menos verbosos en prod (respeta DEBUG)
Author: José Soto
"""
import os
import time
from datetime import timedelta
from pathlib import Path
from typing import Optional, Dict, Any
# Optional Google SDK
try:
from google.cloud import storage
from google.api_core import exceptions as gcs_exceptions
except ImportError:
storage = None
gcs_exceptions = None
# Internal config
from config import (
GCS_BUCKET,
GCS_FOLDER_OUTPUTS,
GCS_FOLDER_STEMS,
GOOGLE_APPLICATION_CREDENTIALS,
URL_BASE_GCS,
PUBLIC_ACCESS,
DEBUG,
is_gcs_enabled,
)
# Optional audit log
try:
from gcs_audit import log_gcs_audit
except Exception:
def log_gcs_audit(*args, **kwargs):
return False
# Optional structured GCS logs
try:
from observability.gcs_logs import log_gcs_event, log_gcs_error
except Exception:
def log_gcs_event(*args, **kwargs):
return None
def log_gcs_error(*args, **kwargs):
return None
# ───────────────────────────────────────────────────────────────
# Sanitized path helpers
# ───────────────────────────────────────────────────────────────
def _sanitize_filename(filename: str) -> str:
"""Prevent directory traversal and illegal characters in file names."""
name = os.path.basename(filename)
return name.replace("..", "").replace("\\", "/").strip()
def _sanitize_folder(folder: str) -> str:
folder = folder.strip().replace("..", "").replace("\\", "/")
return folder.rstrip("/")
# ───────────────────────────────────────────────────────────────
# GCS client initialization
# ───────────────────────────────────────────────────────────────
def init_gcs_client() -> Optional["storage.Client"]:
if not is_gcs_enabled():
if DEBUG:
print("⚠️ GCS disabled or credentials missing — local mode only.")
return None
if storage is None:
if DEBUG:
print("⚠️ google-cloud-storage not installed.")
return None
try:
cred_path = str(Path(GOOGLE_APPLICATION_CREDENTIALS).expanduser())
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = cred_path
return storage.Client()
except Exception as e:
if DEBUG:
print(f"⚠️ Failed to init GCS client: {e}")
log_gcs_error("init_gcs_client", str(e))
return None
# ───────────────────────────────────────────────────────────────
# Signed URL
# ───────────────────────────────────────────────────────────────
def generate_signed_url(blob, expiration_seconds: int = 86400) -> Optional[str]:
try:
return blob.generate_signed_url(
version="v4",
expiration=timedelta(seconds=expiration_seconds),
method="GET",
)
except Exception as e:
if DEBUG:
print(f"⚠️ Failed to generate signed URL: {e}")
log_gcs_error("generate_signed_url", str(e))
return None
# ───────────────────────────────────────────────────────────────
# Hardened blob-path resolver
# ───────────────────────────────────────────────────────────────
def resolve_gcs_blob_name(local_path: str, folder: Optional[str] = None) -> str:
"""
Securely maps a local path -> GCS blob path.
If the file lives under a structured stems directory:
stems/name/<NAME>/file.wav
stems/developer/<DEV>/file.wav
stems/script/<SCRIPT>/file.wav
→ Preserve the entire relative path in GCS.
Else: fallback to classic behavior:
<folder>/<filename>
Always sanitizes both folder and filename.
"""
p = Path(local_path)
# Detect structured stems hierarchy
try:
parts = p.parts
if "stems" in parts:
idx = parts.index("stems")
relative = Path(*parts[idx:]) # stems/.../*.wav
clean = _sanitize_folder(str(relative))
return clean
except Exception:
pass
# Classic fallback mode (legacy-compatible)
filename = _sanitize_filename(local_path)
folder = _sanitize_folder(folder) if folder else ""
return f"{folder}/{filename}" if folder else filename
# ───────────────────────────────────────────────────────────────
# Uploader
# ───────────────────────────────────────────────────────────────
def upload_to_gcs(local_file: str, folder: str = GCS_FOLDER_OUTPUTS) -> Dict[str, Any]:
file_path = Path(local_file)
# Local existence check
if not file_path.exists() or not file_path.is_file():
msg = f"File not found: {local_file}"
log_gcs_error("upload_to_gcs", msg)
return {"ok": False, "error": msg}
# GCS disabled
if not is_gcs_enabled():
payload = {
"mode": "local-only",
"file_path": str(file_path),
"reason": "GCS disabled",
}
log_gcs_event("upload_skipped", payload)
return {"ok": False, **payload}
# Client init
client = init_gcs_client()
if client is None:
payload = {
"mode": "local-only",
"file_path": str(file_path),
"reason": "GCS client unavailable",
}
log_gcs_event("upload_skipped", payload)
return {"ok": False, **payload}
if not GCS_BUCKET:
msg = "GCS_BUCKET not configured"
log_gcs_error("upload_to_gcs", msg)
return {"ok": False, "error": msg}
try:
t0 = time.time()
bucket = client.bucket(GCS_BUCKET)
if not bucket.exists():
msg = f"Bucket not found: {GCS_BUCKET}"
log_gcs_error("upload_to_gcs", msg)
return {"ok": False, "error": msg}
blob_name = resolve_gcs_blob_name(str(file_path), folder)
blob = bucket.blob(blob_name)
blob.upload_from_filename(str(file_path))
signed_url = generate_signed_url(blob)
latency = round(time.time() - t0, 3)
metadata = {
"ok": True,
"bucket": GCS_BUCKET,
"blob_name": blob_name,
"size_bytes": file_path.stat().st_size,
"uploaded_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"latency_sec": latency,
"public_access": PUBLIC_ACCESS,
"file_url": signed_url or f"{URL_BASE_GCS}/{blob_name}",
"signed_url": signed_url,
}
try:
log_gcs_audit(metadata)
log_gcs_event(
"upload_success",
{
"bucket": GCS_BUCKET,
"blob_name": blob_name,
"size_bytes": metadata["size_bytes"],
"latency_sec": latency,
},
)
except Exception:
pass
return metadata
except gcs_exceptions.NotFound:
msg = f"Bucket not found: {GCS_BUCKET}"
log_gcs_error("upload_to_gcs", msg)
return {"ok": False, "error": msg}
except Exception as e:
log_gcs_error("upload_to_gcs", str(e))
return {"ok": False, "error": str(e)}
# ───────────────────────────────────────────────────────────────
# Convenience wrappers
# ───────────────────────────────────────────────────────────────
def upload_stem_file(stem_path: str) -> Dict[str, Any]:
return upload_to_gcs(stem_path, folder=GCS_FOLDER_STEMS)
def upload_output_file(output_path: str) -> Dict[str, Any]:
return upload_to_gcs(output_path, folder=GCS_FOLDER_OUTPUTS)
# ───────────────────────────────────────────────────────────────
# Health check
# ───────────────────────────────────────────────────────────────
def gcs_healthcheck() -> Dict[str, Any]:
if not is_gcs_enabled() or storage is None:
return {"ok": False, "enabled": False, "reason": "GCS disabled or missing SDK"}
try:
t0 = time.time()
client = init_gcs_client()
if client is None:
return {"ok": False, "enabled": False, "reason": "Client init failed"}
bucket = client.bucket(GCS_BUCKET)
exists = bucket.exists()
latency = round(time.time() - t0, 3)
return {
"ok": bool(exists),
"enabled": True,
"bucket": GCS_BUCKET,
"mode": "public" if PUBLIC_ACCESS else "restricted",
"latency_sec": latency,
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
}
except Exception as e:
log_gcs_error("gcs_healthcheck", str(e))
return {
"ok": False,
"enabled": True,
"error": str(e),
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
}
# ───────────────────────────────────────────────────────────────
# Minimal compatibility wrappers for tests + API (v1)
# ───────────────────────────────────────────────────────────────
def gcs_check_file_exists(blob_path: str) -> bool:
"""
Minimal existence checker for internal + test use.
Returns:
True → blob exists in GCS bucket
False → missing OR GCS disabled OR SDK unavailable
Never raises on error.
"""
try:
client = init_gcs_client()
if not client or not is_gcs_enabled() or not GCS_BUCKET:
return False
bucket = client.bucket(GCS_BUCKET)
blob = bucket.blob(blob_path)
return blob.exists()
except Exception as e:
log_gcs_error("gcs_check_file_exists", str(e))
return False
def gcs_resolve_uri(blob_path: str) -> str:
"""
Build a public-style GCS URL for a blob.
This is used by rotation and cache layers.
"""
try:
return f"{URL_BASE_GCS}/{blob_path}"
except Exception:
return blob_path
# ───────────────────────────────────────────────────────────────
# v2 API surface (explicit filename/blob operations)
# ───────────────────────────────────────────────────────────────
def _get_gcs_bucket():
"""
Internal helper to get an initialized bucket instance.
Returns:
bucket object or None on failure.
"""
try:
if not is_gcs_enabled() or not GCS_BUCKET:
return None
client = init_gcs_client()
if client is None:
return None
bucket = client.bucket(GCS_BUCKET)
return bucket
except Exception as e:
log_gcs_error("_get_gcs_bucket", str(e))
return None
def gcs_check_file_exists_v2(blob_path: str) -> bool:
"""
Existence check for a GCS blob by its path.
This function is intended for higher-level batch and consistency checks.
It never raises and returns False on any failure or if GCS is disabled.
"""
try:
bucket = _get_gcs_bucket()
if bucket is None:
return False
clean_blob = _sanitize_folder(blob_path)
blob = bucket.blob(clean_blob)
t0 = time.time()
exists = blob.exists()
latency = round(time.time() - t0, 3)
log_gcs_event(
"exists_check",
{
"blob_name": clean_blob,
"exists": bool(exists),
"latency_sec": latency,
},
)
return bool(exists)
except Exception as e:
log_gcs_error("gcs_check_file_exists_v2", str(e))
return False
def upload_file_v2(local_path: str, blob_path: str) -> Dict[str, Any]:
"""
Upload a local file to an explicit GCS blob path.
Args:
local_path: Path to the local file.
blob_path: Target blob path inside the bucket (e.g. "stems/name/...wav").
Returns:
Dict with keys:
ok (bool)
bucket (str, optional)
blob_name (str, optional)
local_path (str)
size_bytes (int, optional)
error (str, optional)
"""
file_path = Path(local_path)
if not file_path.exists() or not file_path.is_file():
msg = f"File not found: {local_path}"
log_gcs_error("upload_file_v2", msg)
return {"ok": False, "local_path": str(file_path), "error": msg}
bucket = _get_gcs_bucket()
if bucket is None:
payload = {
"ok": False,
"local_path": str(file_path),
"reason": "GCS disabled or bucket unavailable",
}
log_gcs_event("upload_v2_skipped", payload)
return payload
try:
clean_blob = _sanitize_folder(blob_path)
blob = bucket.blob(clean_blob)
t0 = time.time()
blob.upload_from_filename(str(file_path))
latency = round(time.time() - t0, 3)
signed_url = generate_signed_url(blob)
result = {
"ok": True,
"bucket": GCS_BUCKET,
"blob_name": clean_blob,
"local_path": str(file_path),
"size_bytes": file_path.stat().st_size,
"uploaded_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"latency_sec": latency,
"file_url": signed_url or f"{URL_BASE_GCS}/{clean_blob}",
"signed_url": signed_url,
}
log_gcs_event(
"upload_v2_success",
{
"bucket": GCS_BUCKET,
"blob_name": clean_blob,
"size_bytes": result["size_bytes"],
"latency_sec": latency,
},
)
return result
except Exception as e:
msg = str(e)
log_gcs_error("upload_file_v2", msg)
return {
"ok": False,
"local_path": str(file_path),
"blob_name": blob_path,
"error": msg,
}
def download_file_v2(blob_path: str, local_path: str) -> Dict[str, Any]:
"""
Download a GCS blob into a local file.
Args:
blob_path: Path to the blob in the bucket.
local_path: Local target path (directories will be created if missing).
Returns:
Dict with keys:
ok (bool)
bucket (str, optional)
blob_name (str)
local_path (str)
size_bytes (int, optional)
error (str, optional)
"""
bucket = _get_gcs_bucket()
if bucket is None:
payload = {
"ok": False,
"blob_name": blob_path,
"local_path": local_path,
"reason": "GCS disabled or bucket unavailable",
}
log_gcs_event("download_v2_skipped", payload)
return payload
try:
clean_blob = _sanitize_folder(blob_path)
blob = bucket.blob(clean_blob)
if not blob.exists():
msg = f"Blob not found: {clean_blob}"
log_gcs_error("download_file_v2", msg)
return {
"ok": False,
"blob_name": clean_blob,
"local_path": local_path,
"error": msg,
}
local_path_obj = Path(local_path)
local_path_obj.parent.mkdir(parents=True, exist_ok=True)
t0 = time.time()
blob.download_to_filename(str(local_path_obj))
latency = round(time.time() - t0, 3)
size_bytes = local_path_obj.stat().st_size if local_path_obj.exists() else 0
result = {
"ok": True,
"bucket": GCS_BUCKET,
"blob_name": clean_blob,
"local_path": str(local_path_obj),
"size_bytes": size_bytes,
"downloaded_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"latency_sec": latency,
}
log_gcs_event(
"download_v2_success",
{
"bucket": GCS_BUCKET,
"blob_name": clean_blob,
"size_bytes": size_bytes,
"latency_sec": latency,
},
)
return result
except Exception as e:
msg = str(e)
log_gcs_error("download_file_v2", msg)
return {
"ok": False,
"blob_name": blob_path,
"local_path": local_path,
"error": msg,
}