butler_ddochi/obs_minio/views.py

48 lines
1.5 KiB
Python

import uuid
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from minio import Minio
from minio.error import S3Error
import urllib3
# MinIO 설정
MINIO_ENDPOINT = 'minio.icurfer.com:9000'
MINIO_ACCESS_KEY = 'h5gOXcQieSE0kReVlpDa'
MINIO_SECRET_KEY = '2S5vtc7DtrnUjqsAO6CF3kPVEqDtqmHgnt3OPIPY'
BUCKET_NAME = 'butler-ddochi-image'
@csrf_exempt
def upload_image(request):
print("이미지업로드 동작시작")
if request.method == 'POST' and 'image' in request.FILES:
image = request.FILES['image']
unique_filename = f"uploads/{uuid.uuid4()}_{image.name}"
# MinIO 클라이언트 생성
client = Minio(
MINIO_ENDPOINT,
access_key=MINIO_ACCESS_KEY,
secret_key=MINIO_SECRET_KEY,
secure=True,
http_client=urllib3.PoolManager(cert_reqs='CERT_NONE') # SSL 검증 비활성화
)
try:
# MinIO에 파일 업로드
client.put_object(
bucket_name=BUCKET_NAME,
object_name=unique_filename,
data=image,
length=image.size,
content_type=image.content_type
)
# Presigned URL 생성
presigned_url = client.presigned_get_object(BUCKET_NAME, unique_filename)
return JsonResponse({"url": presigned_url}, status=201)
except S3Error as e:
return JsonResponse({"error": str(e)}, status=500)
return JsonResponse({"error": "Invalid request"}, status=400)