LoginSignup
0
0

More than 3 years have passed since last update.

AzureのWebAppsでAzure Blob StorageのjsonをAPI的に返却

Posted at

WebAppsでBlobストレージのデータにアクセスしてデータを返すメモ

ストレージへのアクセスをTASK化

static async Task<string> GetJsonData()
{
    //storageAccountの作成(接続情報の定義)
    var accountName = "ストレージのアカウント名";
    var accessKey = "accessKey文字列";
    var credential = new StorageCredentials(accountName, accessKey);
    var storageAccount = new CloudStorageAccount(credential, true);
    //blob
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    //container
    CloudBlobContainer container = blobClient.GetContainerReference("demo");
    //data
    CloudBlockBlob blockBlob_download = container.GetBlockBlobReference("demo.json");
    string text;
    using (var memoryStream = new MemoryStream())
    {
        await blockBlob_download.DownloadToStreamAsync(memoryStream);
        text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
    }
    return text;
}

HttpGetの処理

[HttpGet]
public ActionResult<string> Get()
{
    var task = GetJsonData();
    var result = task.Result;
    return result;
}

参考サイト

Azure Storage (blob)を使う
https://qiita.com/zaburo/items/ed2f5ee1224ab40bd7b1

Azure Blob Storage と Visual Studio 接続済みサービスの概要 (ASP.NET)
https://github.com/MicrosoftDocs/azure-docs.ja-jp/blob/master/articles/visual-studio/vs-storage-aspnet-getting-started-blobs.md

ASP.NET で Azure Blob Storage を操作してみる
https://qiita.com/komiyasa/items/f69dc1ab773bfc3115bd

0
0
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
0
0