简体   繁体   English

在 ASP.NET Core MVC 中从 Azure blob 容器提供 static 个文件

[英]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:请检查以下参考资料:

  • Providing the files in asp.net mvc via Azure Blob Storage may be achieved by using the NuGet package Strathweb.AspNetCore.AzureBlobFileProvider通过 Azure 提供 asp.net mvc 中的文件 Blob 存储可以通过使用 NuGet package Strathweb.AspNetCore.AzureBlobFileProvider来实现
  • Check if you can place your static content in public containers with read access.检查您是否可以将您的 static 内容放在具有读取权限的公共容器中。

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 - CodeProjectAzure .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.

相关问题 将 blob 从一个 Azure 容器复制到另一个使用 ASP.NET Core 6.0 MVC 和 C# - Copy blobs from one Azure container to another using ASP.NET Core 6.0 MVC with C# 在 ASP.NET Core MVC 中从 Cognito 注销时出错 - Error logging out from Cognito in ASP.NET Core MVC ASP.NET Core - 从不同端口同时服务 API 和 SPA React - ASP.NET Core - Serve both API and SPA React from different port 从ASP.NET Core访问Azure App Service ConnectionString - Access Azure App Service ConnectionString from ASP.NET Core 有没有办法使用 Azure AD 令牌对 ASP.NET Core MVC controller 操作进行身份验证? - Is there a way to authenticate an ASP.NET Core MVC controller action using an Azure AD token? 如果项目使用 Azure AD,当前用户 Id 是否可以在 ASP.NET Core 5 MVC controller 中访问? - Is the current user Id accessible in an ASP.NET Core 5 MVC controller if the project uses Azure AD? 将 Asp.net 核心 mvc 应用程序从 visual studio 重新发布到 azure 应用程序服务是否会生成指向实时网站的新链接或与以前的链接相同? - Does republishing Asp.net core mvc app from visual studio to azure app service generate a new link to live website or same as previous? .NET 5 ASP.NET 核心应用程序未在 Azure 中启动 - .NET 5 ASP.NET Core App not starting in Azure 使用 .Net Core API 从 Azure Blob 存储异步流式传输视频 - Asynchronously Streaming Video with .Net Core API from Azure Blob Storage Matillion:从 Azure Blob 存储容器和 Windows 文件共享中删除文件 - Matillion: Delete files from Azure Blob Storage Container and Windows Fileshare
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM