초기커밋
This commit is contained in:
63
UGQApiServer/Storage/S3StorageService.cs
Normal file
63
UGQApiServer/Storage/S3StorageService.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Amazon.S3;
|
||||
using UGQDataAccess.Settings;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Amazon.S3.Transfer;
|
||||
using Amazon.S3.Model;
|
||||
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
||||
|
||||
namespace UGQApiServer.Storage;
|
||||
|
||||
public class S3StorageService : IStorageService
|
||||
{
|
||||
readonly AmazonS3Client _s3Client;
|
||||
readonly S3Settings _settings;
|
||||
|
||||
public S3StorageService(IOptions<S3Settings> settings)
|
||||
{
|
||||
_settings = settings.Value;
|
||||
_s3Client = new AmazonS3Client(_settings.AccessKey, _settings.SecretAccessKey, Amazon.RegionEndpoint.USEast1);
|
||||
}
|
||||
|
||||
public string fileUrl(string filename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filename) == true)
|
||||
return string.Empty;
|
||||
|
||||
return $"{_settings.DownloadUrl}/{_settings.RootFolder}/{filename}";
|
||||
}
|
||||
|
||||
public async Task<bool> uploadFile(string filename, IFormFile file)
|
||||
{
|
||||
using (var newMemoryStream = new MemoryStream())
|
||||
{
|
||||
file.CopyTo(newMemoryStream);
|
||||
|
||||
var uploadRequest = new TransferUtilityUploadRequest
|
||||
{
|
||||
InputStream = newMemoryStream,
|
||||
Key = $"{_settings.RootFolder}/{filename}",
|
||||
BucketName = _settings.BucketName,
|
||||
};
|
||||
|
||||
var fileTransferUtility = new TransferUtility(_s3Client);
|
||||
await fileTransferUtility.UploadAsync(uploadRequest);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> deleteFile(string filename)
|
||||
{
|
||||
var deleteObjectRequest = new DeleteObjectRequest
|
||||
{
|
||||
BucketName = _settings.BucketName,
|
||||
Key = $"{_settings.RootFolder}/{filename}",
|
||||
};
|
||||
|
||||
await _s3Client.DeleteObjectAsync(deleteObjectRequest);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user