Browse Source

Improve

pull/4826/head
liangshiwei 6 years ago
parent
commit
ae78f0dcda
  1. 2
      framework/src/Volo.Abp.BlobStoring.Aws/Volo.Abp.BlobStoring.Aws.csproj
  2. 8
      framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsModule.cs
  3. 13
      framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfiguration.cs
  4. 1
      framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfigurationNames.cs
  5. 2
      framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsTemporaryCredentialsCacheItem.cs
  6. 42
      framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/DefaultAmazonS3ClientFactory.cs
  7. 1
      framework/test/Volo.Abp.BlobStoring.Aws.Tests/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsTestModule.cs

2
framework/src/Volo.Abp.BlobStoring.Aws/Volo.Abp.BlobStoring.Aws.csproj

@ -19,6 +19,8 @@
<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.3.111.27" />
<PackageReference Include="AWSSDK.SecurityToken" Version="3.3.105.30" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="3.1.5" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Extensions" Version="3.1.5" />
</ItemGroup>
</Project>

8
framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsModule.cs

@ -1,4 +1,5 @@
using Volo.Abp.Caching;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Caching;
using Volo.Abp.Modularity;
namespace Volo.Abp.BlobStoring.Aws
@ -7,6 +8,9 @@ namespace Volo.Abp.BlobStoring.Aws
typeof(AbpCachingModule))]
public class AbpBlobStoringAwsModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddDataProtection();
}
}
}

13
framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfiguration.cs

@ -1,7 +1,4 @@
using Amazon;
using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using Amazon.S3;
using System;
namespace Volo.Abp.BlobStoring.Aws
{
@ -97,11 +94,19 @@ namespace Volo.Abp.BlobStoring.Aws
set => _containerConfiguration.SetConfiguration(AwsBlobProviderConfigurationNames.CreateContainerIfNotExists, value);
}
private readonly string _temporaryCredentialsCacheKey;
public string TemporaryCredentialsCacheKey
{
get => _containerConfiguration.GetConfigurationOrDefault(AwsBlobProviderConfigurationNames.TemporaryCredentialsCacheKey, _temporaryCredentialsCacheKey);
set => _containerConfiguration.SetConfiguration(AwsBlobProviderConfigurationNames.TemporaryCredentialsCacheKey, value);
}
private readonly BlobContainerConfiguration _containerConfiguration;
public AwsBlobProviderConfiguration(BlobContainerConfiguration containerConfiguration)
{
_containerConfiguration = containerConfiguration;
_temporaryCredentialsCacheKey = Guid.NewGuid().ToString("N");
}
}
}

1
framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfigurationNames.cs

@ -10,6 +10,7 @@
public const string ProfileName = "Aws.ProfileName";
public const string ProfilesLocation = "Aws.ProfilesLocation";
public const string DurationSeconds = "Aws.DurationSeconds";
public const string TemporaryCredentialsCacheKey = "Aws.TemporaryCredentialsCacheKey";
public const string Name = "Aws.Name";
public const string Policy = "Aws.Policy";
public const string Region = "Aws.Region";

2
framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsTemporaryCredentialsCacheItem.cs

