├── .gitignore ├── LICENSE ├── README.md ├── Strathweb.AspNetCore.AzureBlobFileProvider.sln └── src ├── SampleWeb ├── Program.cs ├── SampleWeb.csproj └── Startup.cs └── Strathweb.AspNetCore.AzureBlobFileProvider ├── AzureBlobDirectoryContents.cs ├── AzureBlobFileInfo.cs ├── AzureBlobFileProvider.cs ├── AzureBlobOptions.cs ├── DefaultBlobContainerFactory.cs ├── IBlobContainerFactory.cs └── Strathweb.AspNetCore.AzureBlobFileProvider.csproj /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Filip W 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Strathweb.AspNetCore.AzureBlobFileProvider 2 | 3 | Azure Blob Storage file provider (`IFileProvider`) for ASP.NET Core. 4 | 5 | ### Installation 6 | 7 | ``` 8 | Install-Package Strathweb.AspNetCore.AzureBlobFileProvider 9 | ``` 10 | 11 | ### Usage 12 | 13 | Configure access to your Blob Storage via storage account connection string or SAS token. 14 | 15 | Below is the usage example for both flows - where access to files from Blob Storage is enabled on the `/files` route (including directory browsing in the browser). 16 | 17 | **Connection string** 18 | 19 | ```csharp 20 | public class Startup 21 | { 22 | public Startup(IConfiguration configuration) 23 | { 24 | Configuration = configuration; 25 | } 26 | 27 | public IConfiguration Configuration { get; } 28 | 29 | public void ConfigureServices(IServiceCollection services) 30 | { 31 | var blobOptions = new AzureBlobOptions 32 | { 33 | ConnectionString = "{my connection string}", 34 | DocumentContainer = "{blob container name}" 35 | } 36 | 37 | var azureBlobFileProvider = new AzureBlobFileProvider(blobOptions); 38 | services.AddSingleton(azureBlobFileProvider); 39 | } 40 | 41 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 42 | { 43 | var blobFileProvider = app.ApplicationServices.GetRequiredService(); 44 | app.UseStaticFiles(new StaticFileOptions() 45 | { 46 | FileProvider = blobFileProvider, 47 | RequestPath = "/files" 48 | }); 49 | 50 | app.UseDirectoryBrowser(new DirectoryBrowserOptions 51 | { 52 | FileProvider = blobFileProvider, 53 | RequestPath = "/files" 54 | }); 55 | } 56 | } 57 | ``` 58 | 59 | **Token** (need to provide the URL of the storage separately) 60 | 61 | ```csharp 62 | public class Startup 63 | { 64 | public Startup(IConfiguration configuration) 65 | { 66 | Configuration = configuration; 67 | } 68 | 69 | public IConfiguration Configuration { get; } 70 | 71 | public void ConfigureServices(IServiceCollection services) 72 | { 73 | var blobOptions = new AzureBlobOptions 74 | { 75 | BaseUri = "{base URL of the storage account}", 76 | Token = "{SAS token}", 77 | DocumentContainer = "{blob container name}" 78 | } 79 | 80 | var azureBlobFileProvider = new AzureBlobFileProvider(blobOptions); 81 | services.AddSingleton(azureBlobFileProvider); 82 | } 83 | 84 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 85 | { 86 | var blobFileProvider = app.ApplicationServices.GetRequiredService(); 87 | app.UseStaticFiles(new StaticFileOptions() 88 | { 89 | FileProvider = blobFileProvider, 90 | RequestPath = "/files" 91 | }); 92 | 93 | app.UseDirectoryBrowser(new DirectoryBrowserOptions 94 | { 95 | FileProvider = blobFileProvider, 96 | RequestPath = "/files" 97 | }); 98 | } 99 | } 100 | ``` 101 | 102 | In case both `ConnectionString` and `Token` are present, connection string is given the preference. 103 | 104 | ### Current limitations 105 | 106 | The watch functionality of the file provider is currently not supported. 107 | -------------------------------------------------------------------------------- /Strathweb.AspNetCore.AzureBlobFileProvider.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27703.2000 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Strathweb.AspNetCore.AzureBlobFileProvider", "src\Strathweb.AspNetCore.AzureBlobFileProvider\Strathweb.AspNetCore.AzureBlobFileProvider.csproj", "{B6CE1911-C8FE-474D-9865-1D63961F94B4}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E182D482-D1CB-45FE-946D-993FD35240BD}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWeb", "src\SampleWeb\SampleWeb.csproj", "{6CC7C3DB-E4B3-42DD-B4E7-867A638F40DC}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {B6CE1911-C8FE-474D-9865-1D63961F94B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {B6CE1911-C8FE-474D-9865-1D63961F94B4}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {B6CE1911-C8FE-474D-9865-1D63961F94B4}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {B6CE1911-C8FE-474D-9865-1D63961F94B4}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {6CC7C3DB-E4B3-42DD-B4E7-867A638F40DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {6CC7C3DB-E4B3-42DD-B4E7-867A638F40DC}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {6CC7C3DB-E4B3-42DD-B4E7-867A638F40DC}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {6CC7C3DB-E4B3-42DD-B4E7-867A638F40DC}.Release|Any CPU.Build.0 = Release|Any CPU 26 | EndGlobalSection 27 | GlobalSection(SolutionProperties) = preSolution 28 | HideSolutionNode = FALSE 29 | EndGlobalSection 30 | GlobalSection(NestedProjects) = preSolution 31 | {6CC7C3DB-E4B3-42DD-B4E7-867A638F40DC} = {E182D482-D1CB-45FE-946D-993FD35240BD} 32 | EndGlobalSection 33 | GlobalSection(ExtensibilityGlobals) = postSolution 34 | SolutionGuid = {47371F15-1873-4620-9CA6-1030D559449A} 35 | EndGlobalSection 36 | EndGlobal 37 | -------------------------------------------------------------------------------- /src/SampleWeb/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace SampleWeb 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | CreateWebHostBuilder(args).Build().Run(); 18 | } 19 | 20 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/SampleWeb/SampleWeb.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/SampleWeb/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.AspNetCore.Http; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.DependencyInjection; 10 | using Strathweb.AspNetCore.AzureBlobFileProvider; 11 | 12 | namespace SampleWeb 13 | { 14 | public class Startup 15 | { 16 | public Startup(IConfiguration configuration) 17 | { 18 | Configuration = configuration; 19 | } 20 | 21 | public IConfiguration Configuration { get; } 22 | 23 | public void ConfigureServices(IServiceCollection services) 24 | { 25 | var blobOptions = Configuration.GetSection("AzureBlobOptions").Get(); 26 | var azureBlobFileProvider = new AzureBlobFileProvider(blobOptions); 27 | services.AddSingleton(azureBlobFileProvider); 28 | } 29 | 30 | public void Configure(IApplicationBuilder app) 31 | { 32 | var blobFileProvider = app.ApplicationServices.GetRequiredService(); 33 | app.UseStaticFiles(new StaticFileOptions() 34 | { 35 | FileProvider = blobFileProvider, 36 | RequestPath = "/files" 37 | }); 38 | 39 | app.UseDirectoryBrowser(new DirectoryBrowserOptions 40 | { 41 | FileProvider = blobFileProvider, 42 | RequestPath = "/files" 43 | }); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Strathweb.AspNetCore.AzureBlobFileProvider/AzureBlobDirectoryContents.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Microsoft.Azure.Storage.Blob; 5 | using Microsoft.Extensions.FileProviders; 6 | 7 | namespace Strathweb.AspNetCore.AzureBlobFileProvider 8 | { 9 | public class AzureBlobDirectoryContents : IDirectoryContents 10 | { 11 | private List _blobs = new List(); 12 | public bool Exists { get; set; } 13 | 14 | public AzureBlobDirectoryContents(CloudBlobDirectory blob) 15 | { 16 | BlobContinuationToken continuationToken = null; 17 | 18 | do 19 | { 20 | var response = blob.ListBlobsSegmented(continuationToken); 21 | continuationToken = response.ContinuationToken; 22 | _blobs.AddRange(response.Results); 23 | } 24 | while (continuationToken != null); 25 | Exists = _blobs.Count > 0; 26 | } 27 | 28 | public IEnumerator GetEnumerator() 29 | { 30 | return _blobs.Select(blob => new AzureBlobFileInfo(blob)).GetEnumerator(); 31 | } 32 | 33 | IEnumerator IEnumerable.GetEnumerator() 34 | { 35 | return GetEnumerator(); 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/Strathweb.AspNetCore.AzureBlobFileProvider/AzureBlobFileInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using Microsoft.Azure.Storage.Blob; 4 | using Microsoft.Extensions.FileProviders; 5 | 6 | namespace Strathweb.AspNetCore.AzureBlobFileProvider 7 | { 8 | public class AzureBlobFileInfo : IFileInfo 9 | { 10 | private readonly CloudBlockBlob _blockBlob; 11 | 12 | public AzureBlobFileInfo(IListBlobItem blob) 13 | { 14 | switch (blob) 15 | { 16 | case CloudBlobDirectory d: 17 | Exists = true; 18 | IsDirectory = true; 19 | Name = ((CloudBlobDirectory)blob).Prefix.TrimEnd('/'); 20 | break; 21 | 22 | case CloudBlockBlob b: 23 | _blockBlob = b; 24 | Name = !string.IsNullOrEmpty(b.Parent.Prefix) ? b.Name.Replace(b.Parent.Prefix, "") : b.Name; 25 | Exists = b.Exists(); 26 | if (Exists) 27 | { 28 | b.FetchAttributes(); 29 | Length = b.Properties.Length; 30 | LastModified = b.Properties.LastModified ?? DateTimeOffset.MinValue; 31 | } 32 | else 33 | { 34 | Length = -1; 35 | // IFileInfo.PhysicalPath docs say: Return null if the file is not directly accessible. 36 | // (PhysicalPath should maybe also be null for blobs that do exist but that would be a potentially breaking change.) 37 | PhysicalPath = null; 38 | } 39 | break; 40 | } 41 | } 42 | 43 | public Stream CreateReadStream() => _blockBlob.OpenRead(); 44 | 45 | public bool Exists { get; } 46 | public long Length { get; } 47 | public string PhysicalPath { get; } 48 | public string Name { get; } 49 | public DateTimeOffset LastModified { get; } 50 | public bool IsDirectory { get; } 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /src/Strathweb.AspNetCore.AzureBlobFileProvider/AzureBlobFileProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.FileProviders; 3 | using Microsoft.Extensions.Primitives; 4 | 5 | namespace Strathweb.AspNetCore.AzureBlobFileProvider 6 | { 7 | public class AzureBlobFileProvider : IFileProvider 8 | { 9 | private readonly IBlobContainerFactory _blobContainerFactory; 10 | 11 | public AzureBlobFileProvider(IBlobContainerFactory blobContainerFactory) 12 | { 13 | _blobContainerFactory = blobContainerFactory; 14 | } 15 | 16 | public AzureBlobFileProvider(AzureBlobOptions azureBlobOptions) 17 | { 18 | _blobContainerFactory = new DefaultBlobContainerFactory(azureBlobOptions); 19 | } 20 | 21 | public IDirectoryContents GetDirectoryContents(string subpath) 22 | { 23 | var container = _blobContainerFactory.GetContainer(subpath); 24 | var blob = container.GetDirectoryReference(_blobContainerFactory.TransformPath(subpath)); 25 | return new AzureBlobDirectoryContents(blob); 26 | } 27 | 28 | public IFileInfo GetFileInfo(string subpath) 29 | { 30 | var container = _blobContainerFactory.GetContainer(subpath); 31 | var blob = container.GetBlockBlobReference(_blobContainerFactory.TransformPath(subpath)); 32 | return new AzureBlobFileInfo(blob); 33 | } 34 | 35 | public IChangeToken Watch(string filter) => throw new NotImplementedException(); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/Strathweb.AspNetCore.AzureBlobFileProvider/AzureBlobOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Strathweb.AspNetCore.AzureBlobFileProvider 4 | { 5 | public class AzureBlobOptions 6 | { 7 | public Uri BaseUri { get; set; } 8 | 9 | public string Token { get; set; } 10 | 11 | public string DocumentContainer { get; set; } 12 | 13 | public string ConnectionString { get; set; } 14 | } 15 | } -------------------------------------------------------------------------------- /src/Strathweb.AspNetCore.AzureBlobFileProvider/DefaultBlobContainerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Azure.Storage; 3 | using Microsoft.Azure.Storage.Auth; 4 | using Microsoft.Azure.Storage.Blob; 5 | 6 | namespace Strathweb.AspNetCore.AzureBlobFileProvider 7 | { 8 | public class DefaultBlobContainerFactory : IBlobContainerFactory 9 | { 10 | private readonly CloudBlobContainer _container; 11 | 12 | public DefaultBlobContainerFactory(AzureBlobOptions azureBlobOptions) 13 | { 14 | CloudBlobClient blobClient; 15 | if (azureBlobOptions.ConnectionString != null && CloudStorageAccount.TryParse(azureBlobOptions.ConnectionString, out var cloudStorageAccount)) 16 | { 17 | blobClient = cloudStorageAccount.CreateCloudBlobClient(); 18 | } 19 | else if (azureBlobOptions.BaseUri != null && azureBlobOptions.Token != null) 20 | { 21 | blobClient = new CloudBlobClient(azureBlobOptions.BaseUri, new StorageCredentials(azureBlobOptions.Token)); 22 | } 23 | else 24 | { 25 | throw new ArgumentException("One of the following must be set: 'ConnectionString' or 'BaseUri'+'Token'!"); 26 | } 27 | 28 | _container = blobClient.GetContainerReference(azureBlobOptions.DocumentContainer); 29 | } 30 | 31 | public CloudBlobContainer GetContainer(string subpath) 32 | { 33 | return _container; 34 | } 35 | 36 | public string TransformPath(string subpath) 37 | { 38 | return subpath.TrimStart('/').TrimEnd('/'); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Strathweb.AspNetCore.AzureBlobFileProvider/IBlobContainerFactory.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Azure.Storage.Blob; 2 | 3 | namespace Strathweb.AspNetCore.AzureBlobFileProvider 4 | { 5 | public interface IBlobContainerFactory 6 | { 7 | CloudBlobContainer GetContainer(string subpath); 8 | string TransformPath(string subpath); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/Strathweb.AspNetCore.AzureBlobFileProvider/Strathweb.AspNetCore.AzureBlobFileProvider.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0 5 | Azure Blob Storage file provider for ASP.NET Core. 6 | 0.5.0 7 | filipw 8 | netstandard2.0 9 | azure;blob;storage;aspnetcore;mvc 10 | https://raw.githubusercontent.com/filipw/Strathweb.TypedRouting.AspNetCore/master/strathweb.png 11 | https://github.com/filipw/Strathweb.AspNetCore.AzureBlobFileProvider 12 | MIT 13 | git 14 | https://github.com/filipw/Strathweb.AspNetCore.AzureBlobFileProvider.git 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | --------------------------------------------------------------------------------