Skip to content

Commit

Permalink
azure blob storageのコンテナ操作の実装 (#169)
Browse files Browse the repository at this point in the history
* post

* post

* formatted

* reviewed

* get

* completed api

* completed api for couples table

* done issue #149

* modified after review

* get user by raspi_id

* the main code couldn't be pushed at last commit

* azure error

* create and delete container on azure blob storage

* lockファイル再作成

* modified after review

* run poetry lock to update dependencies

* 環境変数を揃えた

---------

Co-authored-by: shun-harutaro <shun_harutaro@protonmail.com>
Co-authored-by: NOZAKI Shuntaro <60352276+shun-harutaro@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 4, 2024
1 parent c62f8c6 commit 8e916f8
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
echo "VOICEVOX_API_KEY=${{ secrets.VOICEVOX_API_KEY }}" >> .env
echo "OPENAI_ASSISTANT_ID=${{ vars.OPENAI_ASSISTANT_ID }}" >> .env
echo "OPENAI_THREAD_ID =${{ vars.OPENAI_THREAD_ID }}" >> .env
echo "AZURE_SAS_TOKEN=${{ secrets.AZURE_SAS_TOKEN }}" >> .env
echo "AZURE_STORAGE_ACCOUNT=${{ vars.AZURE_STORAGE_ACCOUNT }}" >> .env
- name: Run docker-compose Build
run: docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build -d
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ echo "VOICEVOX_API_KEY=[voicevox api key]" >> .env
echo "OPENAI_API_KEY=[openAI api key]" >> .env
echo "OPENAI_ASSISTANT_ID=[openAI assistant id]" >> .env
echo "OPENAI_THREAD_ID=[openAI thread id]" >> .env
echo "AZURE_STORAGE_ACCOUNT=[azure storage-account name]" >> .env
echo "AZURE_SAS_TOKEN=[azure storage-account SAS token]" >> .env
```
`VOICEVOX_API_KEY`https://su-shiki.com/api/ から、
`OPENAI_API_KEY`https://platform.openai.com/docs/overview から取得します
- `VOICEVOX_API_KEY` は https://su-shiki.com/api/ から、
- `OPENAI_API_KEY` は https://platform.openai.com/docs/overview から
- `OPENAI_API_KEY` は https://platform.openai.com/docs/overview から

3. Dockerイメージのビルド
```
Expand Down
138 changes: 126 additions & 12 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
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)
10 changes: 10 additions & 0 deletions api/v2/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def check_env_variables():
"IS_DEV_MODE",
"OPENAI_API_KEY",
"VOICEVOX_API_KEY",
"AZURE_SAS_TOKEN",
"AZURE_STORAGE_ACCOUNT",
]
env_vars_prod: list[str] = [
# 本番環境のみで使う環境変数
Expand Down Expand Up @@ -49,5 +51,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")


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

0 comments on commit 8e916f8

Please sign in to comment.