-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAzureStorageAspNetCoreBuilderExtensions.cs
60 lines (52 loc) · 2.5 KB
/
AzureStorageAspNetCoreBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Bet.Extensions.AzureStorage;
using Bet.Extensions.AzureStorage.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Builder
{
public static class AzureStorageAspNetCoreBuilderExtensions
{
/// <summary>
/// Uses Azure Storage Blob container for service "Static Files".
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
/// <param name="namedContainer">The name of the named container registry. The same type can have several named options.</param>
/// <returns></returns>
public static IApplicationBuilder UseAzureStorageForStaticFiles(this IApplicationBuilder app, string namedContainer = "")
{
return app.UseAzureStorageForStaticFiles<StorageFileProviderOptions>(namedContainer);
}
/// <summary>
/// Uses Azure Storage Blob Container for serving `Static File`.
/// </summary>
/// <typeparam name="TOptions">The type of the configuration object to be used to register the Azure Storage Blob container.</typeparam>
/// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
/// <param name="namedContainer">The name of the named container registry. The same type can have several named options.</param>
/// <returns></returns>
public static IApplicationBuilder UseAzureStorageForStaticFiles<TOptions>(
this IApplicationBuilder app,
string namedContainer = "")
where TOptions : StorageFileProviderOptions
{
var sp = app.ApplicationServices;
var options = sp.GetRequiredService<IOptionsMonitor<TOptions>>().Get(namedContainer);
var storageOptions = sp.GetRequiredService<IOptionsMonitor<StorageAccountOptions>>()
.Get(options.AccountName);
var azureFileProvider = new StorageFileProvider(storageOptions, options.ContainerName);
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = azureFileProvider,
RequestPath = options.RequestPath
});
if (options.EnableDirectoryBrowsing)
{
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
FileProvider = azureFileProvider,
RequestPath = options.RequestPath
});
}
return app;
}
}
}