Upload File Into Azure Blob Storage And Trigger Blob Storage Using Azure Function

In this article, we will create an azure storage account and blob container inside which we upload files using .NET Core Web API and then create one azure blob trigger function that process and save file into the database whenever the file is uploaded inside the azure blob storage.

Agenda

  • Overview
  • Create the Azure Storage Account
  • Implementation of .NET Core Web API
  • Azure Blob Trigger Function

Prerequisites

  • Visual Studio
  • SQL Server
  • Azure Account

Overview

Azure storage accounts have many data related that we use to store data efficiently like file storage, blob storage, azure queue, and many more that we use frequently.

  • Here we discuss azure blob storage which we use to store unstructured data such as text and binary.
  • Blob is scalable, durable, cost-effective, and secure.
  • It is designed for streaming audio and video, storing log files, and storing backups and images.

Azure Storage Account

Step 1

Search Storage account inside the Azure portal

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 2

Create and provide some configuration details

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 3

Here we can see the container section, click on it and create a new container.

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 4

Provide container name and other configuration details

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 5

Here we see our new container. Inside we are going to upload files

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 6

Next, Open the storage account and click on the Access Keys section. Inside you can find the connection string that we are going to use inside the Web API

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Implementation of .NET Core Web API

Step 1

Create a new .NET Core Web API

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 2

Configure your application

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 3

Provide additional information

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 4

Add File Details model class inside the project solution

namespace AzureBlobStorageDemo.Models {
    public class FileDetails {
        public IFormFile file {
            get;
            set;
        }
    }
}

Step 5

Install Azure Storage Blob NuGet

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 6

Next, add a blob storage interface inside the repository folder

using AzureBlobStorageDemo.Models;
using System.IO;
namespace AzureBlobStorageDemo.Repositories {
    public interface IBlobStorage {
        Task UploadFileAsync(FileDetails fileDetails);
    }
}

Step 7

Provide the implementation inside the blob storage class

using Azure.Storage.Blobs;
using AzureBlobStorageDemo.Models;
using System.IO;
using System.Reflection;
namespace AzureBlobStorageDemo.Repositories {
    public class BlobStorage: IBlobStorage {
        private readonly BlobServiceClient _blobService;
        private readonly IConfiguration _configuration;
        public BlobStorage(BlobServiceClient blobService, IConfiguration configuration) {
            _blobService = blobService;
            _configuration = configuration;
        }
        public async Task UploadFileAsync(FileDetails fileDetails) {
            //get the blob container
            var blobStorageContainerName = _blobService.GetBlobContainerClient(_configuration.GetValue < string > ("BlobContainer"));
            //get the blob client
            var blobStorageClient = blobStorageContainerName.GetBlobClient(fileDetails.file.FileName);
            //read file stream
            var streamContent = fileDetails.file.OpenReadStream();
            //upload file
            await blobStorageClient.UploadAsync(streamContent);
        }
    }
}

Step 8

Add blob storage connection string and container name inside the app settings file.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "AzureBlobStorageKey": ""
  },
  "BlobContainer": "jdlearningblobcontainer"
}

Step 9

Register a few services inside the Program class

using Azure.Storage.Blobs;
using AzureBlobStorageDemo.Repositories;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped(_ => {
    return new BlobServiceClient(builder.Configuration.GetConnectionString("AzureBlobStorageKey"));
});
builder.Services.AddScoped < IBlobStorage, BlobStorage > ();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Step 10

Create a new files controller

using AzureBlobStorageDemo.Models;
using AzureBlobStorageDemo.Repositories;
using Microsoft.AspNetCore.Mvc;
namespace AzureBlobStorageDemo.Controllers {
    [Route("api/[controller]")]
    [ApiController]
    public class FilesController: ControllerBase {
        private readonly IBlobStorage _blobService;
        public FilesController(IBlobStorage blobService) {
            _blobService = blobService;
        }
        /// <summary>
        /// upload file
        /// </summary>
        /// <param name="fileDetail"></param>
        /// <returns></returns>
        [HttpPost("Upload")]
        public async Task < IActionResult > UploadFile([FromForm] FileDetails fileDetail) {
            if (fileDetail.file != null) {
                await _blobService.UploadFileAsync(fileDetail);
            }
            return Ok();
        }
    }
}

Step 11

Run your project

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Azure Blob Trigger Function

Step 1

Open visual studio and create a new azure function

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 2

Configure your new function

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 3

Provide some additional information like connection string setting name and container name.

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 4

We need the following NuGet Packages.

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Step 5

Create a new Blob Details class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AzureBlobTriggerFunction {
    public class BlobDetails {
        public Guid Id {
            get;
            set;
        }
        public string FileName {
            get;
            set;
        }
        public bool isFileUploaded {
            get;
            set;
        }
        public DateTime DateOfUpdation {
            get;
            set;
        }
    }
}

Step 6

Add blob storage connection string inside the local setting JSON file

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureBlobStorageKey": ""
  }
}

Step 7

Finally, add the following code inside the function which triggers blob storage and saves the uploaded file data inside the SQL Server Database. Here I added SQL Code, but in real-time if you want to process your data and need to send it anywhere, you can add code as per your requirement.

using System;
using System.Data.SqlClient;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace AzureBlobTriggerFunction {
    public class BlobTrigger {
        [FunctionName("BlobTrigger")]
        public void Run([BlobTrigger("jdlearningblobcontainer/{name}", Connection = "AzureBlobStorageKey")] Stream myBlob, string name, ILogger log) {
            log.LogInformation($ "C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
            using(SqlConnection conn = new SqlConnection("Data Source=DESKTOP-8RL8JOG;Initial Catalog=AzureDemo;User Id=sa;Password=database@1;")) {
                conn.Open();
                var insertQuery = $ "INSERT INTO [BlobDetails] (FileName,isFileUploaded,DateOfUpdation) VALUES('{name}', '{true}' , '{DateTime.UtcNow}')";
                using(SqlCommand cmd = new SqlCommand(insertQuery, conn)) {
                    var rows = cmd.ExecuteNonQuery();
                    log.LogInformation($ "{rows} rows were updated");
                }
            }
        }
    }
}

Step 8

Now, run your function application.

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

You can see in the above image our function is in the running add listen to blob storage.

Step 9

Upload a file using the Web API application

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

After the file is uploaded on Azure, our function triggers blob storage, receives the file details, and stores them inside the SQL Server Database Table.

(Note: This is just for an example purpose. If you want to process your data and perform some operations, then add that code as per your requirement.

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Upload File into Azure Blob Storage and Trigger Blob Storage using Azure Function

Conclusion

Here we discuss the azure storage account and blob trigger functions. Also, the step-by-step implementation using .NET Core.

Happy Learning!


Similar Articles