Open23

Azure

forest1forest1

REST APIのテストクライアント
VSCodeの拡張機能で、REST Client

テキストファイルで、言語指定httpにて作成する。
構文:

###
Send Request
GET http://localhost:7071/api/Function1?name=test1

###
Send Request
POST http://localhost:7071/api/Function1
content-type:application/json
{
	"name":"posttest"
}

"Send Request"はGET,POST行を書くと自動で表示される。
この Send Request をクリックすることでサーバーにHTTP送信される。

forest1forest1

Azure Functions
.NET Framework 4.8でも作成可能
デバッグ実行でコンソールが表示されて、そこに表示されるURIになんらかのRESTクライアントからメッセージを送ることでブレークポイントで停止したりなどデバッグ可能。

forest1forest1
forest1forest1

Microsoft.Azure.WebJobs.Extentions.Storage
v5.*系の場合

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Azure.Storage.Blobs;

namespace FunctionBlob2
{
    public static class Function1
    {
        [FunctionName("Function2")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [Blob("myblob", FileAccess.Write)] BlobContainerClient blobContainer,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            await blobContainer.CreateIfNotExistsAsync();
            await blobContainer.UploadBlobAsync("Test1Dir/data.json", req.Body);

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

上記を実行すると、Azure Storage Explorer より、

そして、Test1Dir/ 以下に、

data.json

が保存される。
data.jsonの中身はこの場合、POSTのBody

forest1forest1

もし同じストレージに別コンテナを作りたい場合、
Run()の引数に新たに追加する

        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [Blob("myblob", FileAccess.Write)] BlobContainerClient blobContainer,
            [Blob("myblob2", FileAccess.Write)] BlobContainerClient blobContainer2,
            ILogger log)
forest1forest1

BlobからFilesにデータをコピーする方法調査中
Windowsクライアント環境で、Explorerから普通にファイルサーバーの共有フォルダにアクセスするような感じのことがしたい。

forest1forest1

Storage Emulatorを最新に更新する必要あり

forest1forest1

APIキー
Azureの関数からURLを取得するとクエリパラメータに code= で指定済
もしくは、クエリパラメータからは削ってHTTPヘッダで指定する。
x-functions-key:~

この時のAPIキー(アプリキー)は以下から取得する。利用者向けはdefaultの文字列を使う。

forest1forest1

関数のAPIキーをFunctionで指定している場合にAPIキー指定必須。
指定せずに呼び出すと、HTTPエラー 401 (認可エラー)が返される。

forest1forest1

Functionのレスポンス制御をしたい場合、
引数の HttpRequest を HttpRequestMessageに、
戻り値の IAsyncResult を HttpResponseMessage に変える。
応答は以下で設定する。
var response = req.CreateResponse();
response.Content = new ByteArrayContent(...);
response.Content.Headers.ContentType = ...

forest1forest1

デフォルトの return new OkObjectResult(...) だとjsonだけになってしまうので。

forest1forest1

Azure 標準化ガイドライン

Azure 標準化ガイドラインとは

Azure 標準化ガイドライン (以下、本書と記載) とは、Microsoft Azure を利用するにあたり、具体的なプラットフォーム/サービス設計の指針となるテンプレートを提供することを目的に作成されたものです。本書は、可能な限り 「ベスト プラクティス」 としての実例をベースに記載していますが、本書を利用する組織の体制や構築するシステム要件に応じてテーラリングして利用されることを前提としています。
本書を初めて利用する際は、「0 章 はじめに 1. Azure 標準化ガイドラインとは」をご参照の上、活用ください。
https://github.com/Azure/Azure-standardization-guideline