OOS 上传
parent
6f4d85ea2b
commit
2d479a15ec
|
@ -969,6 +969,11 @@ namespace IRaCIS.Core.API.Controllers
|
||||||
_userInfo = userInfo;
|
_userInfo = userInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost, Route("TrialSiteSurvey/TestOOS")]
|
||||||
|
public string TestUploadFileToOOS(string path)
|
||||||
|
{
|
||||||
|
return FileStoreHelper.UploadOOS(path);
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost, Route("TrialSiteSurvey/UploadTrialSiteSurveyUser")]
|
[HttpPost, Route("TrialSiteSurvey/UploadTrialSiteSurveyUser")]
|
||||||
[DisableFormValueModelBinding]
|
[DisableFormValueModelBinding]
|
||||||
|
|
|
@ -6,6 +6,12 @@
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"AliyunOSS": {
|
||||||
|
"endpoint": "http://oss-cn-shanghai.aliyuncs.com",
|
||||||
|
"accessKeyId": "mpXG7Nu6zTpsDrI1",
|
||||||
|
"accessKeySecret": "yNINcEb099SkNfF6vYKaoP8TZNI3xZ",
|
||||||
|
"bucketName": "zyypacs"
|
||||||
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"RemoteNew": "Server=123.56.94.154,1433\\MSSQLSERVER;Database=Test.Study;User ID=sa;Password=dev123456DEV;TrustServerCertificate=true",
|
"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"
|
"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,
|
"LoginMaxFailCount": 5,
|
||||||
|
|
||||||
"LoginFailLockMinutes": 30
|
"LoginFailLockMinutes": 30
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"SystemEmailSendConfig": {
|
"SystemEmailSendConfig": {
|
||||||
|
|
|
@ -6,6 +6,12 @@
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"AliyunOSS": {
|
||||||
|
"endpoint": "http://oss-cn-shanghai.aliyuncs.com",
|
||||||
|
"accessKeyId": "mpXG7Nu6zTpsDrI1",
|
||||||
|
"accessKeySecret": "yNINcEb099SkNfF6vYKaoP8TZNI3xZ",
|
||||||
|
"bucketName": "zyypacs"
|
||||||
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"RemoteNew": "Server=123.56.94.154,1433\\MSSQLSERVER;Database=Uat.Study;User ID=sa;Password=dev123456DEV;TrustServerCertificate=true",
|
"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"
|
"Hangfire": "Server=123.56.94.154,1433\\MSSQLSERVER;Database=Uat.Study.hangfire;User ID=sa;Password=dev123456DEV;TrustServerCertificate=true"
|
||||||
|
|
|
@ -5,6 +5,12 @@
|
||||||
"Audience": "ZhiZhun",
|
"Audience": "ZhiZhun",
|
||||||
"TokenExpireDays": "7"
|
"TokenExpireDays": "7"
|
||||||
},
|
},
|
||||||
|
"AliyunOSS": {
|
||||||
|
"endpoint": "http://oss-cn-shanghai.aliyuncs.com",
|
||||||
|
"accessKeyId": "mpXG7Nu6zTpsDrI1",
|
||||||
|
"accessKeySecret": "yNINcEb099SkNfF6vYKaoP8TZNI3xZ",
|
||||||
|
"bucketName": "zyypacs"
|
||||||
|
},
|
||||||
"IpRateLimiting": {
|
"IpRateLimiting": {
|
||||||
"EnableEndpointRateLimiting": true,
|
"EnableEndpointRateLimiting": true,
|
||||||
"StackBlockedRequests": false,
|
"StackBlockedRequests": false,
|
||||||
|
@ -19,6 +25,7 @@
|
||||||
"EndpointWhitelist": [
|
"EndpointWhitelist": [
|
||||||
"post:/study/archivestudy/*"
|
"post:/study/archivestudy/*"
|
||||||
],
|
],
|
||||||
|
|
||||||
"IpWhitelist": [],
|
"IpWhitelist": [],
|
||||||
"GeneralRules": [
|
"GeneralRules": [
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
|
|
||||||
|
|
||||||
|
using Aliyun.OSS;
|
||||||
|
using Aliyun.OSS.Util;
|
||||||
using IRaCIS.Core.Domain.Share;
|
using IRaCIS.Core.Domain.Share;
|
||||||
using IRaCIS.Core.Infrastructure;
|
using IRaCIS.Core.Infrastructure;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace IRaCIS.Core.Application.Helper;
|
namespace IRaCIS.Core.Application.Helper;
|
||||||
|
@ -12,6 +16,79 @@ namespace IRaCIS.Core.Application.Helper;
|
||||||
public static class FileStoreHelper
|
public static class FileStoreHelper
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上传文件到OOS
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filePath">本地文件路径</param>
|
||||||
|
/// <param name="IsDeleteOriginalFile">是否删除本地文件</param>
|
||||||
|
/// <returns>返回文件路径</returns>
|
||||||
|
/// <exception cref="BusinessValidationFailedException"></exception>
|
||||||
|
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)
|
public static (string TrustedFileNameForFileStorage, string RealName) GetStoreFileName(string fileName,bool isChangeToPdfFormat=false)
|
||||||
|
|
|
@ -63,6 +63,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.13.0" />
|
||||||
<PackageReference Include="AutoMapper.Collection.EntityFrameworkCore" Version="9.0.0" />
|
<PackageReference Include="AutoMapper.Collection.EntityFrameworkCore" Version="9.0.0" />
|
||||||
<PackageReference Include="EntityFrameworkCore.Triggered" Version="3.2.1" />
|
<PackageReference Include="EntityFrameworkCore.Triggered" Version="3.2.1" />
|
||||||
<PackageReference Include="Castle.Core.AsyncInterceptor" Version="2.1.0" />
|
<PackageReference Include="Castle.Core.AsyncInterceptor" Version="2.1.0" />
|
||||||
|
|
Loading…
Reference in New Issue