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.
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
Creating a S3FileProvider
instance is very simple:
var s3FileProvider = new S3FileProvider(amazonS3, bucketName);
Or using GetS3FileProvider
extension method
var s3FileProvider = app.Services.GetS3FileProvider(bucketName);
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>();
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 |
-
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);
dotnet add package Microsoft.Owin.Host.SystemWeb
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
}
}
<system.webServer>
<handlers>
<add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
</handlers>
</system.webServer>
dotnet build
dotnet test