'Serve static files from Azure blob container in ASP.NET Core MVC
I store static files (js, css and images etc) for web application in an Azure Blob container and want to use those files in application with app.UseStaticFiles. Is this possible?
Previously I used PhysicalFileProvider to map folder path, but now I want to map one Azure Blob container.
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider("c:/folder"),
RequestPath = new PathString("/cdn"),
ServeUnknownFileTypes = true
});
Solution 1:[1]
Please check the below references:
- Providing the files in asp.net mvc via Azure Blob Storage may be
achieved by using the NuGet package
Strathweb.AspNetCore.AzureBlobFileProvider - Check if you can place your static content in public containers with read access.
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var blobOptions = new AzureBlobOptions
{
ConnectionString = "{CONNECTION STRING}",
DocumentContainer = "{BLOB CONTAINER NAME}"
}
var azureBlobFileProvider = new AzureBlobFileProvider(blobOptions);
services.AddSingleton(azureBlobFileProvider);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var blobFileProvider = app.ApplicationServices.GetRequiredService<AzureBlobFileProvider>();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = blobFileProvider,
RequestPath = "/files"
});
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
FileProvider = blobFileProvider,
RequestPath = "/files"
});
}
}
Reference: GitHub -Strathweb.AspNetCore.AzureBlobFileProvider
See from Azure .NET Core Web App and Where to Locate Static Files - CodeProject
You could also use a combination of the options, using the local static files via the PhysicalFileProvider on Development, and trying to then switch to Blob storage in production .
Example:
// static files under wwwroot handled normally
app.UseStaticFiles();
// The path in the url we will manage
var staticFilesPath =
Configuration.GetSection("StaticFilesPath").Value;
if (env.IsDevelopment())
{
// The local/network folder containing static files we are mapping to
var staticFilesFolder =
Configuration.GetSection("StaticFilesFolder").Value;
if (!string.IsNullOrWhiteSpace(staticFilesFolder))
{
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(staticFilesFolder),
RequestPath = staticFilesPath
});
}
}
else
{
// map to blob storage for the path
var blobFileProvider =
app.ApplicationServices.GetRequiredService<AzureBlobFileProvider>();
if (blobFileProvider != null)
{
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = blobFileProvider,
RequestPath = staticFilesPath
});
}
}
Please refer Azure .NET Core Web App and Where to Locate Static Files – CodeHelper.Net
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kavyasaraboju-MT |
