はじめに
Azure FunctionsがPythonランタイムに対応しています。
ただ、ストレージのへの入力をトリガー→Functions処理→ストレージへの出力、を行う方法がなかなか見つけられなかったので備忘録を兼ねてメモします。
手順
※ Functionsリソースの作成等は終わっている前提です。
1.バインドするAzureストレージの情報を記載します。
local.settings.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=<ストレージアカウント>;AccountKey=<アカウントキー>;EndpointSuffix=core.windows.net"
}
}
2.入力元と出力先のパスを記載します。
function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "inputblob",
"type": "blobTrigger",
"direction": "in",
"path": "container/input/{name}",
"connection": ""
},
{
"name": "outputblob",
"type": "blob",
"direction": "out",
"path": "container/output/{name}.csv",
"connection": ""
}
]
}
3.Pythonスクリプト内に処理を書きます。
__init__.py
import logging
import azure.functions as func
def main(inputblob: func.InputStream, outputblob: func.Out[str]):
logging.info(f"Python blob trigger function processed blob. v2.0\n"
f"Name: {inputblob.name}\n"
f"Blob Size: {inputblob.length} bytes\n")
input_text = inputblob.read(size=-1).decode("utf-8")
# 行いたい処理
output_text = input_text += "hoge"
outputblob.set(output_text)
これで2.で設定したpath "container/output/{name}.csv"へファイルが出力されます。ファイル名はこの場合だと入力ファイル名の末尾に".csv"が付いた形となります。