Blake Rivell
Blake Rivell

Reputation: 13875

Uploading files to a directory with an ASP.NET Core Web Application hosted on Azure

I am using Azure App Service with Azure SQL Database to host an ASP.NET Core Web Application.

This application involves uploading documents to a directory. On my local dev machine I am simply using:

var fileUploadDir = $"C:\\FileUploads";

On Azure what feature would I use to create a directory structure and place files in the directory structure using:

var filePath = Path.Combine(fileUploadDir, formFile.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
     await formFile.CopyToAsync(stream);
}

What Azure feature would I use and is there an API for file system actions or can I simply update the fileUploadDir that my existing code uses with an Azure directory path?

Upvotes: 0

Views: 1794

Answers (1)

NicoD
NicoD

Reputation: 1189

With an Azure App Service you can upload your file the same way. You just have to create your directory in the wwwroot folder. If you got multiple instances, this folder will be shared between them as stated in the documentation File access across multiple instances :

File access across multiple instances The home directory contains an app's content, and application code can write to it. If an app runs on multiple instances, the home directory is shared among all instances so that all instances see the same directory. So, for example, if an app saves uploaded files to the home directory, those files are immediately available to all instances.

Nevertheless, depending on the need of your application, a better solution could be to use a blob storage to manage your files especially if they must persist. The use of blobs can also be useful if you want to trigger async treatments with azure function after the upload for instance.

For short duration process with temporary files I used the home directory without any issue. As soon as the processing can be long, or if I want to keep the files, I tend to use asynchronous processing and the blob storage.

The blob storage solves the problems of access to the files in the home directory by users, allows to rely on a service dedicated to the storage and not a simple storage of type file system related to app service. Writing, deleting is simple and provides many other possibilities: direct access via REST service, access via shared access signature, async processing ...

Upvotes: 2

Related Questions