Skip to content

Commit 1eca2b6

Browse files
committed
pass minio_bucket down to minio_utils instead of using get_minio_client_and_bucket
1 parent cfdf03b commit 1eca2b6

2 files changed

Lines changed: 69 additions & 89 deletions

File tree

app/utils/minio_utils.py

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,42 @@
1818
logger = logging.getLogger(__name__)
1919

2020

21-
def fetch_ro_crate_from_minio(crate_id: str) -> str:
21+
def fetch_ro_crate_from_minio(minio_bucket: str, crate_id: str) -> str:
2222
"""
2323
Fetches an RO-Crate from MinIO based on the crate ID. Downloads the crate as a file and returns local file path.
2424
25+
:param minio_bucket: The MinIO bucket containing the RO-Crate.
2526
:param crate_id: The ID of the RO-Crate to fetch from MinIO.
2627
:return: The local file path where the RO-Crate is saved.
2728
"""
2829

29-
minio_client, bucket_name = get_minio_client_and_bucket()
30+
minio_client = get_minio_client()
3031

31-
rocrate_object = find_rocrate_object_on_minio(crate_id, minio_client, bucket_name)
32+
rocrate_object = find_rocrate_object_on_minio(crate_id, minio_client, minio_bucket)
3233

3334
rocrate_path = rocrate_object.object_name
34-
rocrate_name = rocrate_path.split('/')[-1]
35+
rocrate_name = rocrate_path.split('/')[-1]
3536

3637
temp_dir = tempfile.mkdtemp()
3738
root_path = os.path.join(temp_dir, rocrate_name)
3839

3940
logging.info(
40-
f"Fetching RO-Crate {rocrate_name} from MinIO bucket {bucket_name}. File path {root_path}"
41+
f"Fetching RO-Crate {rocrate_name} from MinIO bucket {minio_bucket}. File path {root_path}"
4142
)
4243

4344
if rocrate_object.is_dir:
4445
os.makedirs(os.path.dirname(root_path), exist_ok=True)
4546

46-
objects_list = get_minio_object_list(rocrate_path, minio_client, bucket_name, recursive=True)
47+
objects_list = get_minio_object_list(rocrate_path, minio_client, minio_bucket, recursive=True)
4748
for obj in objects_list:
4849
relative_path = obj.object_name[len(rocrate_path):].lstrip("/")
4950
local_file_path = os.path.join(root_path, relative_path)
5051
os.makedirs(os.path.dirname(local_file_path), exist_ok=True)
51-
download_file_from_minio(minio_client, bucket_name, obj.object_name, local_file_path)
52+
download_file_from_minio(minio_client, minio_bucket, obj.object_name, local_file_path)
5253

5354
else:
5455
file_path = root_path
55-
download_file_from_minio(minio_client, bucket_name, rocrate_path, file_path)
56+
download_file_from_minio(minio_client, minio_bucket, rocrate_path, file_path)
5657

