122 lines
4.3 KiB
Python
122 lines
4.3 KiB
Python
"""
|
|
Azure Blob Storage 업로드 유틸리티
|
|
|
|
Azure Blob Storage에 파일을 업로드하는 비동기 함수들을 제공합니다.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
import aiofiles
|
|
import httpx
|
|
|
|
SAS_TOKEN = "sp=racwdl&st=2025-12-01T00:13:29Z&se=2026-07-31T08:28:29Z&spr=https&sv=2024-11-04&sr=c&sig=7fE2ozVBPu3Gq43%2FZDxEYdEcPLDXyNVfTf16IBasmVQ%3D"
|
|
|
|
|
|
async def upload_music_to_azure_blob(
|
|
file_path: str = "스테이 머뭄_1.mp3",
|
|
url: str = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/dev-user-idx/dev-task-idx/test_my_mp3_should_now_exist.mp3",
|
|
) -> bool:
|
|
"""음악 파일을 Azure Blob Storage에 업로드합니다.
|
|
|
|
Args:
|
|
file_path: 업로드할 파일 경로
|
|
url: Azure Blob Storage URL
|
|
|
|
Returns:
|
|
bool: 업로드 성공 여부
|
|
"""
|
|
access_url = f"{url}?{SAS_TOKEN}"
|
|
headers = {"Content-Type": "audio/mpeg", "x-ms-blob-type": "BlockBlob"}
|
|
|
|
async with aiofiles.open(file_path, "rb") as file:
|
|
file_content = await file.read()
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
response = await client.put(access_url, content=file_content, headers=headers, timeout=120.0)
|
|
|
|
if response.status_code in [200, 201]:
|
|
print(f"[upload_music_to_azure_blob] Success - Status Code: {response.status_code}")
|
|
return True
|
|
else:
|
|
print(f"[upload_music_to_azure_blob] Failed - Status Code: {response.status_code}")
|
|
print(f"[upload_music_to_azure_blob] Response: {response.text}")
|
|
return False
|
|
|
|
|
|
async def upload_video_to_azure_blob(
|
|
file_path: str = "스테이 머뭄.mp4",
|
|
url: str = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/dev-user-idx/dev-task-idx/test_my_mp3_should_now_exist.mp4",
|
|
) -> bool:
|
|
"""영상 파일을 Azure Blob Storage에 업로드합니다.
|
|
|
|
Args:
|
|
file_path: 업로드할 파일 경로
|
|
url: Azure Blob Storage URL
|
|
|
|
Returns:
|
|
bool: 업로드 성공 여부
|
|
"""
|
|
access_url = f"{url}?{SAS_TOKEN}"
|
|
headers = {"Content-Type": "video/mp4", "x-ms-blob-type": "BlockBlob"}
|
|
|
|
async with aiofiles.open(file_path, "rb") as file:
|
|
file_content = await file.read()
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
response = await client.put(access_url, content=file_content, headers=headers, timeout=180.0)
|
|
|
|
if response.status_code in [200, 201]:
|
|
print(f"[upload_video_to_azure_blob] Success - Status Code: {response.status_code}")
|
|
return True
|
|
else:
|
|
print(f"[upload_video_to_azure_blob] Failed - Status Code: {response.status_code}")
|
|
print(f"[upload_video_to_azure_blob] Response: {response.text}")
|
|
return False
|
|
|
|
|
|
async def upload_image_to_azure_blob(
|
|
file_path: str = "스테이 머뭄.png",
|
|
url: str = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/dev-user-idx/dev-task-idx/test_my_mp3_should_now_exist.png",
|
|
) -> bool:
|
|
"""이미지 파일을 Azure Blob Storage에 업로드합니다.
|
|
|
|
Args:
|
|
file_path: 업로드할 파일 경로
|
|
url: Azure Blob Storage URL
|
|
|
|
Returns:
|
|
bool: 업로드 성공 여부
|
|
"""
|
|
access_url = f"{url}?{SAS_TOKEN}"
|
|
extension = Path(file_path).suffix.lower()
|
|
content_types = {
|
|
".jpg": "image/jpeg",
|
|
".jpeg": "image/jpeg",
|
|
".png": "image/png",
|
|
".gif": "image/gif",
|
|
".webp": "image/webp",
|
|
".bmp": "image/bmp",
|
|
}
|
|
content_type = content_types.get(extension, "image/jpeg")
|
|
headers = {"Content-Type": content_type, "x-ms-blob-type": "BlockBlob"}
|
|
|
|
async with aiofiles.open(file_path, "rb") as file:
|
|
file_content = await file.read()
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
response = await client.put(access_url, content=file_content, headers=headers, timeout=60.0)
|
|
|
|
if response.status_code in [200, 201]:
|
|
print(f"[upload_image_to_azure_blob] Success - Status Code: {response.status_code}")
|
|
return True
|
|
else:
|
|
print(f"[upload_image_to_azure_blob] Failed - Status Code: {response.status_code}")
|
|
print(f"[upload_image_to_azure_blob] Response: {response.text}")
|
|
return False
|
|
|
|
|
|
# 사용 예시:
|
|
# import asyncio
|
|
# asyncio.run(upload_video_to_azure_blob())
|
|
# asyncio.run(upload_image_to_azure_blob())
|