43 lines
1.5 KiB
C#
43 lines
1.5 KiB
C#
namespace LiteCharms.Features.S3.Abstractions;
|
|
|
|
public abstract class S3ServiceBase(IAmazonS3 amazonS3)
|
|
{
|
|
protected readonly IAmazonS3 Client = amazonS3;
|
|
|
|
protected abstract string BucketName { get; }
|
|
protected abstract string CdnBaseUrl { get; }
|
|
|
|
public virtual async Task<Result<string>> UploadFileAsync(string fileName, Stream fileStream, string contentType, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(BucketName))
|
|
return Result.Fail<string>("Bucket name is not configured.");
|
|
|
|
if (string.IsNullOrWhiteSpace(CdnBaseUrl))
|
|
return Result.Fail<string>("CDN base URL is not configured.");
|
|
|
|
var fileKey = $"{Guid.NewGuid():N}{Path.GetExtension(fileName)}";
|
|
|
|
var putRequest = new PutObjectRequest
|
|
{
|
|
BucketName = BucketName,
|
|
Key = fileKey,
|
|
InputStream = fileStream,
|
|
ContentType = contentType,
|
|
UseChunkEncoding = false
|
|
};
|
|
|
|
var response = await Client.PutObjectAsync(putRequest, cancellationToken);
|
|
|
|
return response.HttpStatusCode != System.Net.HttpStatusCode.OK
|
|
? Result.Fail<string>($"Failed to upload {fileName} to S3.")
|
|
: Result.Ok($"{CdnBaseUrl}/{fileKey}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail<string>(new Error($"Error uploading {fileName} to S3: {ex.Message}").CausedBy(ex));
|
|
}
|
|
}
|
|
}
|