finished upload images

This commit is contained in:
bluebamus committed 2025-12-26 13:27:39 +09:00
1 parent 03a80afc71
commit 6917a76d60
14 files changed
+1359 -208

No files matched your search

+96 -39
View File
@@ -1,39 +1,92 @@
import requests
"""
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"
def upload_music_to_azure_blob(file_path = "스테이 머뭄_1.mp3", url = "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"):
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"
}
with open(file_path, "rb") as file:
response = requests.put(access_url, data=file, headers=headers)
if response.status_code in [200, 201]:
print(f"Success Status Code: {response.status_code}")
else:
print(f"Failed Status Code: {response.status_code}")
print(f"Response: {response.text}")
headers = {"Content-Type": "audio/mpeg", "x-ms-blob-type": "BlockBlob"}
def upload_video_to_azure_blob(file_path = "스테이 머뭄.mp4", url = "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"):
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"
}
with open(file_path, "rb") as file:
response = requests.put(access_url, data=file, headers=headers)
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"Success Status Code: {response.status_code}")
print(f"[upload_video_to_azure_blob] Success - Status Code: {response.status_code}")
return True
else:
print(f"Failed Status Code: {response.status_code}")
print(f"Response: {response.text}")
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
def upload_image_to_azure_blob(file_path = "스테이 머뭄.png", url = "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"):
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 = {
@@ -42,23 +95,27 @@ def upload_image_to_azure_blob(file_path = "스테이 머뭄.png", url = "https:
".png": "image/png",
".gif": "image/gif",
".webp": "image/webp",
".bmp": "image/bmp"
".bmp": "image/bmp",
}
content_type = content_types.get(extension, "image/jpeg")
headers = {
"Content-Type": content_type,
"x-ms-blob-type": "BlockBlob"
}
with open(file_path, "rb") as file:
response = requests.put(access_url, data=file, headers=headers)
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"Success Status Code: {response.status_code}")
print(f"[upload_image_to_azure_blob] Success - Status Code: {response.status_code}")
return True
else:
print(f"Failed Status Code: {response.status_code}")
print(f"Response: {response.text}")
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
upload_video_to_azure_blob()
upload_image_to_azure_blob()
# 사용 예시:
# import asyncio
# asyncio.run(upload_video_to_azure_blob())
# asyncio.run(upload_image_to_azure_blob())