Take advantage of Azure Blob storage to store data in the Microsoft cloud from your ASP.Net Core applications Credit: Lukas Budimaier It’s easy to take advantage of Microsoft Azure cloud resources in ASP.Net Core, Microsoft’s cross-platform, lean, and modular framework for building high-performance web applications. You can use an Azure storage account to store or retrieve data, for example. Such data might include files, blobs, queues, or tables. In this article we’ll look at how we can upload data to Azure Blob storage from an ASP.Net Core application. Create an ASP.Net Core Web API project in Visual Studio Assuming that you’re running Visual Studio 2017, you can follow the steps outlined below to create an ASP.Net Web API project in Visual Studio 2017. In the Visual Studio IDE, click on File > New > Project. Select “ASP.Net Core Web Application (.Net Core)” from the list of templates displayed. Specify a name for the project. Click OK to save the project. Select “API” in the “New .Net Core Web Application…” window. Uncheck the “Enable Docker Support” checkbox. Select “No Authentication” as we won’t be using authentication here. Click OK. This will create a new ASP.Net Core 2.1 Project in Visual Studio 2017. As a prerequisite, you should have an Azure storage account. To learn how to create an Azure storage account, you can refer to this article. Implement the UploadDataController service in ASP.Net Core In this section we’ll explore how we can implement a simple ASP.Net Core controller to upload data to Azure Blob storage. To create the controller, right-click on the Controllers folder in the Solution Explorer Window, select Add > Controller, choose “API Controller – Empty,” and click Add. Next, name the controller “UploadDataController” and click Add again. Now add the WindowsAzure.Storage package via the NuGet Package Manager. Alternatively, you can use the NuGet Package Manager Console to install the package with the following command: Install-Package WindowsAzure.Storage -ProjectName YourProjectNameHere Connect to your Azure Blob storage container Assuming that you have successfully installed the WindowsAzure.Storage package in your project, you can now start writing the code. You will need the connection string from your Azure storage account. Then you will need to create a reference to the Azure Blob client as shown in the code snippet given below. The Azure Blob client, named CloudBobClient below, refers to the Blob storage in your Azure storage account. string connectionString ="Specify the connection string to connect to the Azure blob storage here."; CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); Once you have the reference to the CloudBlobClient instance, you can create an instance of the CloudBlobContainer, the actual container object in your Azure Blob storage. Note that the name of the container is passed as a parameter to the GetContainerReference method of the CloudBlobClient instance. CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName); Next, you can retrieve a reference to the block using the following code. Block blobs—one of three kinds of blobs in Azure Blob storage—make it easy to upload large blobs (up to 4.75TB). CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename); The complete UploadData controller method in ASP.Net Core Finally, here is the complete source code of the controller method named UploadData. public async Task UploadData(Stream stream, string filename) { try { string connectionString ="Specify the connection string to connect to the Azure blob storage here."; string containerName ="Specify the container name here."; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName); CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename); await cloudBlockBlob.UploadFromStreamAsync(stream); stream.Dispose(); return true; } catch { return false; } } You can leverage Azure Blob storage to store large amounts of unstructured data in Azure—documents, images, audio and video files, log files, backups, anything. You can access this data from anywhere, and take advantage of role-based access control and encryption to secure the data as well. In this article we examined how we can upload data to Azure Blob Storage using a RESTful ASP.Net Core service. I’ll discuss more ways we can work with Azure Blob storage in future posts here. Related content how-to How to use FastEndpoints in ASP.NET Core Take advantage of the free open-source FastEndpoints library to build fast and lean APIs in your ASP.NET Core applications. By Joydip Kanjilal Jul 11, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Refit to consume APIs in ASP.NET Core Take advantage of Refit REST library to simplify API consumption and make your code cleaner, more efficient, and easier to maintain. By Joydip Kanjilal Jul 04, 2024 10 mins C# Microsoft .NET Software Deployment how-to When to use an abstract class vs. interface in C# Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications. By Joydip Kanjilal Jun 20, 2024 10 mins Small and Medium Business Microsoft .NET C# how-to 6 security best practices for ASP.NET Core Learn the best practices and built-in safeguards for preventing attacks and protecting sensitive data in your ASP.NET Core web applications. By Joydip Kanjilal Jun 07, 2024 6 mins C# Microsoft .NET Web Development Resources Videos