Skip to content
This repository has been archived by the owner on Jan 5, 2025. It is now read-only.

adding external storage - minio #184

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from .upload_stragegy_interface import UploadStrategy
import os
from uuid import uuid4

class LocalUploadStrategy(UploadStrategy):

def __init__(self, upload_dir='uploads'):
self.upload_dir = upload_dir

def upload_files(self, files):
uploaded_files = []
for file in files:
file_name = file.name
file_ext = os.path.splitext(file_name)[1]

# Generate unique file name
unique_name = str(uuid4()) + file_ext
file_path = os.path.join(self.upload_dir, unique_name)

# Create upload dir if doesn't exist
if not os.path.exists(self.upload_dir):
os.makedirs(self.upload_dir)

# Save file
with open(file_path, 'wb+') as f:
for chunk in file.chunks():
f.write(chunk)

uploaded_file = {'name': file_name, 'path': file_path}
uploaded_files.append(uploaded_file)

return uploaded_files
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from minio import Minio
from minio.error import ResponseError
from .upload_stragegy_interface import UploadStrategy
from uuid import uuid4
import os

class MinioUploadStrategy(UploadStrategy):

def __init__(self, endpoint, access_key, secret_key, bucket_name):
self.minio_client = Minio(endpoint, access_key=access_key, secret_key=secret_key, secure=True)
self.bucket_name = bucket_name

def upload_files(self, files):
uploaded_files = []
try:
for file in files:
file_name = file.name
file_ext = os.path.splitext(file_name)[1]

# Generate unique file name
unique_name = str(uuid4()) + file_ext

# Upload file
self.minio_client.fput_object(self.bucket_name, unique_name, file)

uploaded_file = {'name': file_name, 'path': unique_name}
uploaded_files.append(uploaded_file)

except ResponseError as err:
print(err)

return uploaded_files
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from abc import ABC, abstractmethod

class UploadStrategy(ABC):

@abstractmethod
def upload_files(self, files):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from api.utils.strategies.upload.local_upload_strategy import LocalUploadStrategy
from api.utils.strategies.upload.minio_upload_strategy import MinioUploadStrategy

class UploadStrategyFactory:

@staticmethod
def get_strategy(strategy_type):
if strategy_type == 'local':
return LocalUploadStrategy()
elif strategy_type == 'minio':
return MinioUploadStrategy()
else:
raise ValueError(f'Invalid strategy {strategy_type}')
15 changes: 15 additions & 0 deletions dj_backend_server/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ services:
command: celery -A dj_backend_server worker --loglevel=info
networks:
- openchat_network

minio:
image: minio/minio
container_name: minio
environment:
MINIO_ACCESS_KEY: your-access-key
MINIO_SECRET_KEY: your-secret-key
ports:
- "9000:9000"
volumes:
- minio-data:/data
command: server /data
networks:
- openchat_network


redis:
image: redis:latest
Expand Down