Commit b4c67bdd authored by Grant, Josh's avatar Grant, Josh
Browse files

Merge branch 'feature/minio' into 'develop'

Feature/minio to develop

See merge request !39
parents 2829e190 cb43971b
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ verify_ssl = true
name = "pypi"

[packages]
common-package = {editable = true, path = "./src"}
common-package = {editable = true, path = "./src", extras = ["dev", "minio"]}
twine = "*"

[dev-packages]
@@ -15,6 +15,7 @@ flake8 = "*"
pylint = "*"
tox = "*"
twine = "*"
moto = {extras = ["all"], version = "*"}

[scripts]
lint = "tox -e lint"
+1492 −276

File changed.

Preview size limit exceeded, changes collapsed.

+56 −0
Original line number Diff line number Diff line
"""Provides a Minio Mixin"""


from minio import Minio
from minio.error import S3Error

from common.env import check_environment as ce
from common.logz import create_logger


class MinioMixin:
    """Provides a wrapper around the Minio client for uploading and retrieving files.

    Attributes:
        minio: A Minio client instance.
        bucket_name: The name of the Minio bucket, defaults to the os environment variable "MINIO_BUCKET_NAME".
        logger: A logger instance, used for logging messages and is defined in the `common.logz` module.
    """
    def __init__(self, bucket_name: str = None):
        self.minio: Minio = self.get_minio_client()
        self.bucket_name = bucket_name or ce("MINIO_BUCKET_NAME")
        self.logger = create_logger()

    @staticmethod
    def get_minio_client():
        """Create a Minio client instance
        Uses the os environment variables of MINIO_ENDPOINT, MINIO_ACCESS_KEY, and MINIO_SECRET_KEY to create the client.
        :return: A Minio client instance.
        """
        return Minio(
            endpoint=ce('MINIO_ENDPOINT'),
            access_key=ce('MINIO_ACCESS_KEY'),
            secret_key=ce("MINIO_SECRET_KEY"),
        )

    def upload_file(self, file_path: str, object_name: str):
        """Upload a file to Minio.
        :param file_path: The path to the file to be uploaded.
        :param object_name: The name of the object to be created in Minio.
        :return: None
        """
        try:
            self.minio.fput_object(self.bucket_name, object_name, file_path)
        except S3Error as exc:
            self.logger.info(f"Error uploading file to Minio: {exc}")

    def retrieve_file(self, object_name: str, file_path: str):
        """Retrieve a file from Minio.
        :param object_name: The name of the object to be retrieved from Minio.
        :param file_path: The path to save the retrieved file.
        :return: None
        """
        try:
            self.minio.fget_object(self.bucket_name, object_name, file_path)
        except S3Error as exc:
            self.logger.info(f"Error retrieving file from Minio: {exc}")
+11 −1
Original line number Diff line number Diff line
@@ -55,6 +55,9 @@ scrapers = [
    "beautifulsoup4~=4.10.0",
    "websocket~=0.2.1"
]
minio = [
    "minio~=7.1.0"
]
all = [
    "psycopg2-binary==2.9.9",
    "pymssql==2.2.11",
@@ -67,7 +70,14 @@ all = [
    "beautifulsoup4~=4.10.0",
    "websocket~=0.2.1",
    "ldap3==2.9.1",
    "flask>=2.0.2"
    "flask>=2.0.2",
    "minio~=7.1.0",
]
dev = [
    "ruff==0.5.4",
    "pytest==8.3.1",
    "pytest-cov==5.0.0",
    "moto==5.0.11",
]


tests/data/test.txt

0 → 100644
+1 −0
Original line number Diff line number Diff line
content
 No newline at end of file
Loading