Azure Functions(TypeScript)でAzurite上のBlobにアクセスする。

前提

・Azuriteがインストール済み
・Microsoft Azure Storage Explorer(確認のためなのでなくても可ですが、説明で使用)

内容

こちらをテンプレートとして使います。

https://github.com/xiaotiantakumi/az-func-ts-starter

こちらを参考に進めました。

https://docs.microsoft.com/en-us/samples/azure/azure-sdk-for-js/storage-blob-typescript/

この中のconnectionStringAuth.tsが対象となります。

SampleTrigger/index.tsを以下のように変更する。
以下のコードのについては別で読み進めたい。

import { AzureFunction, Context, HttpRequest } from "@azure/functions"; 
import { BlobServiceClient } from "@azure/storage-blob"; 
const httpTrigger: AzureFunction = async function ( 
  context: Context, 
  req: HttpRequest 
): Promise<void> { 
  // Create Blob Service Client from Account connection string or SAS connection string 
  // Account connection string example - `DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=accountKey;EndpointSuffix=core.windows.net` 
  // SAS connection string example - `BlobEndpoint=https://myaccount.blob.core.windows.net/;QueueEndpoint=https://myaccount.queue.core.windows.net/;FileEndpoint=https://myaccount.file.core.windows.net/;TableEndpoint=https://myaccount.table.core.windows.net/;SharedAccessSignature=sasString` 
  const STORAGE_CONNECTION_STRING = process.env.STORAGE_CONNECTION_STRING || ""; 
  // Note - Account connection string can only be used in node. 
  const blobServiceClient = BlobServiceClient.fromConnectionString( 
    STORAGE_CONNECTION_STRING 
  ); 
  let i = 1; 
  for await (const container of blobServiceClient.listContainers()) { 
    console.log(`Container ${i++}: ${container.name}`); 
  } 
  // Create a container 
  const containerName = `newcontainer${new Date().getTime()}`; 
  const containerClient = blobServiceClient.getContainerClient(containerName); 
  const createContainerResponse = await containerClient.create(); 
  console.log( 
    `Create container ${containerName} successfully`, 
    createContainerResponse.requestId 
  ); 
  // Delete container 
  await containerClient.delete(); 
  console.log("deleted container"); 
}; 
export default httpTrigger;

local.settings.jsonにSTORAGE_CONNECTION_STRINGを追加する。

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsStorage": "",
    "STORAGE_CONNECTION_STRING":"AccountName=devstoreaccount1;〜〜;DefaultEndpointsProtocol=http;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;"
  }
}

STORAGE_CONNECTION_STRINGの値は以下から取得できます。

動作確認

実際にHTTPリクエストを投げてみると以下のようにコンソールにコンテナーの内容が表示されました。

[start:*func] [2022-07-25T00:17:08.588Z] Container 1: azure-webjobs-hosts
[start:*func] [2022-07-25T00:17:08.588Z] Container 2: azure-webjobs-secrets
[start:*func] [2022-07-25T00:17:08.588Z] Container 3: excel-container
[start:*func] [2022-07-25T00:17:08.588Z] Container 4: newcontainer1658277761179
[start:*func] [2022-07-25T00:17:08.588Z] Container 5: newcontainer1658282166193
[start:*func] [2022-07-25T00:17:08.589Z] Container 6: newcontainer1658293007614
[start:*func] [2022-07-25T00:17:08.589Z] Container 7: newcontainer1658295155183
[start:*func] [2022-07-25T00:17:08.595Z] Create container newcontainer1658708228589 successfully 06ef0d7c-fcd5-474f-975d-2506c209befd
[start:*func] [2022-07-25T00:17:08.663Z] Executed 'Functions.BlobSampleTrigger' (Succeeded, Id=a6437882-15f3-4fb6-b3ad-60b1ba229add, Duration=167ms)

Azure ストレージエクスプロラーでも確認しました。