Browse Source

Added configuration to override s3 assets store service url from config (#464)

* Added configuration to override s3 assets store service url from config

* Added config value for amazon s3 service url
pull/466/head
Ruben Vardanyan 7 years ago
committed by Sebastian Stehle
parent
commit
af42d4d3cd
  1. 22
      backend/src/Squidex.Infrastructure.Amazon/Assets/AmazonS3AssetStore.cs
  2. 5
      backend/src/Squidex/Config/Domain/AssetServices.cs
  3. 6
      backend/src/Squidex/appsettings.json
  4. 2
      backend/tests/Squidex.Infrastructure.Tests/Assets/AmazonS3AssetStoreFixture.cs
  5. 2
      backend/tests/Squidex.Infrastructure.Tests/Assets/AmazonS3AssetStoreTests.cs

22
backend/src/Squidex.Infrastructure.Amazon/Assets/AmazonS3AssetStore.cs

@ -19,6 +19,7 @@ namespace Squidex.Infrastructure.Assets
public sealed class AmazonS3AssetStore : DisposableObjectBase, IAssetStore, IInitializable public sealed class AmazonS3AssetStore : DisposableObjectBase, IAssetStore, IInitializable
{ {
private const int BufferSize = 81920; private const int BufferSize = 81920;
private readonly string? serviceUrl;
private readonly string accessKey; private readonly string accessKey;
private readonly string secretKey; private readonly string secretKey;
private readonly string bucketName; private readonly string bucketName;
@ -27,12 +28,13 @@ namespace Squidex.Infrastructure.Assets
private TransferUtility transferUtility; private TransferUtility transferUtility;
private IAmazonS3 s3Client; private IAmazonS3 s3Client;
public AmazonS3AssetStore(string regionName, string bucketName, string? bucketFolder, string accessKey, string secretKey) public AmazonS3AssetStore(string? serviceUrl, string? regionName, string bucketName, string? bucketFolder, string accessKey, string secretKey)
{ {
Guard.NotNullOrEmpty(bucketName); Guard.NotNullOrEmpty(bucketName);
Guard.NotNullOrEmpty(accessKey); Guard.NotNullOrEmpty(accessKey);
Guard.NotNullOrEmpty(secretKey); Guard.NotNullOrEmpty(secretKey);
this.serviceUrl = serviceUrl;
this.bucketName = bucketName; this.bucketName = bucketName;
this.bucketFolder = bucketFolder; this.bucketFolder = bucketFolder;
this.accessKey = accessKey; this.accessKey = accessKey;
@ -55,10 +57,20 @@ namespace Squidex.Infrastructure.Assets
{ {
try try
{ {
s3Client = new AmazonS3Client( if (!string.IsNullOrWhiteSpace(serviceUrl))
accessKey, {
secretKey, s3Client = new AmazonS3Client(
bucketRegion); accessKey,
secretKey,
new AmazonS3Config { ServiceURL = serviceUrl });
}
else
{
s3Client = new AmazonS3Client(
accessKey,
secretKey,
bucketRegion);
}
transferUtility = new TransferUtility(s3Client); transferUtility = new TransferUtility(s3Client);

5
backend/src/Squidex/Config/Domain/AssetServices.cs

@ -88,7 +88,8 @@ namespace Squidex.Config.Domain
}, },
["AmazonS3"] = () => ["AmazonS3"] = () =>
{ {
var regionName = config.GetRequiredValue("assetStore:amazonS3:regionName"); var serviceUrl = config.GetOptionalValue<string>("assetStore:amazonS3:serviceUrl");
var regionName = config.GetOptionalValue<string>("assetStore:amazonS3:regionName");
var bucketName = config.GetRequiredValue("assetStore:amazonS3:bucket"); var bucketName = config.GetRequiredValue("assetStore:amazonS3:bucket");
var bucketFolder = config.GetRequiredValue("assetStore:amazonS3:bucketFolder"); var bucketFolder = config.GetRequiredValue("assetStore:amazonS3:bucketFolder");
@ -96,7 +97,7 @@ namespace Squidex.Config.Domain
var accessKey = config.GetRequiredValue("assetStore:amazonS3:accessKey"); var accessKey = config.GetRequiredValue("assetStore:amazonS3:accessKey");
var secretKey = config.GetRequiredValue("assetStore:amazonS3:secretKey"); var secretKey = config.GetRequiredValue("assetStore:amazonS3:secretKey");
services.AddSingletonAs(c => new AmazonS3AssetStore(regionName, bucketName, bucketFolder, accessKey, secretKey)) services.AddSingletonAs(c => new AmazonS3AssetStore(serviceUrl, regionName, bucketName, bucketFolder, accessKey, secretKey))
.As<IAssetStore>(); .As<IAssetStore>();
}, },
["MongoDb"] = () => ["MongoDb"] = () =>

6
backend/src/Squidex/appsettings.json

@ -284,6 +284,12 @@
"connectionString": "UseDevelopmentStorage=true" "connectionString": "UseDevelopmentStorage=true"
}, },
"AmazonS3": { "AmazonS3": {
/*
* The url of the S3 API service. Leave it empty if using the one provided by Amazon
*/
"serviceUrl": "",
/* /*
* The name of your bucket. * The name of your bucket.
*/ */

2
backend/tests/Squidex.Infrastructure.Tests/Assets/AmazonS3AssetStoreFixture.cs

@ -13,7 +13,7 @@ namespace Squidex.Infrastructure.Assets
public AmazonS3AssetStoreFixture() public AmazonS3AssetStoreFixture()
{ {
AssetStore = new AmazonS3AssetStore("eu-central-1", "squidex-test", "squidex-assets", "secret", "secret"); AssetStore = new AmazonS3AssetStore(null, "eu-central-1", "squidex-test", "squidex-assets", "secret", "secret");
AssetStore.InitializeAsync().Wait(); AssetStore.InitializeAsync().Wait();
} }
} }

2
backend/tests/Squidex.Infrastructure.Tests/Assets/AmazonS3AssetStoreTests.cs

@ -28,7 +28,7 @@ namespace Squidex.Infrastructure.Assets
[Fact] [Fact]
public async Task Should_throw_exception_for_invalid_config() public async Task Should_throw_exception_for_invalid_config()
{ {
var sut = new AmazonS3AssetStore("invalid", "invalid", null, "invalid", "invalid"); var sut = new AmazonS3AssetStore(null, "invalid", "invalid", null, "invalid", "invalid");
await Assert.ThrowsAsync<ConfigurationException>(() => sut.InitializeAsync()); await Assert.ThrowsAsync<ConfigurationException>(() => sut.InitializeAsync());
} }

Loading…
Cancel
Save