Azure

How To Add Information Into Azure Weblog Storage Utilizing Azure Capabilities In C#

Introduction

On this article, we’ll talk about about Azure Storage and several types of storage providers offered by azure. We’ll create a storage account from Azure portal and we’ll then create Azure perform to retailer the information into azure blob storage, lastly, we’ll deploy the identical perform to the Azure perform app from Visual Studio.

If you’re new to Azure Capabilities then try the beneath articles,

What’s Azure Storage Service

Every time we create an software now we have to first resolve what storage we are able to use to retailer the info. Azure Storage supplies answer to completely different storage issues. It supplies scalable, accessible, sturdy storage for information in azure. Azure storage is a set of various providers akin to Blobs, Information, Queues, and many others. We will work together with azure storage utilizing completely different programming languages such .NET, JAVA, Golang, Javascript, and many others. utilizing the SDKs.

Completely different Azure Storage Service

  1. Azure Blob – also called blobs that are primarily used to retailer the binary/textual content information akin to photographs, movies, paperwork, and many others.
  2. Azure Tables – which is nothing however a NoSQL storage for schemaless structured information.
  3. Azure Queue – it’s used to retailer the messages which can be utilized to speak with functions
  4. Azure Information – it’s managed file share service for cloud or on-premise utilizing SMB/NFS protocol.
  5. Azure Disks – It’s a digital arduous disk (VHD) that’s of two varieties: managed and unmanaged.

What’s Azure Blob storage?

Azure Blob storage is a storage service used to retailer schemaless structured information within the cloud. We will retailer information akin to paperwork, footage, movies, log information, backup information which don’t have a set construction. Blob storage is much like listing construction the place we are able to create completely different containers to retailer the blobs.

There are three sorts of Blobs:

  1. Block Blobs – that are used to retailer the textual content/binary information.
  2. Append Blobs – nothing however block blobs which are optimized for append operation.
  3. Web page Blobs – which retailer random entry information as much as eight TiB in dimension.

Create easy azure perform utilizing C# to add information in Azure Weblog Storage

Conditions

  1. Azure account (If you do not have an Azure subscription, create a free trial account)
  2. Visual Studio 22 with Azure Growth workload
  3. Primary information of C#

Create a Azure Storage useful resource utilizing Azure Portal

Login to Azure and click on on Create a useful resource button.

In search field kind “storage account” and choose it.

Click on on Create button and you may see the Create storage account web page.

How to upload files into Azure Blog Storage using Azure functions in C#

Azure has Useful resource Teams (RG) act as a container on your assets. So now we’re going to create a Storage account useful resource. First we have to create Useful resource Group. When you’ve got already created RG then you should use the identical right here. Beneath Useful resource group click on on Create New button and provides a singular RG identify.

How to upload files into Azure Blog Storage using Azure functions in C#

Give the distinctive identify to storage account and choose the area. Click on on Subsequent : Superior button.

How to upload files into Azure Blog Storage using Azure functions in C#

By default the “Allow blob public entry” is checked which signifies that any nameless consumer can entry blobs inside the containers. So uncheck the choice and click on on Evaluation + Create button

How to upload files into Azure Blog Storage using Azure functions in C#

Anticipate few sec to finish the validation and Evaluation all the things is added correctly after that click on on Create button.

How to upload files into Azure Blog Storage using Azure functions in C#

Creating assets will take time so watch for deployment course of to complete.

How to upload files into Azure Blog Storage using Azure functions in C#

Our storage account is created efficiently. Now click on on Go to useful resource button to see the storage account.

How to upload files into Azure Blog Storage using Azure functions in C#

How to upload files into Azure Blog Storage using Azure functions in C#

Now subsequent step is to create a container to retailer our blobs. So in left part click on on Containers after which click on on + Container button. Give the right identify to container wherein we’re going to add our information and click on on Create button.

How to upload files into Azure Blog Storage using Azure functions in C#

Subsequent step is to create an Azure perform that uploads information into the “file-upload” container.

How to upload files into Azure Blog Storage using Azure functions in C#

Create a HTTP set off azure perform utilizing C# to add information to blob storage

So open Visual Studio and Go to File -> New -> Undertaking. Search “Azure Capabilities” within the search field and choose the Azure perform template and click on on Subsequent.

How to upload files into Azure Blog Storage using Azure functions in C#

Give a reputation to the perform venture and click on on Create.

How to upload files into Azure Blog Storage using Azure functions in C#

Choose the HTTP set off template and set the Authorization degree as Nameless and click on on Create.

How to upload files into Azure Blog Storage using Azure functions in C#

That is it. Now we have created our first Azure perform. By default the identify of the perform is Function1.cs so now change it to “FileUpload“.

How to upload files into Azure Blog Storage using Azure functions in C#

So for our software, we’re creating HTTP Set off perform which takes file as enter and uploads it into the Azure Blob. To attach with the Blob Storage container we want a connection string. So let’s open the Azure Storage useful resource in portal ->Entry Keys -> Click on on Present Keys – > Copy the Key 1 Connection String.

How to upload files into Azure Blog Storage using Azure functions in C#

Open the native.settings.json file in our perform app and paste the connection string of our Azure Storage useful resource as worth of “AzureWebJobsStorage” key. Additionally add one other key referred to as “ContainerName” and paste the identify of container now we have created earlier.

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "<change your blob storage connection key right here>",
        "ContainerName": "file-upload", // Container identify
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    }
}

To work together with Azure storage now we have to first set up the beneath NuGet package deal.

How to upload files into Azure Blog Storage using Azure functions in C#

Now add the beneath code to add a file into the container in our blob storage.

