diff --git a/IRaCIS.Core.API/Controllers/ExtraController.cs b/IRaCIS.Core.API/Controllers/ExtraController.cs index ee87fc0a5..75698ad6b 100644 --- a/IRaCIS.Core.API/Controllers/ExtraController.cs +++ b/IRaCIS.Core.API/Controllers/ExtraController.cs @@ -146,7 +146,7 @@ namespace IRaCIS.Api.Controllers var ossOptions = serviceOption.AliyunOSS; - return ResponseOutput.Ok(new ObjectStoreDTO() { ObjectStoreUse = serviceOption.ObjectStoreUse, MinIO = serviceOption.MinIO ,AliyunOSS= serviceOption.AliyunOSS }); + return ResponseOutput.Ok(new ObjectStoreDTO() { ObjectStoreUse = serviceOption.ObjectStoreUse, MinIO = serviceOption.MinIO, AliyunOSS = serviceOption.AliyunOSS, AWS = serviceOption.AWS }); #region 临时token 屏蔽 //IClientProfile profile = DefaultProfile.GetProfile(ossOptions.RegionId, ossOptions.AccessKeyId, ossOptions.AccessKeySecret); @@ -189,11 +189,11 @@ namespace IRaCIS.Api.Controllers } else if (Enum.TryParse(serviceOption.ObjectStoreUse, out var parsedValue) && parsedValue == ObjectStoreUse.MinIO) { - return ResponseOutput.Ok(new ObjectStoreDTO() { ObjectStoreUse = serviceOption.ObjectStoreUse, MinIO = serviceOption.MinIO }); + return ResponseOutput.Ok(new ObjectStoreDTO() { ObjectStoreUse = serviceOption.ObjectStoreUse, MinIO = serviceOption.MinIO, AWS = serviceOption.AWS }); } else { - return ResponseOutput.Ok(new ObjectStoreDTO() { ObjectStoreUse = serviceOption.ObjectStoreUse, MinIO = serviceOption.MinIO }); + return ResponseOutput.Ok(new ObjectStoreDTO() { ObjectStoreUse = serviceOption.ObjectStoreUse, MinIO = serviceOption.MinIO, AWS = serviceOption.AWS }); } } diff --git a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs index 47f2a2399..bc182817f 100644 --- a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs +++ b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs @@ -800,7 +800,7 @@ namespace IRaCIS.Core.API.Controllers templateFileStream.Seek(0, SeekOrigin.Begin); - var ossRelativePath = oSSService.UploadToOSS(fileStream, "InspectionUpload/Check", $"{Guid.NewGuid()}_" + realFileName); + var ossRelativePath = await oSSService.UploadToOSSAsync(fileStream, "InspectionUpload/Check", realFileName); await _repository.AddAsync(new InspectionFile() { FileName = realFileName, RelativePath = ossRelativePath, TrialId = trialId }); @@ -1024,7 +1024,7 @@ namespace IRaCIS.Core.API.Controllers throw new BusinessValidationFailedException(StaticData.International("UploadDownLoad_TemplateUploadData")); } - var ossRelativePath = oSSService.UploadToOSS(fileStream, "InspectionUpload/SiteSurvey", $"{Guid.NewGuid()}_"+ realFileName); + var ossRelativePath = await oSSService.UploadToOSSAsync(fileStream, "InspectionUpload/SiteSurvey", realFileName); await _inspectionFileRepository.AddAsync(new InspectionFile() { FileName = realFileName, RelativePath = ossRelativePath, TrialId = trialId }); diff --git a/IRaCIS.Core.API/appsettings.Test_Study.json b/IRaCIS.Core.API/appsettings.Test_Study.json index 122983466..765da7cfe 100644 --- a/IRaCIS.Core.API/appsettings.Test_Study.json +++ b/IRaCIS.Core.API/appsettings.Test_Study.json @@ -20,12 +20,22 @@ }, "MinIO": { - "endpoint": "http://192.168.3.68", + "endPoint": "192.168.3.68", "port": "8001", "useSSL": false, "accessKey": "IDFkwEpWej0b4DtiuThL", "secretKey": "Lhuu83yMhVwu7c1SnjvGY6lq74jzpYqifK6Qtj4h", - "bucketName": "test" + "bucketName": "test", + "viewEndpoint": "http://192.168.3.68:8001/test/" + }, + + "AWS": { + "endPoint": "s3.us-east-1.amazonaws.com", + "useSSL": false, + "accessKey": "AKIAZQ3DRSOHFPJJ6FEU", + "secretKey": "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", + "bucketName": "ei-irc-test-store", + "viewEndpoint": "https://ei-irc-test-store.s3.amazonaws.com/" } }, diff --git a/IRaCIS.Core.Application/Helper/OSSService.cs b/IRaCIS.Core.Application/Helper/OSSService.cs index 47ad9e321..4cacd52f1 100644 --- a/IRaCIS.Core.Application/Helper/OSSService.cs +++ b/IRaCIS.Core.Application/Helper/OSSService.cs @@ -3,6 +3,8 @@ using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Infrastructure; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; +using Minio.DataModel.Args; +using Minio; using NPOI.HPSF; using SharpCompress.Common; using System; @@ -13,17 +15,24 @@ using System.Linq; using System.Security.AccessControl; using System.Text; using System.Threading.Tasks; +using SkiaSharp; namespace IRaCIS.Core.Application.Helper { - public class MinIOOptions + public class MinIOOptions : AWSOptions { - public string endpoint { get; set; } - public string port { get; set; } + public int port { get; set; } + + } + + public class AWSOptions + { + public string endPoint { get; set; } public bool useSSL { get; set; } public string accessKey { get; set; } public string secretKey { get; set; } public string bucketName { get; set; } + public string viewEndpoint { get; set; } } public class AliyunOSSOptions @@ -48,6 +57,8 @@ namespace IRaCIS.Core.Application.Helper public AliyunOSSOptions AliyunOSS { get; set; } public MinIOOptions MinIO { get; set; } + public AWSOptions AWS { get; set; } + } public class ObjectStoreDTO @@ -58,6 +69,8 @@ namespace IRaCIS.Core.Application.Helper public MinIOOptions MinIO { get; set; } + public AWSOptions AWS { get; set; } + } public class AliyunOSSTempToken @@ -81,76 +94,108 @@ namespace IRaCIS.Core.Application.Helper AWS = 2, } - - - - - public interface IOSSService { - public string UploadToOSS(Stream fileStream, string oosFolderPath, string fileRealName); - public string UploadToOSS(string localFilePath, string oosFolderPath); + public Task UploadToOSSAsync(Stream fileStream, string oosFolderPath, string fileRealName); + public Task UploadToOSSAsync(string localFilePath, string oosFolderPath); - public void DownLoadFromOSS(string ossRelativePath, string localFilePath); + public Task DownLoadFromOSSAsync(string ossRelativePath, string localFilePath); + public ObjectStoreServiceOptions ObjectStoreServiceOptions { get; set; } } + + public class OSSService : IOSSService { - - public AliyunOSSOptions _OSSConfig { get; set; } - - public OssClient _ossClient { get; set; } - + public ObjectStoreServiceOptions ObjectStoreServiceOptions { get; set; } public OSSService(IOptionsMonitor options) { - var ossOptions = options.CurrentValue; - - _OSSConfig = ossOptions.AliyunOSS; - - _ossClient = new OssClient(_OSSConfig.endPoint, _OSSConfig.accessKeyId, _OSSConfig.accessKeySecret); - + ObjectStoreServiceOptions = options.CurrentValue; } /// - /// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder + /// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder /// /// /// /// /// - /// - public string UploadToOSS(Stream fileStream, string oosFolderPath,string fileRealName) + public async Task UploadToOSSAsync(Stream fileStream, string oosFolderPath, string fileRealName) { - var ossRelativePath = oosFolderPath + "/" + fileRealName; - try + var ossRelativePath = $"{oosFolderPath}/{Guid.NewGuid()}_{fileRealName}"; + //var ossRelativePath = oosFolderPath + "/" + fileRealName; + + using (var memoryStream = new MemoryStream()) { - using (var memoryStream = new MemoryStream()) + fileStream.Seek(0, SeekOrigin.Begin); + + fileStream.CopyTo(memoryStream); + + memoryStream.Seek(0, SeekOrigin.Begin); + + + if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS") { - fileStream.Seek(0, SeekOrigin.Begin); + var aliConfig = ObjectStoreServiceOptions.AliyunOSS; + + var _ossClient = new OssClient(aliConfig.endPoint, aliConfig.accessKeyId, aliConfig.accessKeySecret); - fileStream.CopyTo(memoryStream); - memoryStream.Seek(0, SeekOrigin.Begin); // 上传文件 - var result = _ossClient.PutObject(_OSSConfig.bucketName, ossRelativePath, memoryStream); + var result = _ossClient.PutObject(aliConfig.bucketName, ossRelativePath, memoryStream); + } + else if (ObjectStoreServiceOptions.ObjectStoreUse == "MinIO") + { + var minIOConfig = ObjectStoreServiceOptions.MinIO; - return "/" + ossRelativePath; + var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.endPoint}:{minIOConfig.port}") + .WithCredentials(minIOConfig.accessKey, minIOConfig.secretKey).WithSSL(minIOConfig.useSSL) + .Build(); + + var putObjectArgs = new PutObjectArgs() + .WithBucket(minIOConfig.bucketName) + .WithObject(ossRelativePath) + .WithStreamData(memoryStream); + + await minioClient.PutObjectAsync(putObjectArgs); + } + else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS") + { + var minIOConfig = ObjectStoreServiceOptions.AWS; + + + var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.endPoint}") + .WithCredentials(minIOConfig.accessKey, minIOConfig.secretKey).WithSSL(minIOConfig.useSSL) + .Build(); + + var putObjectArgs = new PutObjectArgs() + .WithBucket(minIOConfig.bucketName) + .WithObject(ossRelativePath) + .WithStreamData(memoryStream); + + await minioClient.PutObjectAsync(putObjectArgs); + } + else + { + throw new BusinessValidationFailedException("未定义的存储介质类型"); + } } - catch (Exception ex) - { - throw new BusinessValidationFailedException("oss上传失败" + ex.Message); - } + + return "/" + ossRelativePath; + } + + /// /// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder /// @@ -158,38 +203,124 @@ namespace IRaCIS.Core.Application.Helper /// /// /// - public string UploadToOSS(string localFilePath, string oosFolderPath) + public async Task UploadToOSSAsync(string localFilePath, string oosFolderPath) { var localFileName = Path.GetFileName(localFilePath); - var ossRelativePath = oosFolderPath + "/" + localFileName; + var ossRelativePath = $"{oosFolderPath}/{Guid.NewGuid()}_{localFileName}"; + + //var ossRelativePath = oosFolderPath + "/" + localFileName; - try + if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS") { + var aliConfig = ObjectStoreServiceOptions.AliyunOSS; + + var _ossClient = new OssClient(aliConfig.endPoint, aliConfig.accessKeyId, aliConfig.accessKeySecret); + // 上传文件 - var result = _ossClient.PutObject(_OSSConfig.bucketName, ossRelativePath, localFilePath); + var result = _ossClient.PutObject(aliConfig.bucketName, ossRelativePath, localFilePath); - return ossRelativePath; } - catch (Exception ex) + else if (ObjectStoreServiceOptions.ObjectStoreUse == "MinIO") { - throw new BusinessValidationFailedException("oss上传失败" + ex.Message); + var minIOConfig = ObjectStoreServiceOptions.MinIO; + + var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.endPoint}:{minIOConfig.port}") + .WithCredentials(minIOConfig.accessKey, minIOConfig.secretKey).WithSSL(minIOConfig.useSSL) + .Build(); + + var putObjectArgs = new PutObjectArgs() + .WithBucket(minIOConfig.bucketName) + .WithObject(ossRelativePath) + .WithFileName(localFilePath); + + await minioClient.PutObjectAsync(putObjectArgs); } + else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS") + { + var minIOConfig = ObjectStoreServiceOptions.AWS; + + + var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.endPoint}") + .WithCredentials(minIOConfig.accessKey, minIOConfig.secretKey).WithSSL(minIOConfig.useSSL) + .Build(); + + var putObjectArgs = new PutObjectArgs() + .WithBucket(minIOConfig.bucketName) + .WithObject(ossRelativePath) + .WithFileName(localFilePath); + + await minioClient.PutObjectAsync(putObjectArgs); + } + else + { + throw new BusinessValidationFailedException("未定义的存储介质类型"); + } + return "/" + ossRelativePath; + } - public void DownLoadFromOSS(string ossRelativePath, string localFilePath) + public async Task DownLoadFromOSSAsync(string ossRelativePath, string localFilePath) { + + ossRelativePath = ossRelativePath.TrimStart('/'); try { - var result = _ossClient.GetObject(_OSSConfig.bucketName, ossRelativePath); - // 将下载的文件流保存到本地文件 - using (var fs = File.OpenWrite(localFilePath)) + + if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS") { - result.Content.CopyTo(fs); - fs.Close(); + var aliConfig = ObjectStoreServiceOptions.AliyunOSS; + + var _ossClient = new OssClient(aliConfig.endPoint, aliConfig.accessKeyId, aliConfig.accessKeySecret); + + // 上传文件 + var result = _ossClient.GetObject(aliConfig.bucketName, ossRelativePath); + + // 将下载的文件流保存到本地文件 + using (var fs = File.OpenWrite(localFilePath)) + { + result.Content.CopyTo(fs); + fs.Close(); + } + + } + else if (ObjectStoreServiceOptions.ObjectStoreUse == "MinIO") + { + var minIOConfig = ObjectStoreServiceOptions.MinIO; + + var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.endPoint}:{minIOConfig.port}") + .WithCredentials(minIOConfig.accessKey, minIOConfig.secretKey).WithSSL(minIOConfig.useSSL) + .Build(); + + var getObjectArgs = new GetObjectArgs() + .WithBucket(minIOConfig.bucketName) + .WithObject(ossRelativePath) + .WithFile(localFilePath); + + await minioClient.GetObjectAsync(getObjectArgs); + + } + else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS") + { + var minIOConfig = ObjectStoreServiceOptions.AWS; + + var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.endPoint}") + .WithCredentials(minIOConfig.accessKey, minIOConfig.secretKey).WithSSL(minIOConfig.useSSL) + .Build(); + + var getObjectArgs = new GetObjectArgs() + .WithBucket(minIOConfig.bucketName) + .WithObject(ossRelativePath) + .WithFile(localFilePath); + + await minioClient.GetObjectAsync(getObjectArgs); + } + else + { + throw new BusinessValidationFailedException("未定义的存储介质类型"); } } catch (Exception ex) @@ -199,6 +330,9 @@ namespace IRaCIS.Core.Application.Helper } + + + } diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj index 7c5eedd25..38b149790 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj @@ -89,6 +89,7 @@ + true diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index d521a226a..8e31348c2 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -94,17 +94,16 @@ - + - oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder + oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder - - + oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder