Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS storage #676

Merged
merged 1 commit into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/platform/mix.constant/Enums/MixStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public enum MixStorageProvider
{
CLOUDFLARE,
MIX
MIX,
AWS
}
}
17 changes: 17 additions & 0 deletions src/platform/mix.storage.lib/Engines/Aws/AwsSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Mix.Storage.Lib.Engines.Aws
{
public class AwsSetting
{
public string BucketName { get; set; }

public string CloudFrontUrl { get; set; }

public string BucketUrl { get; set; }

public string AccessKeyId { get; set; }

public string SecretAccessKey { get; set; }

public string Region { get; set; }
}
}
78 changes: 78 additions & 0 deletions src/platform/mix.storage.lib/Engines/Aws/AwsUploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Net;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Mix.Storage.Lib.Engines.Base;

namespace Mix.Storage.Lib.Engines.Aws
{
public class AwsUploader : UploaderBase
{
private readonly AwsSetting _setting;

private readonly IAmazonS3 _client;

public AwsUploader(IHttpContextAccessor httpContextAccessor, IConfiguration configuration, UnitOfWorkInfo<MixCmsContext> cmsUow) : base(httpContextAccessor, configuration, cmsUow)
{
_setting = new AwsSetting();
Configuration.Bind("StorageSetting:AwsSetting", _setting);
_client = new AmazonS3Client(
_setting.AccessKeyId,
_setting.SecretAccessKey,
new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.GetBySystemName(_setting.Region)
});
}

public override async Task<string?> Upload(IFormFile file, string? themeName, string? createdBy, CancellationToken cancellationToken = default)
{
using (var inputStream = new MemoryStream())
{
await file.CopyToAsync(inputStream, cancellationToken);

var request = new PutObjectRequest
{
BucketName = _setting.BucketName,
Key = string.IsNullOrEmpty(themeName) ? file.FileName : Path.Combine(themeName, file.FileName),
InputStream = inputStream,
CannedACL = S3CannedACL.PublicRead
};

return await GetStorageUrl(request, cancellationToken);
}
}

public override async Task<string?> UploadStream(FileModel file, string? createdBy, CancellationToken cancellationToken = default)
{
var base64String = file.FileBase64.Split(',')[1];
var bytes = Convert.FromBase64String(base64String);
using (var inputStream = new MemoryStream(bytes))
{
var request = new PutObjectRequest
{
BucketName = _setting.BucketName,
Key = string.IsNullOrEmpty(file.FileFolder) ? $"{file.Filename}{file.Extension}" : file.FullPath,
InputStream = inputStream,
CannedACL = S3CannedACL.PublicRead
};

return await GetStorageUrl(request, cancellationToken);
}
}

private async Task<string> GetStorageUrl(PutObjectRequest request, CancellationToken cancellationToken = default)
{
var response = await _client.PutObjectAsync(request, cancellationToken);
if (response.HttpStatusCode != HttpStatusCode.OK)
{
return string.Empty;
}

var storageUrl = string.IsNullOrEmpty(_setting.CloudFrontUrl) ? _setting.BucketUrl : _setting.CloudFrontUrl;
return $"{storageUrl}/{request.Key}";
}
}
}
8 changes: 4 additions & 4 deletions src/platform/mix.storage.lib/Engines/Base/UploaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ protected MixTenantSystemModel CurrentTenant
}
}

protected UploaderBase(IHttpContextAccessor httpContextAccessor, IConfiguration configuration, UnitOfWorkInfo<MixCmsContext> cmsUOW)
protected UploaderBase(IHttpContextAccessor httpContextAccessor, IConfiguration configuration, UnitOfWorkInfo<MixCmsContext> cmsUow)
{
CmsUow = cmsUOW;
CmsUow = cmsUow;
Configuration = configuration;
Session = httpContextAccessor?.HttpContext?.Session;
}
Expand Down Expand Up @@ -60,7 +60,7 @@ public async Task CreateMedia(string fullname, int tenantId, string? createdBy,
var result = await Upload(file, folder, createdBy, cancellationToken);
if (!string.IsNullOrEmpty(result))
{
await CreateMedia(result, _currentTenant.Id, createdBy, cancellationToken);
await CreateMedia(result, CurrentTenant.Id, createdBy, cancellationToken);
}

return result;
Expand All @@ -71,7 +71,7 @@ public async Task CreateMedia(string fullname, int tenantId, string? createdBy,
var result = await UploadStream(file, createdBy, cancellationToken);
if (!string.IsNullOrEmpty(result))
{
await CreateMedia(result, _currentTenant.Id, createdBy, cancellationToken);
await CreateMedia(result, CurrentTenant.Id, createdBy, cancellationToken);
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/mix.storage.lib/Engines/Mix/MixUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private string GetUploadFolder(string filename, string? fileFolder, string? crea
folder = $"{folder}/{createdBy}";
}

return $"{folder}/{DateTime.Now.ToString("yyyy-MM")}";
return $"{folder}/{DateTime.Now:yyyy-MM}";
}
}
}
18 changes: 10 additions & 8 deletions src/platform/mix.storage.lib/Services/MixStorageService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Http;
using Amazon.S3;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Mix.Storage.Lib.Engines.Aws;
using Mix.Storage.Lib.Engines.Base;
using Mix.Storage.Lib.Engines.CloudFlare;
using Mix.Storage.Lib.Engines.Mix;
Expand All @@ -22,14 +24,14 @@ private IMixUploader CreateUploader(IHttpContextAccessor httpContext, UnitOfWork
{
var providerSetting = _configuration["StorageSetting:Provider"];
var provider = Enum.Parse<MixStorageProvider>(providerSetting);
switch (provider)
return provider switch
{
case MixStorageProvider.CLOUDFLARE:
return new CloudFlareUploader(httpContext, _configuration, cmsUow, _httpService);
case MixStorageProvider.MIX:
default:
return new MixUploader(httpContext, _configuration, cmsUow);
}
MixStorageProvider.CLOUDFLARE => new CloudFlareUploader(httpContext, _configuration, cmsUow,
_httpService),
MixStorageProvider.AWS => new AwsUploader(httpContext, _configuration, cmsUow),
MixStorageProvider.MIX => new MixUploader(httpContext, _configuration, cmsUow),
_ => new MixUploader(httpContext, _configuration, cmsUow)
};
}
#region Methods

Expand Down
4 changes: 4 additions & 0 deletions src/platform/mix.storage.lib/mix.storage.lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<RootNamespace>Mix.Storage.Lib</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.104.15" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\mix.library\mix.library.csproj" />
</ItemGroup>
Expand Down