29 lines
1.0 KiB
C#
29 lines
1.0 KiB
C#
namespace LiteCharms.Features.S3;
|
|
|
|
public class S3Service(IAmazonS3 amazonS3)
|
|
{
|
|
public async Task<Result<string>> UploadFileAsync(string bucketName, string fileName, Stream fileStream, string contentType, string cdnBaseUrl, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
var putRequest = new PutObjectRequest
|
|
{
|
|
BucketName = bucketName,
|
|
Key = fileName,
|
|
InputStream = fileStream,
|
|
ContentType = contentType
|
|
};
|
|
|
|
var response = await amazonS3.PutObjectAsync(putRequest, cancellationToken);
|
|
|
|
return response.HttpStatusCode != System.Net.HttpStatusCode.OK
|
|
? Result.Fail<string>($"Failed to upload {fileName} to S3.")
|
|
: Result.Ok(string.Format(cdnBaseUrl, bucketName, fileName));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail<string>(new Error($"Error uploading {fileName} to S3: {ex.Message}").CausedBy(ex));
|
|
}
|
|
}
|
|
}
|