Files
mtusi2026lab5/src/Messenger.Api/Infrastructure/S3Storage.cs
T

27 lines
1.1 KiB
C#

using Amazon.S3;
using Amazon.S3.Model;
using Amazon.Runtime;
namespace Messenger.Api.Infrastructure;
public sealed class S3Storage : IDisposable
{
public AmazonS3Client Client { get; }
public string Bucket { get; }
public S3Storage(IConfiguration config)
{
Bucket=config["S3_BUCKET"] ?? "messenger";
Client=new AmazonS3Client(new BasicAWSCredentials(
File.ReadAllText(config["S3_ACCESS_KEY_FILE"]!).Trim(),
File.ReadAllText(config["S3_SECRET_KEY_FILE"]!).Trim()),new AmazonS3Config {
ServiceURL=config["S3_ENDPOINT"] ?? "http://s3:9000", ForcePathStyle=true,
AuthenticationRegion="us-east-1", MaxErrorRetry=0, Timeout=TimeSpan.FromSeconds(20)
});
}
public async Task<bool> ReadyAsync()
{
using var timeout=new CancellationTokenSource(TimeSpan.FromSeconds(1));
try { await Client.ListObjectsV2Async(new ListObjectsV2Request {BucketName=Bucket,MaxKeys=1},timeout.Token); return true; }
catch { return false; }
}
public void Dispose() => Client.Dispose();
}