From c35da39ba69646bba77a320810ba4b913bfc763a Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Wed, 10 Jan 2024 14:57:41 +0800 Subject: [PATCH] =?UTF-8?q?OSS=20=E5=AF=B9=E6=8E=A5=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/UploadDownLoadController.cs | 4 +- IRaCIS.Core.Application/Helper/OSSService.cs | 155 ++++++++++++------ 2 files changed, 109 insertions(+), 50 deletions(-) diff --git a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs index 31dadf49c..9f5f903e2 100644 --- a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs +++ b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs @@ -566,7 +566,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", $"{Guid.NewGuid()}_" + realFileName); await _repository.AddAsync(new InspectionFile() { FileName = realFileName, RelativePath = ossRelativePath, TrialId = trialId }); @@ -997,7 +997,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", $"{Guid.NewGuid()}_"+ realFileName); await _inspectionFileRepository.AddAsync(new InspectionFile() { FileName = realFileName, RelativePath = ossRelativePath, TrialId = trialId }); diff --git a/IRaCIS.Core.Application/Helper/OSSService.cs b/IRaCIS.Core.Application/Helper/OSSService.cs index 7cbe43924..5f370b698 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,6 +15,7 @@ using System.Linq; using System.Security.AccessControl; using System.Text; using System.Threading.Tasks; +using SkiaSharp; namespace IRaCIS.Core.Application.Helper { @@ -81,78 +84,85 @@ 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 AliyunOSSOptions _OSSConfig { get; set; } + public ObjectStoreServiceOptions ObjectStoreServiceOptions { get; set; } - public OssClient _ossClient { 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 + 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); + } } - catch (Exception ex) - { - throw new BusinessValidationFailedException("oss上传失败" + ex.Message); - } + + return "/" + ossRelativePath; + } + + /// /// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder /// @@ -160,38 +170,84 @@ 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; - 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); } + + 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); + } } catch (Exception ex) @@ -201,6 +257,9 @@ namespace IRaCIS.Core.Application.Helper } + + + }