Skip to content

Commit

Permalink
feat(S3): Add Endpoint config for usage with S3-compatibel storage pr…
Browse files Browse the repository at this point in the history
…oviders

* Add and Endpoint option to be able to set ServiceURL

* Validate that Endoint and region are mutually exclusive, but at least one must be set

#132
  • Loading branch information
ferarias authored Apr 19, 2024
1 parent 9543efd commit 3fc6f19
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
7 changes: 3 additions & 4 deletions src/BaGetter.Aws/AwsApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ public static BaGetterApplication AddAwsS3Storage(this BaGetterApplication app)
{
var options = provider.GetRequiredService<IOptions<S3StorageOptions>>().Value;
var config = new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.GetBySystemName(options.Region)
};
var config = options.Endpoint != null
? new AmazonS3Config { ServiceURL = options.Endpoint.AbsoluteUri }
: new AmazonS3Config { RegionEndpoint = RegionEndpoint.GetBySystemName(options.Region) };
if (options.UseInstanceProfile)
{
Expand Down
26 changes: 24 additions & 2 deletions src/BaGetter.Aws/S3StorageOptions.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using BaGetter.Core;

namespace BaGetter.Aws;

public class S3StorageOptions
public class S3StorageOptions : IValidatableObject
{
[RequiredIf(nameof(SecretKey), null, IsInverted = true)]
public string AccessKey { get; set; }

[RequiredIf(nameof(AccessKey), null, IsInverted = true)]
public string SecretKey { get; set; }

[Required]
[RequiredIf(nameof(Endpoint), null)]
public string Region { get; set; }

[RequiredIf(nameof(Region), null)]
public Uri Endpoint { get; set; }

[Required]
public string Bucket { get; set; }

Expand All @@ -22,4 +27,21 @@ public class S3StorageOptions
public bool UseInstanceProfile { get; set; }

public string AssumeRoleArn { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Endpoint != null && !string.IsNullOrEmpty(Region))
{
yield return new ValidationResult(
$"Only one of S3 {nameof(Region)} or {nameof(Endpoint)} configuration can be set, but not both.",
new[] { nameof(Region), nameof(Endpoint) });
}

if(Endpoint != null && !Endpoint.IsAbsoluteUri)
{
yield return new ValidationResult(
$"The S3 {nameof(Endpoint)} must be an absolute URI.",
new[] { nameof(Endpoint) });
}
}
}

0 comments on commit 3fc6f19

Please sign in to comment.