MrrHak.Extensions.FileProviders.S3FileProvider
1.2.1
Prefix Reserved
dotnet add package MrrHak.Extensions.FileProviders.S3FileProvider --version 1.2.1
NuGet\Install-Package MrrHak.Extensions.FileProviders.S3FileProvider -Version 1.2.1
<PackageReference Include="MrrHak.Extensions.FileProviders.S3FileProvider" Version="1.2.1" />
<PackageVersion Include="MrrHak.Extensions.FileProviders.S3FileProvider" Version="1.2.1" />
<PackageReference Include="MrrHak.Extensions.FileProviders.S3FileProvider" />
paket add MrrHak.Extensions.FileProviders.S3FileProvider --version 1.2.1
#r "nuget: MrrHak.Extensions.FileProviders.S3FileProvider, 1.2.1"
#addin nuget:?package=MrrHak.Extensions.FileProviders.S3FileProvider&version=1.2.1
#tool nuget:?package=MrrHak.Extensions.FileProviders.S3FileProvider&version=1.2.1
S3 File Provider
This package represents a file provider with an Amazon S3 bucket. It constructs virtual file systems that implement IFileProvider and integrate with AWS S3 SDK to provide the functionality for serving static files in ASP.NET.
Where can I get it?
First, install NuGet. Then, install MrrHak.Extensions.FileProviders.S3FileProvider from the package manager console:
PM> NuGet\Install-Package MrrHak.Extensions.FileProviders.S3FileProvider
Or from the .NET CLI as:
dotnet add package MrrHak.Extensions.FileProviders.S3FileProvider
How do I get started?
.Net Core Configuration
Creating a S3FileProvider
instance is very simple:
var s3FileProvider = new S3FileProvider(amazonS3, bucketName);
Or using GetS3FileProvider
extension method
var s3FileProvider = app.Services.GetS3FileProvider(bucketName);
Using S3 for serve as static files
First, configure S3FileProvider
with UseStaticFiles
, in the program/startup of your application:
var s3FileProvider = new S3FileProvider(amazonS3, bucketName);
var staticFilesOption = new StaticFileOptions(){ FileProvider = s3FileProvider};
app.UseStaticFiles(staticFilesOption);
Or using UseS3StaticFiles
extension method (recommended)
Parameter | Type | Required | Default Value | Description |
---|---|---|---|---|
bucketName |
string |
Yes | The name of the S3 bucket | |
requestPath |
string |
No | null |
The request path for the static files |
rootPath |
string |
No | null |
The root path for the S3 bucket |
serveUnknownFileTypes |
bool |
No | false |
Whether to serve unknown file types |
app.UseS3StaticFiles(bucketName);
Note: AWSSDK.S3 is required to create an Amazon S3 client.
For Amazon S3 Service instance
var amazonS3 = new AmazonS3Client("AccessKeyId", "SecretAccessKey", Amazon.RegionEndpoint.APSoutheast1);
Or if you have already registered Amazon S3 services
var amazonS3 = app.Services.GetService<IAmazonS3>();
S3StaticFileOptions
Option | Type | Required | Default Value | Description |
---|---|---|---|---|
BucketName |
string |
Yes | empty |
The name of the S3 bucket |
RequestPath |
string |
No | null |
The relative request path that maps to static resources |
RootPath |
string |
No | null |
The root path for the S3 bucket |
ServeUnknownFileTypes |
bool |
No | false |
Whether to serve unknown file types |
DefaultContentType |
string |
No | null |
The default content type for a request if the ContentTypeProvider cannot determine one. None is provided by default, so the client must determine the format themselves |
ContentTypeProvider |
IContentTypeProvider |
No | null |
Used to map files to content-types |
OnPrepareResponse |
Action<PrepareResponseContext> |
No | null |
Called after the status code and headers have been set, but before the body has been written. This can be used to add or change the response headers |
Example
Program.cs
Register
IAmazonS3
client toservices
collectionAWSOptions
is using fromAWSSDK.Extensions.NETCore.Setup
// This value should be get from appsettings.json const string S3_BUCKET_NAME = "bucket-name"; const string S3_ACCESS_KEY = "access-key"; const string S3_SECRET_KEY = "secret-key"; const string DEFAULT_REGION = "region"; // Get AWS profile info directly from configuration (Profile authentication) AWSOptions awsOptions = builder.Configuration.GetAWSOptions(); awsOptions.Region = RegionEndpoint.GetBySystemName(DEFAULT_REGION); builder.Services.AddDefaultAWSOptions(awsOptions); // IAM user authentication AWSOptions s3Options = awsOptions; if (!string.IsNullOrEmpty(S3_ACCESS_KEY) && !string.IsNullOrEmpty(S3_SECRET_KEY)) { s3Options = new AWSOptions() { Credentials = new BasicAWSCredentials(S3_ACCESS_KEY, S3_SECRET_KEY) }; s3Options.Region = RegionEndpoint.GetBySystemName(DEFAULT_REGION); } builder.Services.AddAWSService<IAmazonS3>(s3Options);
Register
S3FileProvider
withUseStaticFiles
var amazonS3 = app.Services.GetService<IAmazonS3>(); var s3FileProvider = new S3FileProvider(amazonS3, S3_BUCKET_NAME); var staticFilesOption = new StaticFileOptions(){ FileProvider = s3FileProvider}; app.UseStaticFiles(staticFilesOption);
Or using
UseS3StaticFiles
extension method (recommended)app.UseS3StaticFiles(S3_BUCKET_NAME);
.Net Framework Configuration
1. Install Required Packages for .Net Framework
dotnet add package Microsoft.Owin.Host.SystemWeb
2. Create Owin Startup class
public class Startup
{
public void Configuration(IAppBuilder app)
{
var s3Client = new AmazonS3Client("your-access-key", "your-secret-key", Amazon.RegionEndpoint.APSoutheast1);
app.UseS3StaticFiles(s3Client, "your-bucket-name");
// Another implementation
}
}
3. Update Web.config
<system.webServer>
<handlers>
<add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
</handlers>
</system.webServer>
Build and Test Source Code
dotnet build
dotnet test
Buy me a coffee
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- AWSSDK.S3 (>= 3.7.308.8)
- Microsoft.AspNetCore (>= 2.2.0)
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
- Microsoft.AspNetCore.StaticFiles (>= 2.2.0)
- Microsoft.Extensions.FileProviders.Abstractions (>= 8.0.0)
- Microsoft.Owin (>= 4.2.2)
- Microsoft.Owin.FileSystems (>= 4.2.2)
- Microsoft.Owin.StaticFiles (>= 4.2.2)
-
.NETStandard 2.0
- AWSSDK.S3 (>= 3.7.308.8)
- Microsoft.AspNetCore (>= 2.2.0)
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
- Microsoft.AspNetCore.StaticFiles (>= 2.2.0)
- Microsoft.Extensions.FileProviders.Abstractions (>= 8.0.0)
- Microsoft.Owin (>= 4.2.2)
- Microsoft.Owin.FileSystems (>= 4.2.2)
- Microsoft.Owin.StaticFiles (>= 4.2.2)
-
net6.0
- AWSSDK.S3 (>= 3.7.308.8)
- Microsoft.AspNetCore (>= 2.2.0)
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
- Microsoft.AspNetCore.StaticFiles (>= 2.2.0)
- Microsoft.Extensions.FileProviders.Abstractions (>= 8.0.0)
- Microsoft.Owin (>= 4.2.2)
- Microsoft.Owin.FileSystems (>= 4.2.2)
- Microsoft.Owin.StaticFiles (>= 4.2.2)
-
net7.0
- AWSSDK.S3 (>= 3.7.308.8)
- Microsoft.AspNetCore (>= 2.2.0)
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
- Microsoft.AspNetCore.StaticFiles (>= 2.2.0)
- Microsoft.Extensions.FileProviders.Abstractions (>= 8.0.0)
- Microsoft.Owin (>= 4.2.2)
- Microsoft.Owin.FileSystems (>= 4.2.2)
- Microsoft.Owin.StaticFiles (>= 4.2.2)
-
net8.0
- AWSSDK.S3 (>= 3.7.308.8)
- Microsoft.AspNetCore (>= 2.2.0)
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
- Microsoft.AspNetCore.StaticFiles (>= 2.2.0)
- Microsoft.Extensions.FileProviders.Abstractions (>= 8.0.0)
- Microsoft.Owin (>= 4.2.2)
- Microsoft.Owin.FileSystems (>= 4.2.2)
- Microsoft.Owin.StaticFiles (>= 4.2.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.1 | 1,063 | 6/4/2024 |
1.2.0 | 374 | 5/30/2024 |
1.1.0 | 193 | 5/10/2024 |
1.0.1 | 140 | 5/4/2024 |
1.0.0 | 150 | 4/25/2024 |
1.0.0-beta.7 | 73 | 4/24/2024 |
1.0.0-beta.6 | 62 | 4/23/2024 |
1.0.0-beta.5 | 65 | 4/22/2024 |
1.0.0-beta.4 | 64 | 4/19/2024 |
1.0.0-beta.3 | 65 | 4/19/2024 |
1.0.0-beta.2 | 69 | 4/19/2024 |
1.0.0-beta.1 | 63 | 4/18/2024 |
## 1.2.1 (04-06-2024)
- Add try catch to AWS S3 SDK
- Update test coverage
- Upgrade AWS S3 SDK
## 1.2.0 (30-05-2024)
- Allow passing rootPath to specify the root path for the S3 bucket
- Update test coverage
- Update README.md
- Upgrade dependencies
## 1.1.0 (10-05-2024)
- Add new overloads for UseS3StaticFiles extension with Owin for .Net Framework
- Add sample project for .Net Framework
- Add more test coverage
- Upgrade AWS S3 SDK
- Update GitHub Action commitlint
- Update README.md
## 1.0.1 (04-05-2024)
- Add new overloads for UseS3StaticFiles extension with S3StaticFileOptions
- Add local commitlint
- Add GitHub Action commitlint
- Upgrade AWS S3 SDK
- Update UseS3StaticFiles extension to allow passing requestPath and serveUnknownFileTypes
- Update README.md
## 1.0.0 (25-04-2024)
- Move to stable version
- Update README.md
- Add example project
- Add extensions helpers
- GetS3FileProvider
- UseS3StaticFiles
## 1.0.0-beta.7 (24-04-2024)
- Improvement
- Update README.md
## 1.0.0-beta.6 (23-04-2024)
- Add Test coverage
- Improvement functionality
- Revert back AWS S3 SDK as public
- Update GitHub Action
- Update README.md
## 1.0.0-beta.5 (22-04-2024)
- Make AWS S3 steam seek able
- Make AWS S3 SDK as private
- Upgrade AWS S3 SDK
- Update README.md
## 1.0.0-beta.4 (19-04-2024)
- Upgrade AWS S3 SDK
- Update README.md
- Update CHANGELOG.md
## 1.0.0-beta.3 (19-04-2024)
- Update README.md
## 1.0.0-beta.2 (19-04-2024)
- Add github action publish
- Update README.md
- Update package metadata
## 1.0.0-beta.1 (18-04-2024)
- Initial version