-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_backend.py
78 lines (58 loc) · 2.36 KB
/
storage_backend.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
import os
from abc import ABC, abstractmethod
class StorageBackend(ABC):
@abstractmethod
def read_object(self, object_key: str) -> bytes:
pass
@abstractmethod
def write_object(self, object_key: str, object_data: bytes) -> None:
pass
@abstractmethod
def delete_object(self, object_key: str) -> None:
pass
@abstractmethod
def object_exists(self, object_key: str) -> bool:
pass
@abstractmethod
def read_metadata(self, object_key: str, metadata_key: str) -> str:
pass
class DiskStorageBackend(StorageBackend):
def __init__(self, base_path: str):
self.base_path = base_path
def read_object(self, object_key: str) -> bytes:
with open(self._get_path(object_key), "rb") as f:
return f.read()
def write_object(self, object_key: str, object_data: bytes) -> None:
with open(self._get_path(object_key), "wb") as f:
f.write(object_data)
def delete_object(self, object_key: str) -> None:
path = self._get_path(object_key)
if os.path.exists(path):
os.remove(path)
def object_exists(self, object_key: str) -> bool:
return os.path.exists(self._get_path(object_key))
def read_metadata(self, object_key: str, metadata_key: str) -> str:
path = self._get_path(object_key) + ".metadata"
if os.path.exists(path):
with open(path, "r") as f:
metadata = json.load(f)
return metadata.get(metadata_key, "")
else:
return ""
def _get_path(self, object_key: str) -> str:
return os.path.join(self.base_path, object_key)
class StorageManager:
def __init__(self, backend: str, base_path: str):
if backend == "disk":
self.storage_backend = DiskStorageBackend(base_path)
else:
raise ValueError("Unsupported backend")
def read_object(self, object_key: str) -> bytes:
return self.storage_backend.read_object(object_key)
def write_object(self, object_key: str, object_data: bytes) -> None:
self.storage_backend.write_object(object_key, object_data)
def delete_object(self, object_key: str) -> None:
self.storage_backend.delete_object(object_key)
def object_exists(self, object_key: str) -> bool:
return self.storage_backend.object_exists(object_key)