LoginSignup
4
5

More than 5 years have passed since last update.

Azure FunctionsのHTTPトリガーでBlob Storageにある画像を表示する(Azure Functions v1、Python 3.6.1)

Last updated at Posted at 2019-02-15

Azure FunctionsのHTTPトリガーでBlob Storageにある画像を表示してみましょう。
※Chrome上のAzure Portalの表示がなんか調子悪いのでEdgeでやってます。

Function Appの作成

Azure Portalにログインします。
リソースの作成からFunction Appを作成します。
azure01.PNG

できたら、リソースに移動します。
Pythonでやりたいので、ランタイムバージョンを~1に変更します。
azure04.PNG

カスタム関数を作成します。
azure05.PNG

HTTP triggerを選択し、作成します。
azure07.PNG

Azure FunctionsでPython3を使うを参考にPythonの設定をします。

https://{your-app-name}.scm.azurewebsites.net/DebugConsole

を開きます。{your-app-name}にはFunction Appの名前を入れてください。(例:gachimoto-blob)

下記コマンドでPython3をインストールします。

> cd D:\home\site\tools
> nuget.exe install -Source https://www.siteextensions.net/api/v2/ -OutputDirectory D:\home\site\tools python361x64
> mv /d/home/site/tools/python361x64.3.6.1.3/content/python361x64/* /d/home/site/tools/

pipでazure-storageライブラリをインストールしましょう。

> D:\home\site\tools\python.exe -m pip install azure-storage

azure11.PNG

ストレージアカウントの作成

ストレージアカウントを作成します。
azure10.PNG

デプロイが完了したら、リソースへ移動します。
ストレージアカウント名とkey1をメモります。
azure13.PNG

コンテナの作成

コンテナを作成します。testという名前のコンテナを作成しました。Blobはアクセスできるようにしています。

azure16.PNG
azure17.PNG

画像のアップロード

testコンテナへ、lena.jpgをアップロードしてみました。
azure17.PNG

Blobのアクセスを可能にしているので、URLから見れてしまいます。
例:https://{your-storage-account}.blob.core.windows.net/test/lena.jpg
azure24.PNG

Function Appの関数作成

画像のアップロードができたら、Function Appに戻ります。

HttpTriggerPython31の内容を次のようにします。

import os
import json
from azure.storage.blob import BlockBlobService

account_name='{your-storage-account}'
account_key='{your-storage-account-key}'
container_name='test'

service = BlockBlobService(account_name=account_name,account_key=account_key)
blobs = service.list_blobs(container_name)

files = []
for blob in blobs:
    files.append(blob.name)

def write_http_response(status, body):
    return_dict = {
        "status": status,
        "body": body,
        "headers": {
            "Content-Type": "text/html"
        }
    }
    output = open(os.environ['res'], 'w')
    output.write(json.dumps(return_dict))

write_http_response(200, "<html><h1>" + files[0] + "</h1><img src=" + "\"" + "https://{your-storage-account}.blob.core.windows.net/test/" + files[0] + "\"" + ">" + "</html>")

account_nameaccount_keyには、メモったストレージアカウント名とkey1を入れます。

testコンテナの中にあるBlobのリストを取得し、リストの一番最初のファイルのURLをHTMLタグに入れて返しています。

統合からHTTPメソッドをGETに変更します。
azure22.PNG

保存および実行をクリックし、出力が進捗状況200 OKになれば成功です。

<html><h1>lena.jpg</h1><img src="https://{your-storage-account}.blob.core.windows.net/test/lena.jpg"></html>

関数のURLを取得し、開いてみましょう。
azure25.PNG

lena.jpgが表示されたらOKです。
azure26.PNG

まとめ

  • Azure Functionsを用いて、Blob Storageの画像を表示しました
  • GETでHTMLを返すやつができました
  • 次はクエリ文字列つけたり、見れる人制限したり、SAS使ったりしたいですよね?

参考文献

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5