Azure

Create Container and Add Blob utilizing Azure Perform in .NET Core

To this point we’ve created a perform app challenge with Http set off perform with default template and put in the required NuGet packages.

Now we’re going to write the precise operation carried out by the perform. As I already talked about, the aim of this perform is to create container and add some JSON blob content material into the container.

Add further parameter into perform Run technique as ExecutionContext which can give the likelihood to make use of of ConfigurationBuilder in order that Azure perform can learn storage connection string from Json file or from perform configuration settings on Azure.

  1. utilizing System;  
  2. utilizing System.IO;  
  3. utilizing System.Threading.Duties;  
  4. utilizing Microsoft.AspNetCore.Mvc;  
  5. utilizing Microsoft.Azure.WebJobs;  
  6. utilizing Microsoft.Azure.WebJobs.Extensions.Http;  
  7. utilizing Microsoft.AspNetCore.Http;  
  8. utilizing Microsoft.Extensions.Logging;  
  9. utilizing Newtonsoft.Json;  
  10. utilizing Microsoft.Azure.Storage;  
  11. utilizing Microsoft.Azure.Storage.Blob;  
  12. utilizing Microsoft.Extensions.Configuration;  
  13.   
  14. namespace AzFunctions  
  15. {  
  16.     public static class UploadBlobHttpTriggerFunc  
  17.     {  
  18.         [FunctionName(“UploadBlobHttpTriggerFunc”)]  
  19.         public static async Process<IActionResult> Run(  
  20.             [HttpTrigger(AuthorizationLevel.Function, “get”“post”, Route = null)] HttpRequest req,  
  21.             ILogger log, ExecutionContext context)  
  22.         {  
  23.             log.LogInformation($“C# Http set off perform executed at: {DateTime.Now}”);  
  24.             CreateContainerIfNotExists(log, context);  
  25.   
  26.             CloudStorageAccount storageAccount = GetCloudStorageAccount(context);  
  27.             CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
  28.             CloudBlobContainer container = blobClient.GetContainerReference(“dummy-messages”);  
  29.   
  30.             for (int i = 1 ; i <= 5; i++)  
  31.             {  
  32.                 string randomStr = Guid.NewGuid().ToString();  
  33.                 CloudBlockBlob blob = container.GetBlockBlobReference(randomStr);  
  34.   
  35.                 var serializeJesonObject = JsonConvert.SerializeObject(new { ID = randomStr, Content material = $“<html><physique><h2> This is a Pattern e-mail content material {i}! </h2></physique></html>” });  
  36.                 blob.Properties.ContentType = “software/json”;  
  37.   
  38.                 utilizing (var ms = new MemoryStream())  
  39.                 {  
  40.                     LoadStreamWithJson(ms, serializeJesonObject);  
  41.                     await blob.UploadFromStreamAsync(ms);  
  42.                 }  
  43.                 log.LogInformation($“Bolb {randomStr} is uploaded to container {container.Title}”);  
  44.                 await blob.SetPropertiesAsync();  
  45.             }  
  46.   
  47.             return new OkObjectResult(“UploadBlobHttpTrigger perform executed efficiently!!”);  
  48.         }  
  49.   
  50.         non-public static void CreateContainerIfNotExists(ILogger logger, ExecutionContext executionContext)  
  51.         {  
  52.             CloudStorageAccount storageAccount = GetCloudStorageAccount(executionContext);  
  53.             CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
  54.             string[] containers = new string[] { “dummy-messages” };  
  55.             foreach (var merchandise in containers)  
  56.             {  
  57.                 CloudBlobContainer blobContainer = blobClient.GetContainerReference(merchandise);  
  58.                 blobContainer.CreateIfNotExistsAsync();  
  59.             }  
  60.         }  
  61.   
  62.         non-public static CloudStorageAccount GetCloudStorageAccount(ExecutionContext executionContext)  
  63.         {  
  64.             var config = new ConfigurationBuilder()  
  65.                             .SetBasePath(executionContext.FunctionAppDirectory)  
  66.                             .AddJsonFile(“native.settings.json”truetrue)  
  67.                             .AddEnvironmentVariables().Construct();  
  68.             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(config[“CloudStorageAccount”]);  
  69.             return storageAccount;  
  70.         }  
  71.         non-public static void LoadStreamWithJson(Stream ms, object obj)  
  72.         {  
  73.             StreamWriter author = new StreamWriter(ms);  
  74.             author.Write(obj);  
  75.             author.Flush();  
  76.             ms.Place = 0;  
  77.         }  
  78.     }  
  79. }  

Open native.settings.json and add CloudStorageAccount connection string which is required for testing from native.

 

This worth might be discovered out of your Azure storage account. For that, login to Azure account => Go to storage account => Click on on Entry Keys below settings on left menu => You will note two keys there => Copy anybody of the connection sting => Previous that into native.settings.json

  1. “Values”: {  
  2.     “AzureWebJobsStorage”“UseDevelopmentStorage=true”,  
  3.     “FUNCTIONS_WORKER_RUNTIME”“dotnet”,  
  4.     “CloudStorageAccount”“DefaultEndpointsProtocol=https;AccountName=;AccountKey=;EndpointSuffix=core.home windows.internet,  
  5.   }   

Let’s construct and run the perform and check in native. You will note the beneath window open.

Whereas working perform app challenge in native after urgent F5, in case you see beneath error, you might want to begin Azure storage emulator from begin menu of your PC.

 

 

If emulator cmd is giving error and didn’t create native storage occasion, you may run AzureStorageEmulator.exe begin –inprocess in emulator cmd to bypass it as of now.

 

Create Container and Upload Blob using Azure Function in .NET Core

 

Now copy the http url from native perform console window (in my case it’s http://localhost:7071/api/UploadBlobHttpTriggerFunc) and paste it into the browser. As soon as it’s executed, you will note message “UploadBlobHttpTrigger perform executed efficiently!!” which we truly returned from the perform. And in case you take a look at the storage account in Azure portal, you will note container is created and blob messages are uploaded.

 

Superior! Let’s deploy the perform.

 

Deploy Azure Perform from Visual Studio

 

We are actually able to publish our perform app to Azure. For that, proper click on on challenge and click on on Publish… => Choose Goal as Azure => Click on on Subsequent => Choose particular goal as Azure Perform App (Home windows) => Click on on Subsequent => Choose your azure account to log in.

 

 Create Container and Upload Blob using Azure Function in .NET Core

 

Create Container and Upload Blob using Azure Function in .NET Core 
Create Container and Upload Blob using Azure Function in .NET Core

 

When you logged in and choose your Azure account from visible studio it is time to  create Azure perform app. 

 

In the event you already created perform App from Azure or when you have present perform app, then you may instantly choose that, in any other case you may create new Azure Perform app by clicking on “Create a brand new Azure Perform…” possibility.

 

Create Container and Upload Blob using Azure Function in .NET Core

 

Give Title to perform app, choose useful resource group and azure storage => Click on on Create.

 

 Create Container and Upload Blob using Azure Function in .NET Core

 

Now choose Azure Perform app => Click on on End.

 

Create Container and Upload Blob using Azure Function in .NET Core

 

Now Click on on Configure => Choose Storage account and click on on Subsequent => Click on End

 

Create Container and Upload Blob using Azure Function in .NET Core

 

 Lastly, we’re able to publish. Click on on Publish. As soon as it’s accomplished, test on Azure portal.

 

 Create Container and Upload Blob using Azure Function in .NET Core 

 

On Azure portal, you will note the newly created Azure perform.

 

 Create Container and Upload Blob using Azure Function in .NET Core

 

Let’s modify the configuration so as to add further settings as “CloudStorageAccount” and replica connection string worth from storage account and click on on save.

 

Create Container and Upload Blob using Azure Function in .NET Core

 

Take a look at Azure Perform

 

Now we will check the perform, navigate to perform and click on on “Click on+Take a look at” => Click on on Take a look at/Run to run it. You may alternatively run by hitting the url. For that, you may click on on “Get perform url” and replica and paste url into the browser.

 

 Create Container and Upload Blob using Azure Function in .NET Core

 

As soon as it is profitable, let’s cross confirm the uploaded content material on container into storage account.

 

 Create Container and Upload Blob using Azure Function in .NET Core

 

Glorious! We  created and examined Http set off perform and deployed it to Azure.

 

Abstract

 

On this article, we created a container and uploaded pattern blob although Azure http set off perform utilizing .NET Core. Additionally we’ve seen deploy an Azure Perform from Visual Studio itself step-by-step and examined the perform from Azure portal. I hope you discover this text helpful!

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button