updated for more general use

This commit is contained in:
brian
2024-10-25 17:09:43 +00:00
parent dc535167fa
commit e189b7c1f7
+8 -11
View File
@@ -1,10 +1,6 @@
"""Definition of put function."""
from __future__ import annotations
import logging
import os
from hashlib import md5
from io import BytesIO
from minio import Minio
@@ -13,14 +9,16 @@ from minio import Minio
def put(
client: Minio,
buffer: BytesIO,
) -> str:
bucket_name: str,
object_name: str,
) -> None:
"""Put buffer in bucket in MinIO and return MD5 checksum as object name."""
assert isinstance(client, Minio)
assert isinstance(buffer, BytesIO)
bucket_name = os.getenv('MINIO_BUCKET_NAME', default='')
assert isinstance(bucket_name, str)
assert len(bucket_name) > 0
# get md5 of image
checksum = md5(buffer.getbuffer()).hexdigest()
assert isinstance(object_name, str)
assert len(object_name) > 0
# prepare for saving
num_bytes = buffer.tell()
buffer.seek(0)
@@ -28,12 +26,11 @@ def put(
try:
client.put_object(
bucket_name=bucket_name,
object_name=checksum,
object_name=object_name,
length=num_bytes,
data=buffer,
)
except Exception as exc:
logging.error('failed saving data to MinIO')
raise exc
logging.debug('saved data to %s', checksum)
return checksum
logging.debug('saved data to %s', object_name)