Search code examples
azureasp.net-coreload-balancingweb-hostingimage-scaling

saving Image to file system in Asp.net Core


I'm building an application that saves a lot of images using the C# Web API of ASP.NET.

The best way seems to save the images in the file system and save their path into the database.

However, I am concerned about load balancing. Since every server will put the image in its own file system, how can another server behind the same load balancer retrieve the image?


Solution

  • If you have resources for it, I would state that:

    the best way is to save them in the file system and save the image path into the database

    Is not true at all.

    Instead, Id say using an existing file server system is probably going to produce the best results, if you are willing to pay for the service.

    For dotnet the 'go to' would be Azure Blob Storage, which is ideal for non-streamed data like images.

    https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet

    Otherwise, you can try and create your own file storage service from scratch. In which case you will effectively be creating a separate service API apart from your main cluster that handles your actual app, this secondary API just handles file storage and runs on its own dedicated server.

    You then simply just create an association between Id <-> File Data on the File Server, and you're App Servers can request and push files to the File Server via those Ids.

    Its possible but a File Server is for sure one of those types of projects that seems straightforward at first but very quickly you realize its a very very difficult task and it may have been easier to just pay for an existing service.

    There might be existing self hosted file server options out there as well!