OSS 对接代码修改
continuous-integration/drone/push Build is passing Details

IRC_NewDev
hang 2024-01-10 14:57:41 +08:00
parent e2639b8055
commit c35da39ba6
2 changed files with 109 additions and 50 deletions

View File

@ -566,7 +566,7 @@ namespace IRaCIS.Core.API.Controllers
templateFileStream.Seek(0, SeekOrigin.Begin); 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 }); 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")); 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 }); await _inspectionFileRepository.AddAsync(new InspectionFile() { FileName = realFileName, RelativePath = ossRelativePath, TrialId = trialId });

View File

@ -3,6 +3,8 @@ using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Infrastructure; using IRaCIS.Core.Infrastructure;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Minio.DataModel.Args;
using Minio;
using NPOI.HPSF; using NPOI.HPSF;
using SharpCompress.Common; using SharpCompress.Common;
using System; using System;
@ -13,6 +15,7 @@ using System.Linq;
using System.Security.AccessControl; using System.Security.AccessControl;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using SkiaSharp;
namespace IRaCIS.Core.Application.Helper namespace IRaCIS.Core.Application.Helper
{ {
@ -81,78 +84,85 @@ namespace IRaCIS.Core.Application.Helper
AWS = 2, AWS = 2,
} }
public interface IOSSService public interface IOSSService
{ {
public string UploadToOSS(Stream fileStream, string oosFolderPath, string fileRealName); public Task<string> UploadToOSSAsync(Stream fileStream, string oosFolderPath, string fileRealName);
public string UploadToOSS(string localFilePath, string oosFolderPath); public Task<string> 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 class OSSService : IOSSService
{ {
public ObjectStoreServiceOptions ObjectStoreServiceOptions { get; set; }
public AliyunOSSOptions _OSSConfig { get; set; }
public OssClient _ossClient { get; set; }
public OSSService(IOptionsMonitor<ObjectStoreServiceOptions> options) public OSSService(IOptionsMonitor<ObjectStoreServiceOptions> options)
{ {
var ossOptions = options.CurrentValue; ObjectStoreServiceOptions = options.CurrentValue;
_OSSConfig = ossOptions.AliyunOSS;
_ossClient = new OssClient(_OSSConfig.endPoint, _OSSConfig.accessKeyId, _OSSConfig.accessKeySecret);
} }
/// <summary> /// <summary>
/// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder /// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder
/// </summary> /// </summary>
/// <param name="fileStream"></param> /// <param name="fileStream"></param>
/// <param name="oosFolderPath"></param> /// <param name="oosFolderPath"></param>
/// <param name="fileRealName"></param> /// <param name="fileRealName"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="BusinessValidationFailedException"></exception> public async Task<string> UploadToOSSAsync(Stream fileStream, string oosFolderPath, string fileRealName)
public string UploadToOSS(Stream fileStream, string oosFolderPath,string fileRealName)
{ {
var ossRelativePath = oosFolderPath + "/" + 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;
} }
/// <summary> /// <summary>
/// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder /// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder
/// </summary> /// </summary>
@ -160,38 +170,84 @@ namespace IRaCIS.Core.Application.Helper
/// <param name="oosFolderPath"></param> /// <param name="oosFolderPath"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="BusinessValidationFailedException"></exception> /// <exception cref="BusinessValidationFailedException"></exception>
public string UploadToOSS(string localFilePath, string oosFolderPath) public async Task<string> UploadToOSSAsync(string localFilePath, string oosFolderPath)
{ {
var localFileName = Path.GetFileName(localFilePath); var localFileName = Path.GetFileName(localFilePath);
var ossRelativePath = oosFolderPath + "/" + 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);
} }
return "/" + ossRelativePath;
} }
public void DownLoadFromOSS(string ossRelativePath, string localFilePath) public async Task DownLoadFromOSSAsync(string ossRelativePath, string localFilePath)
{ {
ossRelativePath = ossRelativePath.TrimStart('/');
try try
{ {
var result = _ossClient.GetObject(_OSSConfig.bucketName, ossRelativePath);
// 将下载的文件流保存到本地文件
using (var fs = File.OpenWrite(localFilePath)) if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS")
{ {
result.Content.CopyTo(fs); var aliConfig = ObjectStoreServiceOptions.AliyunOSS;
fs.Close();
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) catch (Exception ex)
@ -201,6 +257,9 @@ namespace IRaCIS.Core.Application.Helper
} }
} }