Azure

Azure Blob Storage – Add/Obtain/Delete File(s) Utilizing .NET 5

Right now on this article, we are going to see tips on how to add/obtain and delete the information within the cloud utilizing Azure blob storage utilizing ASP.Web Core 5.Zero Net API.

  1. Create an Azure storage account and blob container
  2. Getting the Entry keys (Connection string)
  3. Create an internet API Mission (.Web 5.0)
  4. Connect with Azure Storage account
  5. Add file to Azure Blob Storage
  6. Obtain file from  Azure Blob Storage
  7. Delete Information

Create an Azure storage account and blob container

 

Open the Azure portal and select the Storage Account below the Azure Companies.

 

Click on on the + New button and it’ll take us to a brand new web page to create a storage account.

 

 

After clicking on create a brand new storage account the very first thing we’re going to select is the subscription you could have, subsequent create/choose useful resource group after that, we’re going to enter the Storage account identify as “newfilestorage” subsequent choose the situation whichever is best for you for this demo, I’m going to decide on “(US) East US” location. Subsequent, we’re deciding on Efficiency choices, right here we’ve got Normal Storage and Premium storage choice we’re going to choose “Normal ” for this demo. Subsequent comes your account sort, we’re going to select “Common-purpose v2” and in replication, we’re going to select “Geo-redundant storage (GRS)” and final choice entry tier we going to decide on “Sizzling storage” eventually click on on overview + create button.

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

After clicking on the Evaluate + create right here we are able to see the validation handed standing and together with the overview web page with all of the choices which we had opted for after which click on on create a button on the backside to create a storage account.

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

 

Storage account 

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

 

Create a blob container below the storage account

 

To retailer the precise paperwork or information we want a blob container below the identical storage account. Let’s create a brand new blob container. Open the newly created storage account and below the left facet choices, you will see that the named Container click on on that the place it provides us an choice to create a brand new container.

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

 

Present the identify for the brand new container, select the entry stage, and click on on the create button on the backside of the web page. 

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

Getting the Entry keys (Connection string)

 

Now right here comes the principle image after the creation of the blob container, to include the Azure storage account within the venture we have to get the Entry keys in speaking our venture with our Azure storage account.

 

Entry keys

 

Created by default once we arrange the storage account.

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

From this display, we’re going to copy the Connection string (key 1).

 

Create a Net API Mission (.Web 5)

 

Open the Visual Studio and selected the ASP.Web Core Net API and as soon as after that select the .Web 5.Zero template from the dropdown it is going to create a venture below the .Web 5 goal framework.

 

Set up the bundle which is able to talk with the Azure Storage account,

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

 

Connect with Azure Storage account 

 

Right here we’re going to add the connection string and in addition to blob container identify to the appsettings.json file with the important thing identify BlobConnectionString &  BlobContainerName and we have to move the connection string and container identify which we’ve got copied from Entry Key.

 

appsettings.json 

  1. {  
  2.     “Logging”: {  
  3.         “LogLevel”: {  
  4.             “Default”“Info”,  
  5.             “Microsoft”“Warning”,  
  6.             “Microsoft.Internet hosting.Lifetime”“Info”  
  7.         }  
  8.     },  
  9.     “AllowedHosts”“*”,  
  10.     “BlobConnectionString”“*Your Connection string*”,  
  11.     “BlobContainerName”“*Your container identify*”  
  12. }   

After including the keys, Now we’re going to create a FileController (API) and injecting the IConfiguration for accessing the keys from the appsettings.json file.

 

FileController.cs

 

After creating the controller we’re going to add the constructor for it for injecting the Iconfiguration dependency with a view to fetch the values,

  1. utilizing System.Collections.Generic;  
  2. utilizing System.IO;  
  3. utilizing System.Linq;  
  4. utilizing System.Threading.Duties;  
  5. namespace UploadAndDownload_AzureBlobStorage.Controllers {  
  6.     [Route(“api/[controller]”)]  
  7.     [ApiController]  
  8.     public class FileController: ControllerBase {  
  9.         personal readonly IConfiguration _configuration;  
  10.         public FileController(IConfiguration configuration) {  
  11.             _configuration = configuration;  
  12.         }  
  13.     }  
  14. }   

