Achraf Ben Alaya
No Result
View All Result
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • About
SUBSCRIBE
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • About
No Result
View All Result
Achraf Ben Alaya
No Result
View All Result
Home Blog Cloud Azure

Azure Function to Upload Data to Azure Blob

achraf by achraf
August 29, 2020
in Azure, Blog
0
Azure Function to Upload Data to Azure Blob
0
SHARES
51
VIEWS
Share on FacebookShare on Twitter

Azure Functions is a solution for easily running small pieces of code, or “functions,” in the cloud. You can write just the code you need for the problem at hand, without worrying about a whole application or the infrastructure to run it.

Functions can make development even more productive, and you can use your development language of choice, such as C#, Java, JavaScript, PowerShell, and Python. Pay only for the time your code runs and trust Azure to scale as needed. Azure Functions lets you develop serverless applications on Microsoft Azure.

In this article we are going to create a new azure function (HTTPTrigger) that receieve a request with parametre in the body and if those parameteres and right we save that into a json or text file inside a blob storage .

For more about Azure function visit : https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview

In this example we are going to use visual studio , it’s possible to use visual studio code too (we will do that in some other blog post asap)

HTTPTrigger Function

In the first part of our post ,we are going to create a simple HTTPTrigger Function :

For that lets start by creating our solution using my favorite editor Visual Studio

Note : make sure to choose anonymous for Authorization Level which means no authentication is required and Any valid HTTP request passes.

Now we have our first function ready called Function1 . Before We explain the code let’s build our application and see the output .

press F5 and like that we have :

If we go to the link provided by the application we have :

and now we have our first application ready to run and even to deploy but let’s back to explain our code :

As you can see in our code we can give our function a name by adding annotation with the Function name that we want to give to our function .

Our function use anonymous authorizationlevel and accept get and post methods .

It accept ‘name’ as parameter and if it’s null it should return a bad request object that contain “Please pass a name on the query string or in the request body”

Now , we need to run our application again to test , since now we understand that we should send a name in parameter :

Now let’s make some changes to our code to understand it more .

We added lastname here as another parameter :

Now let’s try to remove the name and keep the lastname :

As you can see we have a badrequest here cause in our code we test on name that should not be null and not lastname .

Now Instead of having Name and last name let’s change our code :

public static class Function1
    {
    [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        //param in
        string ID = req.Query["Id"];
        string Name = req.Query["Name"];
        string TrId = req.Query["TrId"];
        string ReId = req.Query["ReId"];


        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        ID = ID ?? data?.ID;
        Name = Name ?? data?.Name;
        TrId = TrId ?? data?.TrId;
        ReId = ReId ?? data?.ReId;

        return ID != null && TrId !=null && ReId !=null
            ? (ActionResult)new OkObjectResult($"Hello, {ID},{Name},{TrId},{ReId} Demo")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
}

Now In this part we are asking for parameters (ID,Name,TrId,ReId) . If we try to run this we have :

Now let’s try to enhance our code by moving the entities to a model called : TransactionData

Let’s add a new class

and now we replace it as in picture below :

If we test our code now we have :

Awesome , now what I need to do ,is to save every single request (correct request) inside a single json file (or text).

For that we are going to use Blob storage and save our data inside a container .

for that we need to go to azure and create a storage account :

 

Now we need to copy our connection string and save it cause we are going to use it later .

To read more about Storage account : https://docs.microsoft.com/en-us/azure/storage/common/storage-account-overview

Now what we need to to is to impliment saving data to container :

Now what we have here is a method to create a Blob ,it’s look’s for a container called “achrafbenalayacontainer” and if it can not find it, it create a new one and upload to data to it .

Ps : you can explore your storage account using Microsoft azure storage explorer .

Download Link : https://azure.microsoft.com/en-us/features/storage-explorer/

Next step is to add some code to our first method , so if we have a true request body we call our method and send data to it .

 

We can add some custom message

And Like that we have our transactions saved each one in a new json file .

In next blog we will see how to upload this function to azure and how to create a CI/CD pipline to deploy our function .

Full source code : http://bit.ly/2T9u5jZ

Happy Azure day =)

 

 

ShareTweet
Previous Post

Sql tips

Next Post

Deploy azure function from visual studio 2019

Related Posts

Microsoft Ignite Cloud Skills Challenge March 2021
Blog

Microsoft Ignite Cloud Skills Challenge March 2021

March 2, 2021
60
Hello Microsoft Graph !
Azure

Hello Microsoft Graph !

February 25, 2021
24
You forget to shutdown your virtual machine on your way home ? send an email and save your time
Blog

You forget to shutdown your virtual machine on your way home ? send an email and save your time

February 6, 2021
37
Azure Automation :  How to create PowerShell Runbook
Azure

Azure Automation : How to create PowerShell Runbook

February 4, 2021
26
Welcome to Azure Resource Mover service
Azure

Welcome to Azure Resource Mover service

February 2, 2021
57
Background Tasks With Hangfire And .Net 5
Blog

Background Tasks With Hangfire And .Net 5

January 25, 2021
130
Next Post
Deploy azure function from visual studio 2019

Deploy azure function from visual studio 2019

Leave a Reply Cancel reply

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

Recommended

You forget to shutdown your virtual machine on your way home ? send an email and save your time

You forget to shutdown your virtual machine on your way home ? send an email and save your time

February 6, 2021
37
Microsoft Ignite Cloud Skills Challenge March 2021

Microsoft Ignite Cloud Skills Challenge March 2021

March 2, 2021
60
My book collection for 2020-2021

My book collection for 2020-2021

December 28, 2020
117
Reading Excel file in Azure Web Apps

Reading Excel file in Azure Web Apps

December 1, 2020
90
Access Microsoft Azure Courses On Pluralsight for Free

Access Microsoft Azure Courses On Pluralsight for Free

April 21, 2020
65
Learn… and get rewarded

Learn… and get rewarded

September 28, 2020
207
Facebook Twitter LinkedIn Youtube
Microsoft Ignite Cloud Skills Challenge March 2021

Microsoft Ignite Cloud Skills Challenge March 2021

March 2, 2021
Hello Microsoft Graph !

Hello Microsoft Graph !

February 25, 2021
You forget to shutdown your virtual machine on your way home ? send an email and save your time

You forget to shutdown your virtual machine on your way home ? send an email and save your time

February 6, 2021

Categories

  • Apps (1)
  • Azure (18)
  • blazor (2)
  • Blog (36)
  • c# (6)
  • Cloud (17)
  • docker (2)
  • Games (1)
  • General Tips & Fix (1)
  • motivation (1)
  • Motivation (3)
  • News (7)
  • sql (3)
  • Tricks, Tips and Fixes (1)
  • xamarin (5)

No Result
View All Result
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • About