Device
This commit is contained in:
49
backend/storage/s3_client.py
Normal file
49
backend/storage/s3_client.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
import boto3
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
def get_s3_client():
|
||||
"""
|
||||
Tạo và trả về S3 client dùng để làm việc với MinIO/S3.
|
||||
|
||||
MinIO tương thích với S3 API, nên mình dùng boto3 như khi làm với AWS S3.
|
||||
Hàm này chỉ chịu trách nhiệm tạo kết nối, không xử lý upload hay nghiệp vụ file.
|
||||
"""
|
||||
|
||||
return boto3.client(
|
||||
"s3",
|
||||
endpoint_url=os.getenv("S3_ENDPOINT_URL"),
|
||||
aws_access_key_id=os.getenv("S3_ACCESS_KEY"),
|
||||
aws_secret_access_key=os.getenv("S3_SECRET_KEY"),
|
||||
region_name=os.getenv("S3_REGION", "us-east-1"),
|
||||
)
|
||||
|
||||
|
||||
def get_bucket_name():
|
||||
"""
|
||||
Lấy tên bucket từ biến môi trường.
|
||||
|
||||
Project này nên dùng một bucket chung, ví dụ: ndms.
|
||||
Sau đó phân loại file bằng object key:
|
||||
- device-types/router-uuid.svg
|
||||
- devices/avatar-uuid.png
|
||||
"""
|
||||
|
||||
return os.getenv("S3_BUCKET_NAME")
|
||||
|
||||
|
||||
def get_public_url():
|
||||
"""
|
||||
Lấy public URL của MinIO/S3.
|
||||
|
||||
Ví dụ khi chạy local:
|
||||
S3_PUBLIC_URL=http://localhost:9000
|
||||
|
||||
URL cuối cùng sẽ có dạng:
|
||||
http://localhost:9000/ndms/device-types/router-uuid.svg
|
||||
"""
|
||||
|
||||
return os.getenv("S3_PUBLIC_URL")
|
||||
Reference in New Issue
Block a user