Friday 24 March 2023

Upload file to Azure Storage Blob/Container in ASP. Net / C# Code

There are many ways to upload files to Azure Storage. We will discuss 3 different ways today. Before that let's create a storage container on Azure Portal.


1. Create a Storage account in Azure Portal
2. Click on the container and create a new container and select the public access level to the container.











3. Click on the "Access keys" option and copy the connection string. We will use it in our code.














Let's learn different ways to upload files to Azure Storage. For that, you need to install Azure.Storage.Blobs package from Nuget and you can run the below code based on your requirements.


1. Upload bytes/base64 string to Azure Storage


      [HttpPost]
public async Task<string> UploadFile(string imageBase64, string fileName) { if (!string.IsNullOrEmpty(imageBase64)) { var bytes = Convert.FromBase64String(imageBase64); var stream = new MemoryStream(bytes); var connectionString = "Default";// pass connectionstring here var blobServiceClient = new BlobServiceClient(connectionString); //replace containername with your container name
                var containerClient = blobServiceClient.GetBlobContainerClient("containername"); if (containerClient == null) { return string.Empty; } var blobClient = containerClient.GetBlobClient(fileName); var result = await blobClient.UploadAsync(stream); return blobClient.Uri.AbsoluteUri; }
return string.Empty; }

2. Upload IFormFile to Azure Storage


      
[HttpPost]
public async Task<string> UploadFile(IFormFile file,string fileName) { if (file != null) { var stream = file.OpenReadStream(); var connectionString = "Default";// pass connectionstring here var blobServiceClient = new BlobServiceClient(connectionString); var containerClient = blobServiceClient.GetBlobContainerClient("containername"); if (containerClient == null) { return string.Empty; } var blobClient = containerClient.GetBlobClient(fileName); var result = await blobClient.UploadAsync(stream); return blobClient.Uri.AbsoluteUri; } return string.Empty; }


3. Upload Local file to Azure Storage

      
[HttpPost] public async Task<string> UploadFile(string fileName) { string localfilePath = "Pass local file path here"; var stream = System.IO.File.OpenRead(localfilePath); var connectionString = "Default";// pass connectsting here var blobServiceClient = new BlobServiceClient(connectionString); var containerClient = blobServiceClient.GetBlobContainerClient("containername"); if (containerClient == null) { return string.Empty; } var blobClient = containerClient.GetBlobClient(fileName); var result = await blobClient.UploadAsync(stream); return blobClient.Uri.AbsoluteUri; }


This is all about this article.

No comments:

Post a Comment

How to find the reason of HTTP Error 500.30 - ASP.NET Core app failed to start in Azure App Service

HTTP Error 500.30 - The ASP.NET Core app failed to start If your web app is throwing an error HTTP error 500.30 then how to find the root ca...