39 lines
1001 B
Python
39 lines
1001 B
Python
"""Definition of put function."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
from hashlib import md5
|
|
from io import BytesIO
|
|
|
|
from minio import Minio
|
|
|
|
|
|
def put(
|
|
client: Minio,
|
|
buffer: BytesIO,
|
|
) -> str:
|
|
"""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=None)
|
|
assert isinstance(bucket_name, str)
|
|
# get md5 of image
|
|
checksum = md5(buffer.getbuffer()).hexdigest()
|
|
# prepare for saving
|
|
num_bytes = buffer.tell()
|
|
buffer.seek(0)
|
|
# send data to bucket
|
|
try:
|
|
client.put_object(
|
|
bucket_name=bucket_name,
|
|
object_name=checksum,
|
|
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
|