Upload files in Azure blob storage using ASP.Net core Web API

Azure blob storage service allows user to create / update / upload unstructured data in Azure. Later, user can access / download this data from anywhere in the world. This data can be Audio, Video, Image, Text or Log file. We can also use these service for streaming video or audio files. Users are directly allow to create / upload blob from Azure portal or developer can also create / upload blob via code. We can access these blobs via HTTP / HTTPS service also.

Container: You can consider container as Folder / Directory in file system. So it means each container can have multiple blobs of same or different type.

Storage account: Storage account contains all storage data objects like blob, queue, table etc.

navigation
Relationship between resources

There are mainly three types of blobs available on Azure:

  1. Block blob: Block blobs includes features that help you manage large files over network. E.g. Image file, video file etc.
  2. Page blob: Page blobs store the virtual hard drive (VHD) files serve as disks for Azure virtual machines.
  3. Append blob: Mostly, we are using this type of blob for appending content to existing file. E.g. Log file

Lets upload new image to Azure using C# Web API. We are going to upload block blob here.

  1. Create Azure free account if you don’t have subscription.
  2. Need to create “Storage account” in Azure portal.
  3. Create Web API project. I have choose .Net core 2.1 for this example.
  4. Install “Microsoft.Azure.Storage.Blob” nuget package to this project.
  5. Please provide Azure storage account connection string in appsettings.json file. If we want to test it locally then we can provide below connection string rather than to provide actual one.
connection string
Azure storage account connection string
  • Create HttpPost action method in UserController which is capable to receive file as parameter. See whole implementation of UserController below.
  • userController
    User controller

    I’ve upload this solution to Github here. Feel free to clone the git repository.

    Leave a Reply

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