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.""" """Definition of put function."""
from __future__ import annotations
import logging import logging
import os
from hashlib import md5
from io import BytesIO from io import BytesIO
from minio import Minio from minio import Minio
@@ -13,14 +9,16 @@ from minio import Minio
def put( def put(
client: Minio, client: Minio,
buffer: BytesIO, buffer: BytesIO,
) -> str: bucket_name: str,
object_name: str,
) -> None:
"""Put buffer in bucket in MinIO and return MD5 checksum as object name.""" """Put buffer in bucket in MinIO and return MD5 checksum as object name."""
assert isinstance(client, Minio) assert isinstance(client, Minio)
assert isinstance(buffer, BytesIO) assert isinstance(buffer, BytesIO)
bucket_name = os.getenv('MINIO_BUCKET_NAME', default='') assert isinstance(bucket_name, str)
assert len(bucket_name) > 0 assert len(bucket_name) > 0
# get md5 of image assert isinstance(object_name, str)
checksum = md5(buffer.getbuffer()).hexdigest() assert len(object_name) > 0
# prepare for saving # prepare for saving
num_bytes = buffer.tell() num_bytes = buffer.tell()
buffer.seek(0) buffer.seek(0)
@@ -28,12 +26,11 @@ def put(
try: try:
client.put_object( client.put_object(
bucket_name=bucket_name, bucket_name=bucket_name,
object_name=checksum, object_name=object_name,
length=num_bytes, length=num_bytes,
data=buffer, data=buffer,
) )
except Exception as exc: except Exception as exc:
logging.error('failed saving data to MinIO') logging.error('failed saving data to MinIO')
raise exc raise exc
logging.debug('saved data to %s', checksum) logging.debug('saved data to %s', object_name)
return checksum