-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathminio.py
More file actions
238 lines (221 loc) · 6.6 KB
/
minio.py
File metadata and controls
238 lines (221 loc) · 6.6 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
from datetime import timedelta
import datetime
import io
from typing import BinaryIO
import time
from typing import Optional
import urllib.parse as urlparse
import aiohttp
import anyio.abc
from ..abc import ResponseFile, ResponseFileMemory, ResponseFileRemote
from ..utils import UnboundTTLCache
from ..logger import logger
from .abc import CPath, FileInfo, Storage
from miniopy_async import Minio
from miniopy_async.api import BaseURL, presign_v4
from miniopy_async.datatypes import Object
class MinioStorage(Storage):
type = "minio"
def __init__(
self,
name: str,
path: str,
weight: int,
access_key: str,
secret_key: str,
endpoint: str,
bucket: str,
**kwargs
):
super().__init__(name, path, weight, **kwargs)
self.endpoint = endpoint
self.bucket = bucket
self.access_key = access_key
self.secret_key = secret_key
self.region = kwargs.get("region", "pyoba")
self.custom_host = kwargs.get("custom_host")
self.public_endpoint = kwargs.get("public_endpoint")
url = urlparse.urlparse(self.endpoint)
self._cache: UnboundTTLCache[str, ResponseFile] = UnboundTTLCache(
maxsize=self.cache_size,
ttl=self.cache_ttl
)
self.minio = Minio(
endpoint=url.netloc,
access_key=access_key,
secret_key=secret_key,
region=self.region,
secure=url.scheme == "https",
)
async def setup(
self,
task_group: anyio.abc.TaskGroup
):
await super().setup(task_group)
if not await self.minio.bucket_exists(self.bucket):
await self.minio.make_bucket(
self.bucket,
location=self.region
)
task_group.start_soon(self._check)
async def list_files(
self,
path: str
) -> list[FileInfo]:
root = self.path / path
res = []
# only files
async for obj in self.minio.list_objects(
self.bucket,
prefix=str(root)[1:],
recursive=True
):
if not isinstance(obj, Object):
continue
res.append(FileInfo(
name=CPath(obj.object_name).name,
size=int(obj.size or 0),
path=str(obj.object_name),
))
return res
async def upload(
self,
path: str,
data: BinaryIO,
size: int
):
root = self.path / path
await self.minio.put_object(
self.bucket,
str(root)[1:],
data,
size
)
return True
async def _check(
self,
):
file = self.get_py_check_path()
while 1:
try:
data = str(time.perf_counter_ns())
await self.minio.put_object(
self.bucket,
file.name,
io.BytesIO(data.encode("utf-8")),
len(data)
)
self.online = True
except:
self.online = False
logger.traceback()
finally:
self.emit_status()
await anyio.sleep(60)
async def get_file(self, path: str) -> ResponseFile:
cpath = str(self.path / path)
# get file info
file = self._cache.get(cpath)
if file is not None:
return file
try:
stat = await self.minio.stat_object(
self.bucket,
cpath[1:],
)
except:
stat = Object(
bucket_name=self.bucket,
object_name=cpath[1:],
)
if stat.size == 0:
file = ResponseFileMemory(
b"",
0
)
elif self.custom_host is not None:
file = ResponseFileRemote(
f"{self.custom_host}/{self.bucket}{cpath}",
int(stat.size or 0),
)
elif self.public_endpoint is not None:
url = await get_presigned_url(
self.minio,
"GET",
self.bucket,
cpath[1:],
region=self.region,
change_host=self.public_endpoint,
)
file = ResponseFileRemote(
url,
int(stat.size or 0),
)
else:
async with aiohttp.ClientSession() as session:
async with (await self.minio.get_object(
self.bucket,
cpath[1:],
session
)) as resp:
file = ResponseFileMemory(
await resp.read(),
int(stat.size or 0),
)
self._cache[cpath] = file
return file
async def check_measure(self, size: int) -> bool:
cpath = str(self.path / "measure" / size)
stat = await self.minio.stat_object(
self.bucket,
cpath[1:],
)
return stat.size == size * 1024 * 1024
async def get_presigned_url(
minio: Minio,
method: str,
bucket_name: str,
object_name: str,
region: Optional[str] = None,
expires: timedelta = timedelta(days=7),
request_date: Optional[datetime.datetime] = None,
extra_query_params=None,
change_host=None,
):
if expires.total_seconds() < 1 or expires.total_seconds() > 604800:
raise ValueError("expires must be between 1 second to 7 days")
region = region or await minio._get_region(bucket_name, None)
query_params = extra_query_params or {}
creds = minio._provider.retrieve() if minio._provider else None
if creds and creds.session_token:
query_params["X-Amz-Security-Token"] = creds.session_token
url = None
if change_host:
url = BaseURL(
change_host,
region,
).build(
method,
region,
bucket_name=bucket_name,
object_name=object_name,
query_params=query_params,
)
else:
url = minio._base_url.build(
method,
region,
bucket_name=bucket_name,
object_name=object_name,
query_params=query_params,
)
if creds:
url = presign_v4(
method,
url,
region,
creds,
request_date or datetime.datetime.now(datetime.UTC),
int(expires.total_seconds()),
)
return urlparse.urlunsplit(url)