FirstCommit_Avatar
This commit is contained in:
45
apps/storage/minio_client.py
Normal file
45
apps/storage/minio_client.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# Kết nối MinIO hoặc S3
|
||||
from minio import Minio
|
||||
import os
|
||||
|
||||
# Tạo connection tới MinIO từ biến môi trường
|
||||
|
||||
# print(dir(Minio)) # Xem phương thức của object
|
||||
# help(Minio)
|
||||
|
||||
def get_minio_client():
|
||||
client = Minio(
|
||||
os.getenv("MINIO_ENDPOINT", "localhost:9000"),
|
||||
access_key=os.getenv("MINIO_ACCESS_KEY", "minioadmin"),
|
||||
secret_key=os.getenv("MINIO_SECRET_KEY", "minioadmin"),
|
||||
secure=False
|
||||
)
|
||||
return client
|
||||
|
||||
|
||||
|
||||
# Phần code dưới này đã xử lý trong crud.py, không cần nữa
|
||||
# Tạo bucket nếu chưa có
|
||||
def create_bucket(bucket_name):
|
||||
client = get_minio_client()
|
||||
found = client.bucket_exists(bucket_name)
|
||||
if not found:
|
||||
client.make_bucket(bucket_name)
|
||||
else:
|
||||
print(f"Bucket {bucket_name} đã tồn tại")
|
||||
|
||||
|
||||
# Fuction Check avatar exists
|
||||
def check_existing_avatar_on_minio(minio_client, user_id):
|
||||
return minio_client.stat_object('user-avatars', f'{user_id}.png')
|
||||
|
||||
# Function Upload avatar lên MinIO
|
||||
def upload_to_minio(minio_client, bucket_name, object_name, file_data, content_type):
|
||||
minio_client.put_object(
|
||||
bucket_name,
|
||||
object_name,
|
||||
file_data,
|
||||
length=-1,
|
||||
part_size=10*1024*1024,
|
||||
content_type=content_type
|
||||
)
|
||||
Reference in New Issue
Block a user