mirror of https://github.com/abpframework/abp.git
16 changed files with 565 additions and 8 deletions
@ -0,0 +1,3 @@ |
|||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
|||
<ConfigureAwait ContinueOnCapturedContext="false" /> |
|||
</Weavers> |
|||
@ -0,0 +1,30 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
|||
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> |
|||
<xs:element name="Weavers"> |
|||
<xs:complexType> |
|||
<xs:all> |
|||
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> |
|||
<xs:complexType> |
|||
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:all> |
|||
<xs:attribute name="VerifyAssembly" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> |
|||
<xs:annotation> |
|||
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="GenerateXsd" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:schema> |
|||
@ -0,0 +1,21 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
<Import Project="..\..\..\configureawait.props" /> |
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<AssemblyName>Volo.Abp.BlobStoring.Minio</AssemblyName> |
|||
<PackageId>Volo.Abp.BlobStoring.Minio</PackageId> |
|||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Volo.Abp.BlobStoring\Volo.Abp.BlobStoring.csproj" /> |
|||
<PackageReference Include="Minio" Version="3.1.13" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,10 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
[DependsOn(typeof(AbpBlobStoringModule))] |
|||
public class AbpBlobStoringMinioModule : AbpModule |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
public class DefaultMinioBlobNameCalculator : IMinioBlobNameCalculator, ITransientDependency |
|||
{ |
|||
protected ICurrentTenant CurrentTenant { get; } |
|||
|
|||
public DefaultMinioBlobNameCalculator(ICurrentTenant currentTenant) |
|||
{ |
|||
CurrentTenant = currentTenant; |
|||
} |
|||
|
|||
public virtual string Calculate(BlobProviderArgs args) |
|||
{ |
|||
return CurrentTenant.Id == null |
|||
? $"host/{args.BlobName}" |
|||
: $"tenants/{CurrentTenant.Id.Value.ToString("D")}/{args.BlobName}"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
public interface IMinioBlobNameCalculator |
|||
{ |
|||
string Calculate(BlobProviderArgs args); |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
public static class MinioBlobContainerConfigurationExtensions |
|||
{ |
|||
public static MinioBlobProviderConfiguration GetMinioConfiguration( |
|||
this BlobContainerConfiguration containerConfiguration) |
|||
{ |
|||
return new MinioBlobProviderConfiguration(containerConfiguration); |
|||
} |
|||
|
|||
public static BlobContainerConfiguration UseMinio( |
|||
this BlobContainerConfiguration containerConfiguration, |
|||
Action<MinioBlobProviderConfiguration> minioConfigureAction) |
|||
{ |
|||
containerConfiguration.ProviderType = typeof(MinioBlobProvider); |
|||
|
|||
minioConfigureAction(new MinioBlobProviderConfiguration(containerConfiguration)); |
|||
|
|||
return containerConfiguration; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,159 @@ |
|||
using Minio; |
|||
using Minio.Exceptions; |
|||
using System; |
|||
using System.IO; |
|||
using System.Net; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
public class MinioBlobProvider : BlobProviderBase, ITransientDependency |
|||
{ |
|||
protected IMinioBlobNameCalculator MinioBlobNameCalculator { get; } |
|||
|
|||
public MinioBlobProvider(IMinioBlobNameCalculator minioBlobNameCalculator) |
|||
{ |
|||
MinioBlobNameCalculator = minioBlobNameCalculator; |
|||
} |
|||
|
|||
public override async Task SaveAsync(BlobProviderSaveArgs args) |
|||
{ |
|||
var blobName = MinioBlobNameCalculator.Calculate(args); |
|||
var configuration = args.Configuration.GetMinioConfiguration(); |
|||
|
|||
if (!args.OverrideExisting && await BlobExistsAsync(args, blobName)) |
|||
{ |
|||
throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{GetContainerName(args)}'! Set {nameof(args.OverrideExisting)} if it should be overwritten."); |
|||
} |
|||
|
|||
if (configuration.CreateBucketIfNotExists) |
|||
{ |
|||
await CreateBucketIfNotExists(args); |
|||
} |
|||
|
|||
await GetMinioClient(args).PutObjectAsync(GetContainerName(args), blobName, args.BlobStream, args.BlobStream.Length); |
|||
} |
|||
|
|||
public override async Task<bool> DeleteAsync(BlobProviderDeleteArgs args) |
|||
{ |
|||
var blobName = MinioBlobNameCalculator.Calculate(args); |
|||
|
|||
if (await BlobExistsAsync(args, blobName)) |
|||
{ |
|||
var client = GetMinioClient(args); |
|||
await client.RemoveObjectAsync(GetContainerName(args), blobName); |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public override async Task<bool> ExistsAsync(BlobProviderExistsArgs args) |
|||
{ |
|||
var blobName = MinioBlobNameCalculator.Calculate(args); |
|||
|
|||
return await BlobExistsAsync(args, blobName); |
|||
} |
|||
|
|||
public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args) |
|||
{ |
|||
var blobName = MinioBlobNameCalculator.Calculate(args); |
|||
|
|||
if (!await BlobExistsAsync(args, blobName)) |
|||
{ |
|||
return null; |
|||
} |
|||
try |
|||
{ |
|||
var client = GetMinioClient(args); |
|||
|
|||
var stat = await client.StatObjectAsync(GetContainerName(args), blobName); |
|||
|
|||
MemoryStream returnStream = new MemoryStream(); |
|||
|
|||
await client.GetObjectAsync(GetContainerName(args), blobName, |
|||
(stream) => |
|||
{ |
|||
if (stream != null) |
|||
{ |
|||
stream.CopyTo(returnStream); |
|||
|
|||
// returnStream = new MemoryStream(stream.GetAllBytes());
|
|||
|
|||
} |
|||
}); |
|||
|
|||
return returnStream; |
|||
} |
|||
catch (MinioException ex) |
|||
{ |
|||
|
|||
} |
|||
return null; |
|||
|
|||
} |
|||
|
|||
|
|||
private MinioClient GetMinioClient(BlobProviderArgs args) |
|||
{ |
|||
var configuration = args.Configuration.GetMinioConfiguration(); |
|||
var client = new MinioClient(configuration.EndPoint, configuration.AccessKey, configuration.SecretKey); |
|||
if (configuration.WithSSL) |
|||
{ |
|||
client.WithSSL(); |
|||
} |
|||
|
|||
return client; |
|||
|
|||
} |
|||
|
|||
|
|||
|
|||
protected virtual async Task CreateBucketIfNotExists(BlobProviderArgs args) |
|||
{ |
|||
var client = GetMinioClient(args); |
|||
var containerName = GetContainerName(args); |
|||
if (!await client.BucketExistsAsync(containerName)) |
|||
{ |
|||
await client.MakeBucketAsync(containerName); |
|||
} |
|||
|
|||
} |
|||
|
|||
private async Task<bool> BlobExistsAsync(BlobProviderArgs args, string blobName) |
|||
{ |
|||
// Make sure Blob Container exists.
|
|||
if (await ContainerExistsAsync(args)) |
|||
{ |
|||
try |
|||
{ |
|||
await GetMinioClient(args).StatObjectAsync(GetContainerName(args), blobName); |
|||
return true; |
|||
} |
|||
catch (MinioException ex) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
return false; |
|||
|
|||
} |
|||
private static string GetContainerName(BlobProviderArgs args) |
|||
{ |
|||
var configuration = args.Configuration.GetMinioConfiguration(); |
|||
|
|||
//minio bucket name must be lower
|
|||
return configuration.BucketName.IsNullOrWhiteSpace() |
|||
? args.ContainerName.ToLower() |
|||
: configuration.BucketName.ToLower(); |
|||
} |
|||
|
|||
private async Task<bool> ContainerExistsAsync(BlobProviderArgs args) |
|||
{ |
|||
var client = GetMinioClient(args); |
|||
|
|||
return await client.BucketExistsAsync(GetContainerName(args)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
public class MinioBlobProviderConfiguration |
|||
{ |
|||
public string BucketName |
|||
{ |
|||
get => _containerConfiguration.GetConfiguration<string>(MinioBlobProviderConfigurationNames.BucketName); |
|||
set => _containerConfiguration.SetConfiguration(MinioBlobProviderConfigurationNames.BucketName, Check.NotNullOrWhiteSpace(value, nameof(value))); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// endPoint is an URL, domain name, IPv4 address or IPv6 address.
|
|||
/// </summary>
|
|||
public string EndPoint |
|||
{ |
|||
get => _containerConfiguration.GetConfiguration<string>(MinioBlobProviderConfigurationNames.EndPoint); |
|||
set => _containerConfiguration.SetConfiguration(MinioBlobProviderConfigurationNames.EndPoint, Check.NotNullOrWhiteSpace(value, nameof(value))); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// accessKey is like user-id that uniquely identifies your account.This field is optional and can be omitted for anonymous access.
|
|||
/// </summary>
|
|||
public string AccessKey |
|||
{ |
|||
get => _containerConfiguration.GetConfiguration<string>(MinioBlobProviderConfigurationNames.AccessKey); |
|||
set => _containerConfiguration.SetConfiguration(MinioBlobProviderConfigurationNames.AccessKey, Check.NotNullOrWhiteSpace(value, nameof(value))); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// secretKey is the password to your account.This field is optional and can be omitted for anonymous access.
|
|||
/// </summary>
|
|||
public string SecretKey |
|||
{ |
|||
get => _containerConfiguration.GetConfiguration<string>(MinioBlobProviderConfigurationNames.SecretKey); |
|||
set => _containerConfiguration.SetConfiguration(MinioBlobProviderConfigurationNames.SecretKey, Check.NotNullOrWhiteSpace(value, nameof(value))); |
|||
} |
|||
|
|||
/// <summary>
|
|||
///connect to to MinIO Client object to use https instead of http
|
|||
/// </summary>
|
|||
public bool WithSSL |
|||
{ |
|||
get => _containerConfiguration.GetConfigurationOrDefault(MinioBlobProviderConfigurationNames.WithSSL, false); |
|||
set => _containerConfiguration.SetConfiguration(MinioBlobProviderConfigurationNames.WithSSL, value); |
|||
} |
|||
|
|||
|
|||
|
|||
/// <summary>
|
|||
///Default value: false.
|
|||
/// </summary>
|
|||
public bool CreateBucketIfNotExists |
|||
{ |
|||
get => _containerConfiguration.GetConfigurationOrDefault(MinioBlobProviderConfigurationNames.CreateBucketIfNotExists, false); |
|||
set => _containerConfiguration.SetConfiguration(MinioBlobProviderConfigurationNames.CreateBucketIfNotExists, value); |
|||
} |
|||
|
|||
private readonly BlobContainerConfiguration _containerConfiguration; |
|||
|
|||
public MinioBlobProviderConfiguration(BlobContainerConfiguration containerConfiguration) |
|||
{ |
|||
_containerConfiguration = containerConfiguration; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
public static class MinioBlobProviderConfigurationNames |
|||
{ |
|||
public const string BucketName = "Minio.BucketName"; |
|||
public const string EndPoint = "Minio.EndPoint"; |
|||
public const string AccessKey = "Minio.AccessKey"; |
|||
public const string SecretKey = "Minio.SecretKey"; |
|||
public const string WithSSL = "Minio.WithSSL"; |
|||
public const string CreateBucketIfNotExists = "Minio.CreateBucketIfNotExists"; |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
<Import Project="..\..\..\common.test.props" /> |
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp3.1</TargetFramework> |
|||
<RootNamespace /> |
|||
<UserSecretsId>9f0d2c00-80c1-435b-bfab-2c39c8249091</UserSecretsId> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.BlobStoring.Minio\Volo.Abp.BlobStoring.Minio.csproj" /> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
|||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" /> |
|||
<ProjectReference Include="..\Volo.Abp.BlobStoring.Tests\Volo.Abp.BlobStoring.Tests.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,20 @@ |
|||
using Volo.Abp.Testing; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
public class AbpBlobStoringMinioTestCommonBase : AbpIntegratedTest<AbpBlobStoringMinioTestCommonModule> |
|||
{ |
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
} |
|||
|
|||
public class AbpBlobStoringMinioTestBase : AbpIntegratedTest<AbpBlobStoringMinioTestModule> |
|||
{ |
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Minio; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
|
|||
[DependsOn( |
|||
typeof(AbpBlobStoringMinioModule), |
|||
typeof(AbpBlobStoringTestModule) |
|||
)] |
|||
public class AbpBlobStoringMinioTestCommonModule : AbpModule |
|||
{ |
|||
|
|||
} |
|||
|
|||
[DependsOn( |
|||
typeof(AbpBlobStoringMinioTestCommonModule) |
|||
)] |
|||
public class AbpBlobStoringMinioTestModule : AbpModule |
|||
{ |
|||
private const string UserSecretsId = "9f0d2c00-80c1-435b-bfab-2c39c8249091"; |
|||
|
|||
private string _endPoint; |
|||
private string _accessKey; |
|||
private string _secretKey; |
|||
|
|||
|
|||
private readonly string _randomContainerName = "abp-minio-test-container-" + Guid.NewGuid().ToString("N"); |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.ReplaceConfiguration(ConfigurationHelper.BuildConfiguration(builderAction: builder => |
|||
{ |
|||
builder.AddUserSecrets(UserSecretsId); |
|||
})); |
|||
|
|||
var configuration = context.Services.GetConfiguration(); |
|||
_endPoint = configuration["Minio:EndPoint"]; |
|||
_accessKey = configuration["Minio:AccessKey"]; |
|||
_secretKey = configuration["Minio:SecretKey"]; |
|||
|
|||
Configure<AbpBlobStoringOptions>(options => |
|||
{ |
|||
options.Containers.ConfigureAll((containerName, containerConfiguration) => |
|||
{ |
|||
containerConfiguration.UseMinio(minio => |
|||
{ |
|||
minio.EndPoint = _endPoint; |
|||
minio.AccessKey = _accessKey; |
|||
minio.SecretKey = _secretKey; |
|||
minio.WithSSL = false; |
|||
minio.BucketName = _randomContainerName; |
|||
minio.CreateBucketIfNotExists = true; |
|||
}); |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
public async override void OnApplicationShutdown(ApplicationShutdownContext context) |
|||
{ |
|||
var minioClient = new MinioClient(_endPoint, _accessKey, _secretKey); |
|||
if (await minioClient.BucketExistsAsync(_randomContainerName)) |
|||
{ |
|||
var observables =minioClient.ListObjectsAsync(_randomContainerName,null,true); |
|||
var objectNames = new List<string>(); |
|||
IDisposable subscription = observables.Subscribe( |
|||
async item => await minioClient.RemoveObjectAsync(_randomContainerName, item.Key), |
|||
async () => await minioClient.RemoveBucketAsync(_randomContainerName) |
|||
); |
|||
|
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
|
|||
////Please set the correct connection string in secrets.json and continue the test.
|
|||
|
|||
//public class MinioBlobContainer_Tests : BlobContainer_Tests<AbpBlobStoringMinioTestModule>
|
|||
//{
|
|||
// public MinioBlobContainer_Tests()
|
|||
// {
|
|||
|
|||
// }
|
|||
//}
|
|||
|
|||
} |
|||
@ -0,0 +1,57 @@ |
|||
using System; |
|||
using Shouldly; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Minio |
|||
{ |
|||
public class MinioBlobNameCalculator_Tests : AbpBlobStoringMinioTestCommonBase |
|||
{ |
|||
private readonly IMinioBlobNameCalculator _calculator; |
|||
private readonly ICurrentTenant _currentTenant; |
|||
|
|||
private const string MinioContainerName = "/"; |
|||
private const string MinioSeparator = "/"; |
|||
|
|||
public MinioBlobNameCalculator_Tests() |
|||
{ |
|||
_calculator = GetRequiredService<IMinioBlobNameCalculator>(); |
|||
_currentTenant = GetRequiredService<ICurrentTenant>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Default_Settings() |
|||
{ |
|||
_calculator.Calculate( |
|||
GetArgs("my-container", "my-blob") |
|||
).ShouldBe($"host{MinioSeparator}my-blob"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Default_Settings_With_TenantId() |
|||
{ |
|||
var tenantId = Guid.NewGuid(); |
|||
|
|||
using (_currentTenant.Change(tenantId)) |
|||
{ |
|||
_calculator.Calculate( |
|||
GetArgs("my-container", "my-blob") |
|||
).ShouldBe($"tenants{MinioSeparator}{tenantId:D}{MinioSeparator}my-blob"); |
|||
} |
|||
} |
|||
|
|||
private static BlobProviderArgs GetArgs( |
|||
string containerName, |
|||
string blobName) |
|||
{ |
|||
return new BlobProviderGetArgs( |
|||
containerName, |
|||
new BlobContainerConfiguration().UseMinio(x => |
|||
{ |
|||
x.BucketName = containerName; |
|||
}), |
|||
blobName |
|||
); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue