From 2d479a15ec7b05c1f465786851e171db783b7920 Mon Sep 17 00:00:00 2001
From: he <10978375@qq.com>
Date: Wed, 30 Aug 2023 11:10:37 +0800
Subject: [PATCH 1/2] =?UTF-8?q?OOS=20=E4=B8=8A=E4=BC=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Controllers/UploadDownLoadController.cs | 5 ++
IRaCIS.Core.API/appsettings.Test_Study.json | 8 +-
IRaCIS.Core.API/appsettings.Uat_Study.json | 6 ++
IRaCIS.Core.API/appsettings.json | 7 ++
.../Helper/FileStoreHelper.cs | 77 +++++++++++++++++++
.../IRaCIS.Core.Application.csproj | 1 +
6 files changed, 103 insertions(+), 1 deletion(-)
diff --git a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs
index ed89ecc45..478162e1c 100644
--- a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs
+++ b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs
@@ -969,6 +969,11 @@ namespace IRaCIS.Core.API.Controllers
_userInfo = userInfo;
}
+ [HttpPost, Route("TrialSiteSurvey/TestOOS")]
+ public string TestUploadFileToOOS(string path)
+ {
+ return FileStoreHelper.UploadOOS(path);
+ }
[HttpPost, Route("TrialSiteSurvey/UploadTrialSiteSurveyUser")]
[DisableFormValueModelBinding]
diff --git a/IRaCIS.Core.API/appsettings.Test_Study.json b/IRaCIS.Core.API/appsettings.Test_Study.json
index fff5a9b8e..095d3c718 100644
--- a/IRaCIS.Core.API/appsettings.Test_Study.json
+++ b/IRaCIS.Core.API/appsettings.Test_Study.json
@@ -6,6 +6,12 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
+ "AliyunOSS": {
+ "endpoint": "http://oss-cn-shanghai.aliyuncs.com",
+ "accessKeyId": "mpXG7Nu6zTpsDrI1",
+ "accessKeySecret": "yNINcEb099SkNfF6vYKaoP8TZNI3xZ",
+ "bucketName": "zyypacs"
+ },
"ConnectionStrings": {
"RemoteNew": "Server=123.56.94.154,1433\\MSSQLSERVER;Database=Test.Study;User ID=sa;Password=dev123456DEV;TrustServerCertificate=true",
"Hangfire": "Server=123.56.94.154,1433\\MSSQLSERVER;Database=Test.Study.hangfire;User ID=sa;Password=dev123456DEV;TrustServerCertificate=true"
@@ -22,7 +28,7 @@
"LoginMaxFailCount": 5,
"LoginFailLockMinutes": 30
-
+
},
"SystemEmailSendConfig": {
diff --git a/IRaCIS.Core.API/appsettings.Uat_Study.json b/IRaCIS.Core.API/appsettings.Uat_Study.json
index 7793be29a..c386ad7ff 100644
--- a/IRaCIS.Core.API/appsettings.Uat_Study.json
+++ b/IRaCIS.Core.API/appsettings.Uat_Study.json
@@ -6,6 +6,12 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
+ "AliyunOSS": {
+ "endpoint": "http://oss-cn-shanghai.aliyuncs.com",
+ "accessKeyId": "mpXG7Nu6zTpsDrI1",
+ "accessKeySecret": "yNINcEb099SkNfF6vYKaoP8TZNI3xZ",
+ "bucketName": "zyypacs"
+ },
"ConnectionStrings": {
"RemoteNew": "Server=123.56.94.154,1433\\MSSQLSERVER;Database=Uat.Study;User ID=sa;Password=dev123456DEV;TrustServerCertificate=true",
"Hangfire": "Server=123.56.94.154,1433\\MSSQLSERVER;Database=Uat.Study.hangfire;User ID=sa;Password=dev123456DEV;TrustServerCertificate=true"
diff --git a/IRaCIS.Core.API/appsettings.json b/IRaCIS.Core.API/appsettings.json
index f142d1f51..a2828b8c7 100644
--- a/IRaCIS.Core.API/appsettings.json
+++ b/IRaCIS.Core.API/appsettings.json
@@ -5,6 +5,12 @@
"Audience": "ZhiZhun",
"TokenExpireDays": "7"
},
+ "AliyunOSS": {
+ "endpoint": "http://oss-cn-shanghai.aliyuncs.com",
+ "accessKeyId": "mpXG7Nu6zTpsDrI1",
+ "accessKeySecret": "yNINcEb099SkNfF6vYKaoP8TZNI3xZ",
+ "bucketName": "zyypacs"
+ },
"IpRateLimiting": {
"EnableEndpointRateLimiting": true,
"StackBlockedRequests": false,
@@ -19,6 +25,7 @@
"EndpointWhitelist": [
"post:/study/archivestudy/*"
],
+
"IpWhitelist": [],
"GeneralRules": [
{
diff --git a/IRaCIS.Core.Application/Helper/FileStoreHelper.cs b/IRaCIS.Core.Application/Helper/FileStoreHelper.cs
index a46c252bb..0c6d66cb3 100644
--- a/IRaCIS.Core.Application/Helper/FileStoreHelper.cs
+++ b/IRaCIS.Core.Application/Helper/FileStoreHelper.cs
@@ -1,10 +1,14 @@
+using Aliyun.OSS;
+using Aliyun.OSS.Util;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Linq;
+using System.Text;
using System.Text.RegularExpressions;
namespace IRaCIS.Core.Application.Helper;
@@ -12,6 +16,79 @@ namespace IRaCIS.Core.Application.Helper;
public static class FileStoreHelper
{
+ ///
+ /// 上传文件到OOS
+ ///
+ /// 本地文件路径
+ /// 是否删除本地文件
+ /// 返回文件路径
+ ///
+ public static string UploadOOS(string filePath, bool IsDeleteOriginalFile)
+ {
+ var configuration = new ConfigurationBuilder()
+ .AddJsonFile("appsettings.json")
+ .Build();
+
+ var endpoint = configuration.GetSection("AliyunOSS:endpoint").Value;
+ // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
+
+ // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
+ var accessKeyId = configuration.GetSection("AliyunOSS:accessKeyId").Value;
+ var accessKeySecret = configuration.GetSection("AliyunOSS:accessKeySecret").Value;
+ // 填写Bucket名称,例如examplebucket。
+ var bucketName = configuration.GetSection("AliyunOSS:bucketName").Value;
+ // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
+
+ var fileNameList = filePath.Split('\\').ToList();
+ var fileName = fileNameList[fileNameList.Count() - 1];
+
+ var objectName = fileName;
+ // 填写本地文件完整路径,例如D:\\localpath\\examplefile.txt。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
+ var localFilename = filePath;
+ // 创建OSSClient实例。
+ var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
+ try
+ {
+ // 上传文件。
+ var result = client.PutObject(bucketName, objectName, localFilename);
+ var expiration = DateTime.Now.AddYears(1);
+ var url = client.GeneratePresignedUri(bucketName, objectName, expiration).ToString();
+ if (File.Exists(filePath)&& IsDeleteOriginalFile)
+ {
+ // 删除文件
+ File.Delete(filePath);
+ }
+ return url;
+ }
+ catch (Exception ex)
+ {
+ throw new BusinessValidationFailedException("上传异常!");
+
+ }
+ }
+
+ private static ObjectMetadata BuildCallbackMetadata(string callbackUrl, string callbackBody)
+ {
+ string callbackHeaderBuilder = new CallbackHeaderBuilder(callbackUrl, callbackBody).Build();
+ string CallbackVariableHeaderBuilder = new CallbackVariableHeaderBuilder().
+ AddCallbackVariable("x:var1", "x:value1").AddCallbackVariable("x:var2", "x:value2").Build();
+ var metadata = new ObjectMetadata();
+ metadata.AddHeader(HttpHeaders.Callback, callbackHeaderBuilder);
+ metadata.AddHeader(HttpHeaders.CallbackVar, CallbackVariableHeaderBuilder);
+ return metadata;
+ }
+
+ private static string GetCallbackResponse(PutObjectResult putObjectResult)
+ {
+ string callbackResponse = null;
+ using (var stream = putObjectResult.ResponseStream)
+ {
+ var buffer = new byte[4 * 1024];
+ var bytesRead = stream.Read(buffer, 0, buffer.Length);
+ callbackResponse = Encoding.Default.GetString(buffer, 0, bytesRead);
+ }
+ return callbackResponse;
+ }
//处理文件名 压缩包,或者目录类的 会带上相对路径
public static (string TrustedFileNameForFileStorage, string RealName) GetStoreFileName(string fileName,bool isChangeToPdfFormat=false)
diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj
index c94eebde3..bf608d946 100644
--- a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj
+++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj
@@ -63,6 +63,7 @@
+
From bc383d9b23a3c046f003a5545ee0be12983c6c90 Mon Sep 17 00:00:00 2001
From: he <10978375@qq.com>
Date: Wed, 30 Aug 2023 11:11:22 +0800
Subject: [PATCH 2/2] =?UTF-8?q?OOS=E4=B8=8A=E4=BC=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
IRaCIS.Core.Application/Helper/FileStoreHelper.cs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/IRaCIS.Core.Application/Helper/FileStoreHelper.cs b/IRaCIS.Core.Application/Helper/FileStoreHelper.cs
index 0c6d66cb3..e5b875cce 100644
--- a/IRaCIS.Core.Application/Helper/FileStoreHelper.cs
+++ b/IRaCIS.Core.Application/Helper/FileStoreHelper.cs
@@ -20,10 +20,10 @@ public static class FileStoreHelper
/// 上传文件到OOS
///
/// 本地文件路径
- /// 是否删除本地文件
+ /// 是否删除本地文件
/// 返回文件路径
///
- public static string UploadOOS(string filePath, bool IsDeleteOriginalFile)
+ public static string UploadOOS(string filePath, bool isDeleteOriginalFile)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
@@ -53,7 +53,7 @@ public static class FileStoreHelper
var result = client.PutObject(bucketName, objectName, localFilename);
var expiration = DateTime.Now.AddYears(1);
var url = client.GeneratePresignedUri(bucketName, objectName, expiration).ToString();
- if (File.Exists(filePath)&& IsDeleteOriginalFile)
+ if (File.Exists(filePath)&& isDeleteOriginalFile)
{
// 删除文件
File.Delete(filePath);