5758
logging.info(
5859
f"RO-Crate {rocrate_name} fetched successfully and saved to {root_path}."
@@ -61,10 +62,11 @@ def fetch_ro_crate_from_minio(crate_id: str) -> str:
6162
return root_path
6263

6364

64-
def update_validation_status_in_minio(crate_id: str, validation_status: str) -> None:
65+
def update_validation_status_in_minio(minio_bucket: str, crate_id: str, validation_status: str) -> None:
6566
"""
6667
Uploads the validation status to the MinIO bucket.
6768
69+
:param minio_bucket: The MinIO bucket containing the RO-Crate.
6870
:param crate_id: The ID of the RO-Crate in MinIO
6971
:param validation_status: The validation result to upload
7072
:raises S3Error: If an error occurs during the MinIO operation
@@ -79,10 +81,10 @@ def update_validation_status_in_minio(crate_id: str, validation_status: str) ->
7981

8082
try:
8183

82-
minio_client, bucket_name = get_minio_client_and_bucket()
84+
minio_client = get_minio_client()
8385

8486
minio_client.put_object(
85-
bucket_name,
87+
minio_bucket,
8688
object_name,
8789
data=BytesIO(validation_string),
8890
length=len(validation_string),
@@ -102,15 +104,16 @@ def update_validation_status_in_minio(crate_id: str, validation_status: str) ->
102104
raise InvalidAPIUsage(f"Unknown Error: {e}", 500)
103105

104106
logging.info(
105-
f"Validation status file uploaded to {bucket_name}/{object_name} successfully."
107+
f"Validation status file uploaded to {minio_bucket}/{object_name} successfully."
106108
)
107109

108110

109-
def get_validation_status_from_minio(crate_id: str) -> dict:
111+
def get_validation_status_from_minio(minio_bucket: str, crate_id: str) -> dict:
110112
"""
111113
Checks for the existence of a validation report for the given RO-Crate in the MinIO bucket.
112114
Returns validation message if it exists, or notification that it is missing if not.
113115
116+
:param minio_bucket: The MinIO bucket containing the RO-Crate.
114117
:param crate_id: The ID of the RO-Crate in MinIO
115118
:return validation_status: Either the validation status, or note that this does not exist
116119
@@ -123,10 +126,10 @@ def get_validation_status_from_minio(crate_id: str) -> dict:
123126

124127
try:
125128

126-
minio_client, bucket_name = get_minio_client_and_bucket()
129+
minio_client = get_minio_client()
127130

128131
response = minio_client.get_object(
129-
bucket_name,
132+
minio_bucket,
130133
object_name,
131134
)
132135

@@ -150,12 +153,12 @@ def get_validation_status_from_minio(crate_id: str) -> dict:
150153
return validation_message
151154

152155

153-
def download_file_from_minio(minio_client: object, bucket_name: str, object_path: str, file_path: str) -> None:
156+
def download_file_from_minio(minio_client: object, minio_bucket: str, object_path: str, file_path: str) -> None:
154157
"""
155158
Downloads a file from MinIO
156159
157160
:param minio_client: MinIO object
158-
:param bucket_name: name of MinIO bucket, string
161+
:param minio_bucket: name of MinIO bucket, string
159162
:param object_path: path to object on MinIO, string
160163
:param file_path: local path, string
161164
:raises S3Error: If an error occurs during the MinIO operation
@@ -164,7 +167,7 @@ def download_file_from_minio(minio_client: object, bucket_name: str, object_path
164167
"""
165168

166169
try:
167-
minio_client.fget_object(bucket_name, object_path, file_path)
170+
minio_client.fget_object(minio_bucket, object_path, file_path)
168171

169172
except S3Error as s3_error:
170173
logging.error(f"MinIO S3 Error: {s3_error}")
@@ -179,7 +182,7 @@ def download_file_from_minio(minio_client: object, bucket_name: str, object_path
179182
raise InvalidAPIUsage(f"Unknown Error: {e}", 500)
180183

181184

182-
def find_validation_object_on_minio(rocrate_id: str, minio_client, bucket_name: str, storage_path: str = None) -> object:
185+
def find_validation_object_on_minio(rocrate_id: str, minio_client, minio_bucket: str, storage_path: str = None) -> object:
183186
"""
184187
Checks that the requested object exists on the MinIO instance.
185188
@@ -189,7 +192,7 @@ def find_validation_object_on_minio(rocrate_id: str, minio_client, bucket_name:
189192
:param rocrate_id: string containing the name of ro-crate
190193
:param storage_path: string containing the path within which the ro-crate should be
191194
:param minio_client: minio object
192-
:param bucket_name: string containing bucket on minio
195+
:param minio_bucket: string containing bucket on minio
193196
:return return_object: rocrate object we require
194197
:raise Exception: If validation result can't be found, 400
195198
"""
@@ -201,7 +204,7 @@ def find_validation_object_on_minio(rocrate_id: str, minio_client, bucket_name:
201204
else:
202205
file_path = f"{rocrate_id}_validation/validation_status.txt"
203206

204-
file_list = get_minio_object_list(file_path, minio_client, bucket_name)
207+
file_list = get_minio_object_list(file_path, minio_client, minio_bucket)
205208

206209
return_object = False
207210
for obj in file_list:
@@ -216,7 +219,7 @@ def find_validation_object_on_minio(rocrate_id: str, minio_client, bucket_name:
216219
return return_object
217220

218221

219-
def find_rocrate_object_on_minio(rocrate_id: str, minio_client, bucket_name: str, storage_path: str = None) -> object | bool:
222+
def find_rocrate_object_on_minio(rocrate_id: str, minio_client, minio_bucket: str, storage_path: str = None) -> object | bool:
220223
"""
221224
Checks that the requested object exists on the MinIO instance.
222225
@@ -226,7 +229,7 @@ def find_rocrate_object_on_minio(rocrate_id: str, minio_client, bucket_name: str
226229
:param rocrate_id: string containing the name of ro-crate
227230
:param storage_path: string containing the path within which the ro-crate should be
228231
:param minio_client: minio object
229-
:param bucket_name: string containing bucket on minio
232+
:param minio_bucket: string containing bucket on minio
230233
:return return_object or False: rocrate object we require, or False result
231234
:raise Exception: If RO-Crate can't be found, 400
232235
"""
@@ -238,7 +241,7 @@ def find_rocrate_object_on_minio(rocrate_id: str, minio_client, bucket_name: str
238241
else:
239242
rocrate_path = rocrate_id
240243

241-
rocrate_list = get_minio_object_list(rocrate_path, minio_client, bucket_name)
244+
rocrate_list = get_minio_object_list(rocrate_path, minio_client, minio_bucket)
242245

243246
return_object = False
244247
for obj in rocrate_list:
@@ -254,14 +257,14 @@ def find_rocrate_object_on_minio(rocrate_id: str, minio_client, bucket_name: str
254257
return return_object
255258

256259

257-
def get_minio_object_list(object_path: str, minio_client, bucket_name: str, recursive: bool = False) -> list:
260+
def get_minio_object_list(object_path: str, minio_client, minio_bucket: str, recursive: bool = False) -> list:
258261
"""
259262
Creates a list of objects which match the object_id and path_prefix
260263
261264
:param object_path: The object ID, string
262265
:param path_prefix: Path prefix, string, optional
263266
:param minio_client: MinIO client object
264-
:param bucket_name: string
267+
:param minio_bucket: string
265268
:param recursive: boolean, default = False
266269
:return object_list: List containing objects of type minio.datatypes.Object
267270
:raises S3Error: If an error occurs during the MinIO operation, 500
@@ -271,7 +274,7 @@ def get_minio_object_list(object_path: str, minio_client, bucket_name: str, recu
271274

272275
try:
273276
response = minio_client.list_objects(
274-
bucket_name,
277+
minio_bucket,
275278
object_path,
276279
recursive=recursive
277280
)
@@ -295,11 +298,11 @@ def get_minio_object_list(object_path: str, minio_client, bucket_name: str, recu
295298
return object_list
296299

297300

298-
def get_minio_client_and_bucket() -> [Minio, str]:
301+
def get_minio_client() -> Minio:
299302
"""
300-
Initialises the MinIO client and retrieves the bucket name from environment variables.
303+
Initialises the MinIO client from environment variables.
301304
302-
:return: A tuple containing the MinIO client and the bucket name.
305+
:return: The MinIO client.
303306
:raises ValueError: If required environment variables are not set.
304307
"""
305308
load_dotenv()
@@ -311,10 +314,4 @@ def get_minio_client_and_bucket() -> [Minio, str]:
311314
secure=False,
312315
)
313316

314-
bucket_name = os.environ.get("MINIO_BUCKET_NAME")
315-
if not bucket_name:
316-
raise ValueError(
317-
"RO Crate MINIO_BUCKET_NAME is not set in the environment variables."
318-
)
319-
320-
return minio_client, bucket_name
317+
return minio_client

0 commit comments

Comments
 (0)