Generate a Blob Container by C# Azure Function

Solution for Generate a Blob Container by C# Azure Function
is Given Below:

I want to create a Function that uses an HTTP Trigger that when it is triggered by an HTTP Signal, it will create a container in Blob Storage.

Create a Http Trigger in visual studio and use the code to create container

Here is the code to create a container using 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;
        }
        }
        }

Check the HTTP Trigger for further information