from minio import Minio
import os
from typing import Optional
from glob import glob
import pathlib
from sqlmodel import Field, Session, SQLModel, create_engine

def get_images(folder:str="../cls_labeling/public/images"):
    return glob(str(pathlib.Path(folder,"**","*.jpg")), recursive=True)


class Image(SQLModel, table=True):
    key: Optional[int] = Field(default=None, primary_key=True)
    image_name: str
    label: str
    image_url: str


if __name__ == "__main__":
    engine = create_engine("sqlite:///image.db")
    client = Minio(
        "localhost:9001",
        secure=False,
        access_key="<KEY>",
        secret_key="<KEY>"
    )

    bucket_found = client.bucket_exists("image")
    if not bucket_found:
        client.make_bucket("image")
    else:
        for obj in client.list_objects("image"):
            client.remove_object("image", obj.object_name)
        client.remove_bucket("image")
        client.make_bucket("image")
        os.remove("./image.db")

    SQLModel.metadata.create_all(engine)

    images = []

    for i, image in enumerate(get_images()):
        print(pathlib.Path(image).stem, image)
        image_name = pathlib.Path(image).stem+'.jpg'
        client.fput_object(
            "image", image_name, image
        )

        image_url = f"http://localhost:9001/image/{image_name}"

        images.append(
            Image(key=i, image_name=pathlib.Path(image).stem, label="", image_url=image_url)
        )

    with Session(engine) as session:
        for data in images: session.add(data)
        session.commit()
minio.Minio sqlmodel.SQLModel.metadata.create_all pathlib.Path os.remove sqlmodel.Session sqlmodel.Field sqlmodel.create_engine
[((355, 392), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (360, 392), False, 'from sqlmodel import Field, Session, SQLModel, create_engine\n'), ((489, 524), 'sqlmodel.create_engine', 'create_engine', (['"""sqlite:///image.db"""'], {}), "('sqlite:///image.db')\n", (502, 524), False, 'from sqlmodel import Field, Session, SQLModel, create_engine\n'), ((538, 615), 'minio.Minio', 'Minio', (['"""localhost:9001"""'], {'secure': '(False)', 'access_key': '"""<KEY>"""', 'secret_key': '"""<KEY>"""'}), "('localhost:9001', secure=False, access_key='<KEY>', secret_key='<KEY>')\n", (543, 615), False, 'from minio import Minio\n'), ((994, 1030), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (1022, 1030), False, 'from sqlmodel import Field, Session, SQLModel, create_engine\n'), ((965, 988), 'os.remove', 'os.remove', (['"""./image.db"""'], {}), "('./image.db')\n", (974, 988), False, 'import os\n'), ((1473, 1488), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1480, 1488), False, 'from sqlmodel import Field, Session, SQLModel, create_engine\n'), ((241, 276), 'pathlib.Path', 'pathlib.Path', (['folder', '"""**"""', '"""*.jpg"""'], {}), "(folder, '**', '*.jpg')\n", (253, 276), False, 'import pathlib\n'), ((1108, 1127), 'pathlib.Path', 'pathlib.Path', (['image'], {}), '(image)\n', (1120, 1127), False, 'import pathlib\n'), ((1162, 1181), 'pathlib.Path', 'pathlib.Path', (['image'], {}), '(image)\n', (1174, 1181), False, 'import pathlib\n'), ((1396, 1415), 'pathlib.Path', 'pathlib.Path', (['image'], {}), '(image)\n', (1408, 1415), False, 'import pathlib\n')]