通过 C# Azure 函数生成 Blob 容器

Posted

技术标签:

【中文标题】通过 C# Azure 函数生成 Blob 容器【英文标题】:Generate a Blob Container by C# Azure Function 【发布时间】:2021-10-08 06:31:35 【问题描述】:

我想创建一个使用 HTTP 触发器的函数,当它被 HTTP 信号触发时,它将在 Blob 存储中创建一个容器。

【问题讨论】:

【参考方案1】:

在visual studio中创建一个Http Trigger并使用代码创建容器

这里是使用 Http Trigger 创建容器的代码

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 Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Extensions.Configuration;
namespace AzFunctions

public  static  class UploadBlobHttpTriggerFunc
    
        [FunctionName("UploadBlobHttpTriggerFunc")]
        public  static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log, ExecutionContext context)
        
        log.LogInformation($"C# Http trigger function executed at: DateTime.Now");
       CreateContainerIfNotExists(log, context);

        CloudStorageAccount storageAccount = GetCloudStorageAccount(context);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference("dummy-messages");

        for (int i = 1 ; i <= 5; i++)
        
        string randomStr = Guid.NewGuid().ToString();
        CloudBlockBlob blob = container.GetBlockBlobReference(randomStr);

        var serializeJesonObject = JsonConvert.SerializeObject(new  ID = randomStr, Content = $"<html><body><h2> This is a Sample email content i! </h2></body></html>" );
        blob.Properties.ContentType = "application/json";

        using (var ms = new MemoryStream())
        
        LoadStreamWithJson(ms, serializeJesonObject);
        await blob.UploadFromStreamAsync(ms);
        
        log.LogInformation($"Bolb randomStr is uploaded to container container.Name");
        await blob.SetPropertiesAsync();
        

        return  new OkObjectResult("UploadBlobHttpTrigger function executed successfully!!");
        

        private  static  void CreateContainerIfNotExists(ILogger logger, ExecutionContext executionContext)
        
        CloudStorageAccount storageAccount = GetCloudStorageAccount(executionContext);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        string[] containers = new  string[]  "dummy-messages" ;
        foreach (var item in containers)
        
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(item);
        blobContainer.CreateIfNotExistsAsync();
        
        

        private  static CloudStorageAccount GetCloudStorageAccount(ExecutionContext executionContext)
        
        var config = new ConfigurationBuilder()
        .SetBasePath(executionContext.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", true, true)
        .AddEnvironmentVariables().Build();
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(config["CloudStorageAccount"]);
        return storageAccount;
        
        private  static  void LoadStreamWithJson(Stream ms, object obj)
        
        StreamWriter writer = new StreamWriter(ms);
        writer.Write(obj);
        writer.Flush();
        ms.Position = 0;
        
        
        

查看HTTP Trigger 了解更多信息

【讨论】:

以上是关于通过 C# Azure 函数生成 Blob 容器的主要内容,如果未能解决你的问题,请参考以下文章

Azure Functions - Blob 流动态输入绑定

C#:使用 Azure.Storage.Blobs 客户端库将 blob 从一个容器移动到另一个容器

在 Table Storage azure 中使用 Etag 通过多线程更新属性

将多个 Blob 输入传递给 QueueTrigger Azure 函数的最佳方式

需要通过oledb连接从azure存储blob容器中读取excel文件

如何使用 c# 从 azure blob 存储中检索 xml 文件