Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azure blob storageのコンテナ操作の実装 #169

Merged
merged 20 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 125 additions & 11 deletions api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ openai = "^1.35.10"
azure-storage-blob = "^12.21.0"
sqlalchemy = "^2.0.35"
aiomysql = "^0.2.0"
azure-identity = "^1.18.0"


[tool.poetry.group.dev.dependencies]
Expand Down
22 changes: 19 additions & 3 deletions api/v2/cruds/user.py
shun-harutaro marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from azure.storage.blob import BlobServiceClient
from openai import OpenAI
from sqlalchemy import select
from sqlalchemy.engine import Result
Expand All @@ -11,16 +12,22 @@


async def create_user(
db: AsyncSession, user_create: user_schema.UserCreate
db: AsyncSession,
user_create: user_schema.UserCreate,
blob_service_client: BlobServiceClient,
) -> user_model.User:
# 引数にスキーマuser_create: user_schema.UserCreateを受け取りDBモデルのuser_model.Userに変換する
user = user_model.User(**user_create.model_dump())
thread = client.beta.threads.create()
user.thread_id = thread.id

db.add(user)
await db.commit()
await db.refresh(user)

# azure-blob-storageにユーザのコンテナーを作成する
container_name = "user" + str(user.id)
blob_service_client.create_container(container_name)

return user


Expand Down Expand Up @@ -58,7 +65,16 @@ async def update_user(
return original


async def delete_user(db: AsyncSession, original: user_model.User) -> None:
async def delete_user(
db: AsyncSession, original: user_model.User, blob_service_client: BlobServiceClient
) -> None:
# azure blob storageのコンテナを削除する
container_name = "user" + str(original.id)
container_client = blob_service_client.get_container_client(
container=container_name
)
container_client.delete_container()

await db.delete(original)
await db.commit()

Expand Down
12 changes: 10 additions & 2 deletions api/v2/routers/user.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
from typing import List

from azure.storage.blob import BlobServiceClient
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession

import v2.cruds.user as user_crud
import v2.schemas.user as user_schema
from db import get_db
from v2.utils.config import get_azure_sas_token, get_azure_storage_account
from v2.utils.logging import get_logger

router = APIRouter()
logger = get_logger()

# azureの認証
azure_storage_account = get_azure_storage_account()
account_url = f"https://{azure_storage_account}.blob.core.windows.net"
sas_token = get_azure_sas_token()
blob_service_client = BlobServiceClient(account_url, credential=sas_token)


@router.get(
"/",
Expand All @@ -29,7 +37,7 @@ async def list_users(db: AsyncSession = Depends(get_db)):
response_model=user_schema.UserResponse,
)
async def create_user(user: user_schema.UserCreate, db: AsyncSession = Depends(get_db)):
return await user_crud.create_user(db, user)
return await user_crud.create_user(db, user, blob_service_client)


@router.put(
Expand All @@ -55,4 +63,4 @@ async def delete_user(id: int, db: AsyncSession = Depends(get_db)):
user = await user_crud.get_user(db, user_id=id)
if user is None:
raise HTTPException(status_code=404, detail="User not found")
return await user_crud.delete_user(db, original=user)
return await user_crud.delete_user(db, user, blob_service_client)
8 changes: 8 additions & 0 deletions api/v2/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,13 @@ def get_db_object() -> Dict[str, str]:
return obj


def get_azure_storage_account():
return os.getenv("AZURE_STORAGE_ACCOUNT")


def get_azure_sas_token():
return os.getenv("AZURE_SAS_TOKEN")
shun-harutaro marked this conversation as resolved.
Show resolved Hide resolved


def get_db_cert_path():
return os.getenv("DB_CERT_PATH")
Loading