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 news Wasmer WebAssembly platform now backs iOS Wasmer 5.0 release also features improved performance, a leaner codebase, and discontinued support for the Emscripten toolchain. By Paul Krill Oct 30, 2024 2 mins Mobile Development Web Development Software Development news analysis What Entrust certificate distrust means for developers Secure communications between web browsers and web servers depend on digital certificates backed by certificate authorities. What if the web browsers stop trusting your CA? By Travis Van Oct 30, 2024 9 mins Browser Security Web Development Application Security news Next.js 15 arrives with faster bundler High-performance Rust-based Turbopack bundler moves from beta to stable with the latest update of the React-based web framework. By Paul Krill Oct 24, 2024 2 mins JavaScript React Web Development feature WasmGC and the future of front-end Java development WebAssembly’s garbage collection extension makes it easier to run languages like Java on the front end. Could it be the start of a new era in web development? By Matthew Tyson Oct 16, 2024 10 mins Web Development Software Development Resources Videos