Skip to content

Commit

Permalink
minor refactor minio/aws s3
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Dec 6, 2024
1 parent 41033ca commit 251d5b5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 47 deletions.
38 changes: 32 additions & 6 deletions src/BE/Services/FileServices/Implementations/AwsS3/AwsS3Config.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Text.Json.Serialization;
using Amazon;
using Amazon.S3;
using System.Text.Json.Serialization;

namespace Chats.BE.Services.FileServices.Implementations.AwsS3;

public record AwsS3Config : IHaveBucket
public record AwsS3Config
{
[JsonPropertyName("region")]
public string? Region { get; init; }
Expand All @@ -13,11 +15,35 @@ public record AwsS3Config : IHaveBucket
[JsonPropertyName("secretAccessKey")]
public string? SecretAccessKey { get; init; }

[JsonPropertyName("sessionToken")]
public string? SessionToken { get; init; }

[JsonPropertyName("bucket")]
public required string Bucket { get; init; }
}

public interface IHaveBucket
{
string Bucket { get; }
public AmazonS3Client CreateS3()
{
if (SecretAccessKey != null)
{
if (SessionToken != null)
{
// for developers temporary credentials
return new(AccessKeyId, SecretAccessKey, SessionToken, new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.GetBySystemName(Region)
});
}
else
{
// for server permanent credentials
return new(AccessKeyId, SecretAccessKey, new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.GetBySystemName(Region)
});
}
}

// if it's null, then auto load from default environment profile
return new();
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
using Amazon;
using Amazon.S3;
using Amazon.S3;
using Amazon.S3.Model;

namespace Chats.BE.Services.FileServices.Implementations.AwsS3;

public class AwsS3FileService : IFileService
{
private readonly IHaveBucket _config;
private readonly string _bucketName;
private readonly AmazonS3Client _s3;

public AwsS3FileService(AwsS3Config config)
{
_config = config;
if (config.AccessKeyId == null)
{
// auto load from default environment profile
_s3 = new();
}
else
{
_s3 = new(config.AccessKeyId, config.SecretAccessKey, new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.GetBySystemName(config.Region)
});
}
_bucketName = config.Bucket;
_s3 = config.CreateS3();
}

public AwsS3FileService(IHaveBucket config, AmazonS3Client s3)
public AwsS3FileService(string bucketName, AmazonS3Client s3)
{
_config = config;
_bucketName = bucketName;
_s3 = s3;
}

public Uri CreateDownloadUrl(CreateDownloadUrlRequest req)
{
string url = _s3.GetPreSignedURL(new GetPreSignedUrlRequest
{
BucketName = _config.Bucket,
BucketName = _bucketName,
Key = req.StorageKey,
Expires = req.ValidEnd.UtcDateTime,
Verb = HttpVerb.GET
Expand All @@ -48,7 +36,7 @@ public async Task<Stream> Download(string storageKey, CancellationToken cancella
{
GetObjectResponse resp = await _s3.GetObjectAsync(new GetObjectRequest
{
BucketName = _config.Bucket,
BucketName = _bucketName,
Key = storageKey
}, cancellationToken);
return resp.ResponseStream;
Expand All @@ -59,7 +47,7 @@ public async Task<string> Upload(FileUploadRequest request, CancellationToken ca
SuggestedStorageInfo ssi = SuggestedStorageInfo.FromFileName(request.FileName);
_ = await _s3.PutObjectAsync(new PutObjectRequest()
{
BucketName = _config.Bucket,
BucketName = _bucketName,
Key = ssi.StorageKey,
InputStream = request.Stream,
ContentType = request.ContentType
Expand Down
21 changes: 19 additions & 2 deletions src/BE/Services/FileServices/Implementations/Minio/MinioConfig.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Chats.BE.Services.FileServices.Implementations.AwsS3;
using Amazon;
using Amazon.S3;
using Chats.BE.Services.FileServices.Implementations.AwsS3;
using System.Text.Json.Serialization;

namespace Chats.BE.Services.FileServices.Implementations.Minio;

public record MinioConfig : IHaveBucket
public record MinioConfig
{
[JsonPropertyName("endpoint")]
public required string Endpoint { get; init; }
Expand All @@ -19,4 +21,19 @@ public record MinioConfig : IHaveBucket

[JsonPropertyName("region")]
public string? Region { get; init; } // Nullable for the default null value.

public AmazonS3Client CreateS3()
{
AmazonS3Config s3Config = new()
{
ForcePathStyle = true,
ServiceURL = Endpoint,
};
if (Region != null)
{
s3Config.RegionEndpoint = RegionEndpoint.GetBySystemName(Region);
}
AmazonS3Client s3 = new(AccessKey, SecretKey, s3Config);
return s3;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
using Amazon;
using Amazon.S3;
using Chats.BE.Services.FileServices.Implementations.AwsS3;
using Chats.BE.Services.FileServices.Implementations.AwsS3;

namespace Chats.BE.Services.FileServices.Implementations.Minio;

public class MinioFileService(MinioConfig config) : AwsS3FileService(config, CreateS3(config))
public class MinioFileService(MinioConfig config) : AwsS3FileService(config.Bucket, config.CreateS3())
{
private static AmazonS3Client CreateS3(MinioConfig config)
{
AmazonS3Config s3Config = new()
{
ForcePathStyle = true,
ServiceURL = config.Endpoint,
};
if (config.Region != null)
{
s3Config.RegionEndpoint = RegionEndpoint.GetBySystemName(config.Region);
}
AmazonS3Client s3 = new(config.AccessKey, config.SecretKey, s3Config);
return s3;
}
}

0 comments on commit 251d5b5

Please sign in to comment.