초기커밋

This commit is contained in:
2025-05-01 07:20:41 +09:00
commit 98bb2e3c5c
2747 changed files with 646947 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
namespace UGQApiServer.Storage;
public interface IStorageService
{
string fileUrl(string filename);
Task<bool> uploadFile(string filename, IFormFile file);
Task<bool> deleteFile(string filename);
}

View File

@@ -0,0 +1,40 @@
namespace UGQApiServer.Storage;
public class LocalStorageService : IStorageService
{
public LocalStorageService()
{
}
public string fileUrl(string filename)
{
return filename;
}
public async Task<bool> uploadFile(string filename, IFormFile file)
{
await Task.CompletedTask;
string path = Path.Combine(Directory.GetCurrentDirectory(), "files");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (var stream = new FileStream(Path.Combine(path, filename), FileMode.Create))
{
file.CopyTo(stream);
}
return true;
}
public async Task<bool> deleteFile(string filename)
{
await Task.CompletedTask;
System.IO.File.Delete(filename);
return true;
}
}

View 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;
}
}