@ -5,8 +5,6 @@ namespace Volo.Abp.BlobStoring.Aws
[Serializable]
public class AwsTemporaryCredentialsCacheItem
{
public const string Key = "AwsBlobTemporaryCredentialsCache";
public string AccessKeyId { get; set; }
public string SecretAccessKey { get; set; }

42
framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/DefaultAmazonS3ClientFactory.cs

@ -6,6 +6,7 @@ using Amazon.Runtime.CredentialManagement;
using Amazon.S3;
using Amazon.SecurityToken;
using Amazon.SecurityToken.Model;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
@ -16,9 +17,13 @@ namespace Volo.Abp.BlobStoring.Aws
{
protected IDistributedCache<AwsTemporaryCredentialsCacheItem> Cache { get; }
public DefaultAmazonS3ClientFactory(IDistributedCache<AwsTemporaryCredentialsCacheItem> cache)
protected IDataProtector DataProtector { get; }
public DefaultAmazonS3ClientFactory(IDistributedCache<AwsTemporaryCredentialsCacheItem> cache,
IDataProtectionProvider dataProtectionProvider)
{
Cache = cache;
DataProtector = dataProtectionProvider.CreateProtector(nameof(AwsTemporaryCredentialsCacheItem));
}
public virtual async Task<AmazonS3Client> GetAmazonS3Client(
@ -72,7 +77,7 @@ namespace Volo.Abp.BlobStoring.Aws
protected virtual async Task<SessionAWSCredentials> GetTemporaryCredentialsAsync(
AwsBlobProviderConfiguration configuration)
{
var temporaryCredentialsCache = await Cache.GetAsync(AwsTemporaryCredentialsCacheItem.Key);
var temporaryCredentialsCache = await Cache.GetAsync(configuration.TemporaryCredentialsCacheKey);
if (temporaryCredentialsCache == null)
{
@ -104,14 +109,14 @@ namespace Volo.Abp.BlobStoring.Aws
var credentials = sessionTokenResponse.Credentials;
temporaryCredentialsCache =
await SetTemporaryCredentialsCache(credentials, configuration.DurationSeconds);
await SetTemporaryCredentialsCache(configuration, credentials);
}
}
var sessionCredentials = new SessionAWSCredentials(
temporaryCredentialsCache.AccessKeyId,
temporaryCredentialsCache.SecretAccessKey,
temporaryCredentialsCache.SessionToken);
DataProtector.Unprotect(temporaryCredentialsCache.AccessKeyId),
DataProtector.Unprotect(temporaryCredentialsCache.SecretAccessKey),
DataProtector.Unprotect(temporaryCredentialsCache.SessionToken));
return sessionCredentials;
}
@ -121,7 +126,7 @@ namespace Volo.Abp.BlobStoring.Aws
Check.NotNullOrWhiteSpace(configuration.Name, nameof(configuration.Name));
Check.NotNullOrWhiteSpace(configuration.Policy, nameof(configuration.Policy));
var temporaryCredentialsCache = await Cache.GetAsync(AwsTemporaryCredentialsCacheItem.Key);
var temporaryCredentialsCache = await Cache.GetAsync(configuration.TemporaryCredentialsCacheKey);
if (temporaryCredentialsCache == null)
{
@ -155,29 +160,30 @@ namespace Volo.Abp.BlobStoring.Aws
var credentials = federationTokenResponse.Credentials;
temporaryCredentialsCache =
await SetTemporaryCredentialsCache(credentials, configuration.DurationSeconds);
await SetTemporaryCredentialsCache(configuration, credentials);
}
}
var sessionCredentials = new SessionAWSCredentials(
temporaryCredentialsCache.AccessKeyId,
temporaryCredentialsCache.SecretAccessKey,
temporaryCredentialsCache.SessionToken);
DataProtector.Unprotect(temporaryCredentialsCache.AccessKeyId),
DataProtector.Unprotect(temporaryCredentialsCache.SecretAccessKey),
DataProtector.Unprotect(temporaryCredentialsCache.SessionToken));
return sessionCredentials;
}
private async Task<AwsTemporaryCredentialsCacheItem> SetTemporaryCredentialsCache(
Credentials credentials,
int durationSeconds)
AwsBlobProviderConfiguration configuration,
Credentials credentials)
{
var temporaryCredentialsCache = new AwsTemporaryCredentialsCacheItem(credentials.AccessKeyId,
credentials.SecretAccessKey,
credentials.SessionToken);
var temporaryCredentialsCache = new AwsTemporaryCredentialsCacheItem(
DataProtector.Protect(credentials.AccessKeyId),
DataProtector.Protect(credentials.SecretAccessKey),
DataProtector.Protect(credentials.SessionToken));
await Cache.SetAsync(AwsTemporaryCredentialsCacheItem.Key, temporaryCredentialsCache,
await Cache.SetAsync(configuration.TemporaryCredentialsCacheKey, temporaryCredentialsCache,
new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(durationSeconds - 10)
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(configuration.DurationSeconds - 10)
});
return temporaryCredentialsCache;

1
framework/test/Volo.Abp.BlobStoring.Aws.Tests/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsTestModule.cs

@ -54,6 +54,7 @@ namespace Volo.Abp.BlobStoring.Aws
aws.SecretAccessKey = secretAccessKey;
aws.Region = region;
aws.CreateContainerIfNotExists = true;
aws.ContainerName = _randomContainerName;
_configuration = aws;
});

Loading…
Cancel
Save