[英]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
.我将 web 应用程序的 static 个文件(js、css 和图像等)存储在一个 Azure Blob 容器中,并希望在带有
app.UseStaticFiles
的应用程序中使用这些文件。 Is this possible?这可能吗?
Previously I used PhysicalFileProvider
to map folder path, but now I want to map one Azure Blob container.以前我使用
PhysicalFileProvider
到 map 文件夹路径,但现在我想要 map 一个 Azure Blob 容器。
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider("c:/folder"),
RequestPath = new PathString("/cdn"),
ServeUnknownFileTypes = true
});
Please check the below references:请检查以下参考资料:
Strathweb.AspNetCore.AzureBlobFileProvider
Strathweb.AspNetCore.AzureBlobFileProvider
来实现 public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration;公共 class 启动 { 公共启动(IConfiguration 配置){ 配置 = 配置; }
}
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参考: GitHub -Strathweb.AspNetCore.AzureBlobFileProvider
See from Azure .NET Core Web App and Where to Locate Static Files - CodeProject从Azure .NET 核心 Web 应用程序和在哪里找到 Static 文件 - 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.您还可以使用这些选项的组合,通过开发中的 PhysicalFileProvider 使用本地 static 文件,然后尝试在生产中切换到 Blob 存储。
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请参考Azure .NET Core Web App 和 Static 文件的位置 – CodeHelper.Net
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.