Subsequent, we’re going to add a submit methodology the place we’re going to use UploadFromStreamAsync to add bytes.

 

Within the Publish methodology, you see the IFormFile interface as enter which is able to get file particulars which might be posted, then the very first thing we’re going to get is the connection string (“BlobConnectionString“) from the appsettings.json file utilizing IConfiguration.

 

Subsequent, we’re going to retrieve the storage account from the connection string, after that we’re going to create a blob consumer, we’re going to use cloudBlobClient to Retrieve a reference to a container.

 

Then to retrieve posted file data, we’re going to use information.OpenReadStream and duplicate them to a reminiscence stream after which lastly we’re going to get the byte array of it to move the UploadFromStreamAsync methodology.

 

Add File [Post]methodology

  1. [HttpPost(nameof(UploadFile))]  
  2. public async Process < IActionResult > UploadFile(IFormFile information) {  
  3.     string systemFileName = information.FileName;  
  4.     string blobstorageconnection = _configuration.GetValue < string > (“BlobConnectionString”);  
  5.       
  6.     CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);  
  7.       
  8.     CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();  
  9.       
  10.     CloudBlobContainer container = blobClient.GetContainerReference(_configuration.GetValue < string > (“BlobContainerName”));  
  11.       
  12.     CloudBlockBlob blockBlob = container.GetBlockBlobReference(systemFileName);  
  13.     await utilizing(var knowledge = information.OpenReadStream()) {  
  14.         await blockBlob.UploadFromStreamAsync(knowledge);  
  15.     }  
  16.     return Okay(“File Uploaded Efficiently”);  
  17. }   

Obtain File [Post] methodology

 

To obtain the file from the blob container we have to move the filename as parameter in order that the filename will examine and get again with the precise file from the container.

  1. [HttpPost(nameof(DownloadFile))]  
  2. public async Process < IActionResult > DownloadFile(string fileName) {  
  3.     CloudBlockBlob blockBlob;  
  4.     await utilizing(MemoryStream memoryStream = new MemoryStream()) {  
  5.         string blobstorageconnection = _configuration.GetValue < string > (“BlobConnectionString”);  
  6.         CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);  
  7.         CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();  
  8.         CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(_configuration.GetValue < string > (“BlobContainerName”));  
  9.         blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);  
  10.         await blockBlob.DownloadToStreamAsync(memoryStream);  
  11.     }  
  12.     Stream blobStream = blockBlob.OpenReadAsync().Outcome;  
  13.     return File(blobStream, blockBlob.Properties.ContentType, blockBlob.Identify);  
  14. }   

Delete File [Delete] Technique

  1. [HttpDelete(nameof(DeleteFile))]  
  2. public async Process < IActionResult > DeleteFile(string fileName) {  
  3.     string blobstorageconnection = _configuration.GetValue < string > (“BlobConnectionString”);  
  4.     CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);  
  5.     CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();  
  6.     string strContainerName = _configuration.GetValue < string > (“BlobContainerName”);  
  7.     CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(strContainerName);  
  8.     var blob = cloudBlobContainer.GetBlobReference(fileName);  
  9.     await blob.DeleteIfExistsAsync();  
  10.     return Okay(“File Deleted”);  
  11. }   

Now let’s run the appliance and add the file to Azure blob storage by Swagger

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

 

Let’s have a look at the file get uploaded to the Azure blob container. Open the storage account and click on on the container and open the container the place we are going to discover all of the uploaded information and their associated data.

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

 

Obtain File

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

 

Delete File

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

 

Now let’s return to the blob container and see the place the file will get deleted.

 

Azure Blob Storage - Upload/Download/Delete File(s) Using .NET 5

 

 

Thanks for studying, please let me know your questions, ideas, or suggestions within the feedback part. I recognize your suggestions and encouragement.

continue to learn….!

Show More

Related Articles

Leave a Reply

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

Back to top button