utilizing Azure.Storage.Blobs;
utilizing Microsoft.AspNetCore.Http;
utilizing Microsoft.AspNetCore.Mvc;
utilizing Microsoft.Azure.WebJobs;
utilizing Microsoft.Azure.WebJobs.Extensions.Http;
utilizing Microsoft.Extensions.Logging;
utilizing System;
utilizing System.IO;
utilizing System.Threading.Duties;
namespace FileUploadFunction {
    public static class FileUpload {
        [FunctionName("FileUpload")]
        public static async Job < IActionResult > Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) {
            string Connection = Surroundings.GetEnvironmentVariable("AzureWebJobsStorage");
            string containerName = Surroundings.GetEnvironmentVariable("ContainerName");
            Stream myBlob = new MemoryStream();
            var file = req.Kind.Information["File"];
            myBlob = file.OpenReadStream();
            var blobClient = new BlobContainerClient(Connection, containerName);
            var blob = blobClient.GetBlobClient(file.FileName);
            await blob.UploadAsync(myBlob);
            return new OkObjectResult("file uploaded successfylly");
        }
    }
}

First now we have saved the Blob Storage connection string and container identify from configuration into native variables. After that now we have learn the file request through the use of key referred to as “File” into variable and utilizing OpenReadStream() perform now we have learn the file into the steam. To work together with Blob storage we first should create BlobContainerClient by passing connection string and container identify. After creating consumer, now we have to create BlobClient utilizing GetBlobClient technique by passing enter file identify.

Lastly to add file to container now we have used UploadAsync technique by passing stream. You’ll be able to learn extra about these strategies right here.

Now run the FileUpload perform.

How to upload files into Azure Blog Storage using Azure functions in C#

For testing the perform open postman and paste the about perform URL i.e. http://localhost:7071/api/FileUpload

Now in our code now we have learn the file from kind information so click on on Physique and choose form-data then add key as “File” and for worth choose which file you need to add and click on on Ship button.

How to upload files into Azure Blog Storage using Azure functions in C#

Now if we open our “file-upload” container we are able to see the file which we uploaded.

How to upload files into Azure Blog Storage using Azure functions in C#

Publish Azure perform to Azure perform app utilizing Visual Studio

Create a perform app utilizing Azure portal

Log in to the Azure portal Within the Search bar, search as “perform app” after which choose the Perform app.

How to upload files into Azure Blog Storage using Azure functions in C#

How to upload files into Azure Blog Storage using Azure functions in C#

After that click on on the ” + Create” button so as to add a brand new perform app. Fill out the essential particulars,

How to upload files into Azure Blog Storage using Azure functions in C#

Now click on on Evaluation: Create button and assessment all the main points and click on on the Create button. Anticipate a couple of minutes to create the assets.

How to upload files into Azure Blog Storage using Azure functions in C#

As soon as deployment is full click on on Go to useful resource button to see our new perform app.

How to upload files into Azure Blog Storage using Azure functions in C#

How to upload files into Azure Blog Storage using Azure functions in C#

Publish the azure perform into the perform app

To deploy our perform to Azure we want Azure Perform App. Proper Click on on our answer and click on on the Publish button.

How to upload files into Azure Blog Storage using Azure functions in C#

As we’re publishing into Azure then Choose Azure and click on on Subsequent. Choose goal as “Azure Perform App(Home windows)” and click on on Subsequent. We will both choose an already created perform app or we are able to create a brand new Perform app.

Click on on End button.

How to upload files into Azure Blog Storage using Azure functions in C#

Click on on Publish button to push our perform to our Perform App.

How to upload files into Azure Blog Storage using Azure functions in C#

As soon as our app is efficiently revealed on Azure, go to the Azure portal and seek for our perform app. Choose Capabilities from the left sidebar to see our deployed perform.

How to upload files into Azure Blog Storage using Azure functions in C#

Configure the storage account connection string into Configuration setting of Perform app

Now now we have deployed our perform into Perform app so subsequent step is to configure the settings. So click on on Configuration. For this app now we have to set worth of Storage connection string and container identify in configuration. So seach for “AzureWebJobsStorage” and click on on Edit button and paste your storage connection string.

How to upload files into Azure Blog Storage using Azure functions in C#

How to upload files into Azure Blog Storage using Azure functions in C#

Click on on “+ New software setting” and provides identify as “ContainerName” and Worth as “file-upload”.

How to upload files into Azure Blog Storage using Azure functions in C#

Lastly click on on Save button to avoid wasting adjustments. 

How to upload files into Azure Blog Storage using Azure functions in C#

Take a look at publish app utilizing postman

To ge the url of deployed perform click on on Capabilities -> Choose File Add perform -> Click on on Get Perform Url -> Copy url and paste in postman.

How to upload files into Azure Blog Storage using Azure functions in C#

Now as we take a look at domestically in form-data add a key “File” and choose file you need to add.

How to upload files into Azure Blog Storage using Azure functions in C#

Now if we verify container we are able to see the file uploaded into it.

How to upload files into Azure Blog Storage using Azure functions in C#

Conclusion

On this article, now we have talk about about Azure Storage and Azure blob storage. Now we have created a Azure Blob storage useful resource from Azure Portal. We created a brand new Azure perform from Visual Studio which uploads the file to blob storage. Additionally, I demonstrated learn how to take a look at and deploy the perform from VS and take a look at utilizing Postman. I actually hope that you just loved this text, share it with pals, and please don’t hesitate to ship me your ideas or feedback. Keep tuned for extra Azure Capabilities articles.

Tags
Show More

Related Articles

Leave a Reply

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

Close