Skip to content

Commit 1cd117d

Browse files
committed
Fix MinIO client
1 parent 22f8ea4 commit 1cd117d

1 file changed

Lines changed: 56 additions & 33 deletions

File tree

src/integrations/minio/client.py

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,12 @@ def __init__(self):
1515
endpoint=config.MINIO_ENDPOINT,
1616
access_key=config.MINIO_ACCESS_KEY,
1717
secret_key=config.MINIO_SECRET_KEY.get_secret_value(),
18-
secure=config.MINIO_SECURE,
19-
region=config.MINIO_REGION,
18+
secure=False,
2019
)
2120

2221
self._external_endpoint = config.MINIO_EXTERNAL_ENDPOINT
23-
if self._external_endpoint:
24-
self._external_for_sign = Minio(
25-
endpoint=self._external_endpoint,
26-
access_key=config.MINIO_ACCESS_KEY,
27-
secret_key=config.MINIO_SECRET_KEY.get_secret_value(),
28-
secure=config.MINIO_SECURE,
29-
region=config.MINIO_REGION,
30-
)
31-
else:
32-
self._external_for_sign = self._client
22+
self._scheme = 'https' if config.MINIO_SECURE else 'http'
23+
self._endpoint_url = f'{self._scheme}://{self._external_endpoint}' if self._external_endpoint else None
3324

3425
self._boto3_session = aioboto3.Session(
3526
aws_access_key_id=config.MINIO_ACCESS_KEY,
@@ -62,12 +53,29 @@ async def delete_object(self, bucket_name: str, object_name: str) -> None:
6253
async def get_presigned_download_url(
6354
self, bucket_name: str, object_name: str, expires_in: int = 3600
6455
) -> tuple[str, datetime]:
65-
url = await asyncio.to_thread(
66-
self._external_for_sign.presigned_get_object,
67-
bucket_name,
68-
object_name,
69-
expires=timedelta(seconds=expires_in),
70-
)
56+
if self._endpoint_url:
57+
async with self._boto3_session.client(
58+
's3',
59+
endpoint_url=self._endpoint_url,
60+
config=self._boto3_config,
61+
region_name=config.MINIO_REGION,
62+
) as client:
63+
url = await client.generate_presigned_url(
64+
ClientMethod='get_object',
65+
HttpMethod='GET',
66+
Params={
67+
'Bucket': bucket_name,
68+
'Key': object_name,
69+
},
70+
ExpiresIn=expires_in,
71+
)
72+
else:
73+
url = await asyncio.to_thread(
74+
self._client.presigned_get_object,
75+
bucket_name,
76+
object_name,
77+
expires=timedelta(seconds=expires_in),
78+
)
7179

7280
expires_at = datetime.now(UTC) + timedelta(seconds=expires_in)
7381
return url, expires_at
@@ -79,12 +87,30 @@ async def delete_objects(self, bucket_name: str, object_names: list[str]) -> Non
7987
async def presigned_put_object(
8088
self, bucket_name: str, object_name: str, expires: timedelta = timedelta(hours=1)
8189
) -> str:
82-
return await asyncio.to_thread(
83-
self._external_for_sign.presigned_put_object,
84-
bucket_name,
85-
object_name,
86-
expires=expires,
87-
)
90+
if self._endpoint_url:
91+
expires_seconds = int(expires.total_seconds())
92+
async with self._boto3_session.client(
93+
's3',
94+
endpoint_url=self._endpoint_url,
95+
config=self._boto3_config,
96+
region_name=config.MINIO_REGION,
97+
) as client:
98+
return await client.generate_presigned_url(
99+
ClientMethod='put_object',
100+
HttpMethod='PUT',
101+
Params={
102+
'Bucket': bucket_name,
103+
'Key': object_name,
104+
},
105+
ExpiresIn=expires_seconds,
106+
)
107+
else:
108+
return await asyncio.to_thread(
109+
self._client.presigned_put_object,
110+
bucket_name,
111+
object_name,
112+
expires=expires,
113+
)
88114

89115
async def create_multipart_upload(self, bucket_name: str, object_name: str) -> str:
90116
return await asyncio.to_thread(
@@ -102,16 +128,15 @@ async def presigned_put_part_url(
102128
part_number: int,
103129
expires: timedelta = timedelta(hours=1),
104130
) -> str:
105-
endpoint = self._external_endpoint or config.MINIO_ENDPOINT
106-
scheme = 'https' if config.MINIO_SECURE else 'http'
107-
endpoint_url = f'{scheme}://{endpoint}'
131+
endpoint_url = self._endpoint_url or f'http://{config.MINIO_ENDPOINT}'
108132

109133
expires_seconds = int(expires.total_seconds())
110134

111135
async with self._boto3_session.client(
112136
's3',
113137
endpoint_url=endpoint_url,
114138
config=self._boto3_config,
139+
region_name=config.MINIO_REGION,
115140
) as client:
116141
return await client.generate_presigned_url(
117142
ClientMethod='upload_part',
@@ -132,16 +157,15 @@ async def complete_multipart_upload(
132157
upload_id: str,
133158
parts: list[dict],
134159
) -> None:
135-
endpoint = self._external_endpoint or config.MINIO_ENDPOINT
136-
scheme = 'https' if config.MINIO_SECURE else 'http'
137-
endpoint_url = f'{scheme}://{endpoint}'
160+
endpoint_url = self._endpoint_url or f'http://{config.MINIO_ENDPOINT}'
138161

139162
s3_parts = [{'ETag': p['ETag'].strip('"'), 'PartNumber': p['PartNumber']} for p in parts]
140163

141164
async with self._boto3_session.client(
142165
's3',
143166
endpoint_url=endpoint_url,
144167
config=self._boto3_config,
168+
region_name=config.MINIO_REGION,
145169
) as client:
146170
await client.complete_multipart_upload(
147171
Bucket=bucket_name,
@@ -156,14 +180,13 @@ async def abort_multipart_upload(
156180
object_name: str,
157181
upload_id: str,
158182
) -> None:
159-
endpoint = self._external_endpoint or config.MINIO_ENDPOINT
160-
scheme = 'https' if config.MINIO_SECURE else 'http'
161-
endpoint_url = f'{scheme}://{endpoint}'
183+
endpoint_url = self._endpoint_url or f'http://{config.MINIO_ENDPOINT}'
162184

163185
async with self._boto3_session.client(
164186
's3',
165187
endpoint_url=endpoint_url,
166188
config=self._boto3_config,
189+
region_name=config.MINIO_REGION,
167190
) as client:
168191
await client.abort_multipart_upload(
169192
Bucket=bucket_name,

0 commit comments

Comments
 (0)