Open16
Blob Storage - EventGrid - Azure Function
まずは環境構築
コマンドラインはエラーが表示されたので、こちらで
環境構築方法
func init LocalFunctionSampleProj --python -m V2
cd LocalFunctionSampleProj
func new
source .venv/bin/activate.fish
deactivate
func new --verbose
SCM_DO_BUILD_DURING_DEPLOYMENT
1
Azurite: Start
一旦OK
python実行環境構築手順 DevContainer編
-
VSCodeへDevContainerをインストール

-
VSCodeのDevContainerを使って環境構築
作業ディレクトリの作成
mkdir py-func
VSCodeを開き、cmd + shift + p から
Add DevContainer Configuration fileを選択

ワークスペースに追加

python3を選択

versionは3.11-bookworm

追加機能はpoetryを選択

.githubファイルを追加

devcontainer.jsonへpoetryのversionを記載
json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bookworm",
"features": {
"ghcr.io/devcontainers-extra/features/poetry:2": {
"version": "1.5.1"
}
}
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
再度、command + shift + p からreopen in Containerを選択

Linux上に開発環境が構築出来る

pythonとpoetryのversionを確認
bash
python --version
poetry --version

これでDevContainerの環境構築は完了です。
環境構築手順 Azure Functions編
VSCodeからAzureの拡張機能をinstall

create functions









/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
source ~/.bashrc # または `~/.zshrc`
brew tap azure/functions
brew install azure-functions-core-tools@4
local.setting.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsSecretStorageType": "Files"
}
}
func start
poetry --version
出てくるものは全てエンター
poetry init
poetry config virtualenvs.in-project true --local
#!/bin/bash
# OpenAI 環境変数
export AZURE_OPENAI_ENDPOINT="https://xxxxxxxxxxxxxxxx-eastus2.cognitiveservices.azure.com/"
export AZURE_OPENAI_API_KEY="xxxxxxxxxxxxxxxx"
export AZURE_OPENAI_API_VERSION="2024-08-01-preview"
export AZURE_OPENAI_CHAT_DEPLOYMENT_NAME="gpt-4o"
export AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME="text-embedding-ada-002"
# Cosmos DB 環境変数
export COSMOSDB_DATABASE_NAME="doc-db"
export COSMOSDB_CONTAINER_NAME="doc-container"
export COSMOSDB_URI="https://xxxxxxxxxxxxxxxx.documents.azure.com:443/"
export COSMOSDB_KEY="xxxxxxxxxxxxxxxx=="
# Azure Storage 環境変数
export BLOB_CONNECTION="DefaultEndpointsProtocol=https;AccountName=xxxxxxxxxxxxxxxx;AccountKey=xxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxx+AStUY32SQ==;EndpointSuffix=core.windows.net"
echo "Environment variables have been set."
chmod +x set_env_variables.sh
source set_env_variables.sh
pip install azure-functions
pip install openai
pip install azure-cosmos
pip install azure-storage-blob
pip install pymupdf
pip install pillow
pip install pydantic
環境構築完了🚀
# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues
azure-functions
openai
azure-cosmos
azure-storage-blob
pymupdf
pillow
pydantic
pip uninstall PyMuPDF
pip install PyMuPDF
pip install -r requirements.txt
でもOK
Debug方法
RestClientをVSCodeへインストール

test.http ファイルを作成し、Blobへtest.pdfをセットしておく
POST http://localhost:7071/runtime/webhooks/eventgrid?functionName=EventGridTrigger
Content-Type: application/json
aeg-event-type: Notification
{
"topic": "/subscriptions/eeeeeee-eeee-cbcb-cccc-ddddddddddd/resourceGroups/eg0122/providers/Microsoft.Storage/storageAccounts/egblobstore",
"subject": "/blobServices/default/containers/{containername}/blobs/blobname.jpg",
"eventType": "Microsoft.Storage.BlobCreated",
"eventTime": "2018-01-23T17:02:19.6069787Z",
"id": "{guid}",
"data": {
"api": "PutBlockList",
"clientRequestId": "{guid}",
"requestId": "{guid}",
"eTag": "0x8D562831044DDD0",
"contentType": "application/octet-stream",
"contentLength": 2248,
"blobType": "BlockBlob",
"url": "https://pocsatablobstorage.blob.core.windows.net/pocsatacontainer/test.pdf",
"sequencer": "000000000000272D000000000003D60F",
"storageDiagnostics": {
"batchId": "{guid}"
}
},
"dataVersion": "",
"metadataVersion": "1"
}
VSCodeからRestClientを実行して実施
Deploy方法