Study 上传 迁移
parent
c4e53d5654
commit
09acd12586
|
@ -24,6 +24,13 @@ using IRaCIS.Core.Domain.Models;
|
|||
using IRaCIS.Core.Infrastructure;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MassTransit;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Aliyun.Acs.Core.Profile;
|
||||
using Aliyun.Acs.Core.Auth.Sts;
|
||||
using Aliyun.Acs.Core;
|
||||
using IRaCIS.Core.Application.Helper;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace IRaCIS.Api.Controllers
|
||||
{
|
||||
|
@ -203,6 +210,44 @@ namespace IRaCIS.Api.Controllers
|
|||
}
|
||||
|
||||
|
||||
[HttpGet("user/GenerateSTS")]
|
||||
public IResponseOutput GenerateSTS(IOptionsMonitor<AliyunOssOptions> options )
|
||||
{
|
||||
var ossOptions = options.CurrentValue;
|
||||
|
||||
|
||||
IClientProfile profile = DefaultProfile.GetProfile(ossOptions.RegionId, ossOptions.AccessKeyId, ossOptions.AccessKeySecret);
|
||||
DefaultAcsClient client = new DefaultAcsClient(profile);
|
||||
|
||||
|
||||
// 创建一个STS请求
|
||||
AssumeRoleRequest request = new AssumeRoleRequest
|
||||
{
|
||||
RoleArn = ossOptions.RoleArn, // 角色ARN,需要替换为你的角色ARN
|
||||
RoleSessionName = $"session-name-{NewId.NextGuid()}", // 角色会话名称,可自定义
|
||||
DurationSeconds = 900, // 令牌有效期(单位:秒),这里设置为1小时
|
||||
};
|
||||
|
||||
|
||||
AssumeRoleResponse response = client.GetAcsResponse(request);
|
||||
|
||||
// 返回STS令牌信息给前端
|
||||
var stsToken = new
|
||||
{
|
||||
AccessKeyId = response.Credentials.AccessKeyId,
|
||||
AccessKeySecret = response.Credentials.AccessKeySecret,
|
||||
SecurityToken = response.Credentials.SecurityToken,
|
||||
Expiration = response.Credentials.Expiration,
|
||||
|
||||
Region = ossOptions.Region ,
|
||||
BucketName = ossOptions.BucketName ,
|
||||
ViewEndpoint = ossOptions.ViewEndpoint ,
|
||||
|
||||
};
|
||||
|
||||
return ResponseOutput.Ok(stsToken);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1222,6 +1222,59 @@ namespace IRaCIS.Core.API.Controllers
|
|||
|
||||
}
|
||||
|
||||
public enum UploadFileType
|
||||
{
|
||||
DataUpload = 1,
|
||||
|
||||
DataDownload = 2,
|
||||
|
||||
EmailAttachment = 3,
|
||||
|
||||
EmailBodyHtml = 4,
|
||||
|
||||
Other = 5
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 1:数据上传 2:导出、 3:邮件附件 4:邮件Html 通过 ----new
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost("SystemFile/Upload")]
|
||||
[DisableRequestSizeLimit]
|
||||
[DisableFormValueModelBinding]
|
||||
public async Task<IResponseOutput> Upload(UploadFileType fileType)
|
||||
{
|
||||
IResponseOutput result = null;
|
||||
|
||||
switch (fileType)
|
||||
{
|
||||
case UploadFileType.DataUpload:
|
||||
result = await SingleFileUploadAsync((fileName) => FileStoreHelper.GetSystemFileUploadPath(_hostEnvironment, StaticData.Folder.DataTemplate, fileName));
|
||||
|
||||
break;
|
||||
case UploadFileType.DataDownload:
|
||||
result = await SingleFileUploadAsync((fileName) => FileStoreHelper.GetSystemFileUploadPath(_hostEnvironment, StaticData.Folder.DataTemplate, fileName));
|
||||
|
||||
break;
|
||||
case UploadFileType.EmailAttachment:
|
||||
result = await SingleFileUploadAsync((fileName) => FileStoreHelper.GetSystemFileUploadPath(_hostEnvironment, StaticData.Folder.EmailTemplate, fileName));
|
||||
|
||||
break;
|
||||
|
||||
case UploadFileType.EmailBodyHtml:
|
||||
result = await SingleFileUploadAsync((fileName) => FileStoreHelper.GetSystemFileUploadPath(_hostEnvironment, StaticData.Folder.EmailTemplate, fileName));
|
||||
break;
|
||||
|
||||
default:
|
||||
result = await SingleFileUploadAsync((fileName) => FileStoreHelper.GetOtherFileUploadPath(_hostEnvironment, StaticData.Folder.TempFile, fileName));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
|
|
@ -66,6 +66,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="aliyun-net-sdk-sts" Version="3.1.1" />
|
||||
<PackageReference Include="AspNetCoreRateLimit" Version="4.0.2" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
|
||||
|
|
|
@ -24,6 +24,7 @@ using Invio.Extensions.Authentication.JwtBearer;
|
|||
using Microsoft.AspNetCore.SignalR;
|
||||
using IRaCIS.Core.Domain.Share;
|
||||
using Microsoft.AspNetCore.StaticFiles;
|
||||
using IRaCIS.Core.Application.Helper;
|
||||
|
||||
namespace IRaCIS.Core.API
|
||||
{
|
||||
|
@ -91,7 +92,7 @@ namespace IRaCIS.Core.API
|
|||
|
||||
services.AddOptions().Configure<SystemEmailSendConfig>( _configuration.GetSection("SystemEmailSendConfig"));
|
||||
services.AddOptions().Configure<ServiceVerifyConfigOption>(_configuration.GetSection("BasicSystemConfig"));
|
||||
|
||||
services.AddOptions().Configure<AliyunOssOptions>(_configuration.GetSection("AliyunOSS"));
|
||||
|
||||
|
||||
//动态WebApi + UnifiedApiResultFilter 省掉控制器代码
|
||||
|
|
|
@ -13,10 +13,14 @@
|
|||
},
|
||||
|
||||
"AliyunOSS": {
|
||||
"endpoint": "https://zyypacs-code.oss-cn-shanghai.aliyuncs.com",
|
||||
"accessKeyId": "mpXG7Nu6zTpsDrI1",
|
||||
"accessKeySecret": "yNINcEb099SkNfF6vYKaoP8TZNI3xZ",
|
||||
"bucketName": "zyypacs-code"
|
||||
"RegionId": "cn-shanghai",
|
||||
"Endpoint": "https://oss-cn-shanghai.aliyuncs.com",
|
||||
"AccessKeyId": "LTAI5tKvzs7ed3UfSpNk3xwQ",
|
||||
"AccessKeySecret": "zTIceGEShlZDGnLrCFfIGFE7TXVRio",
|
||||
"BucketName": "zy-sir-test-store",
|
||||
"RoleArn": "acs:ram::1899121822495495:role/oss-upload",
|
||||
"ViewEndpoint": "https://zy-sir-test-store.oss-cn-shanghai.aliyuncs.com",
|
||||
"Region": "oss-cn-shanghai"
|
||||
},
|
||||
"BasicSystemConfig": {
|
||||
|
||||
|
@ -30,7 +34,7 @@
|
|||
|
||||
"LoginMaxFailCount": 5,
|
||||
|
||||
"LoginFailLockMinutes":1
|
||||
"LoginFailLockMinutes": 1
|
||||
},
|
||||
|
||||
"SystemEmailSendConfig": {
|
||||
|
|
|
@ -116,6 +116,51 @@ public static class FileStoreHelper
|
|||
return physicalFilePath;
|
||||
}
|
||||
|
||||
|
||||
#region 修改后留存
|
||||
|
||||
|
||||
public static (string PhysicalPath, string RelativePath) GetSystemFileUploadPath(IWebHostEnvironment _hostEnvironment, string templateFolderName, string fileName)
|
||||
{
|
||||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||||
|
||||
//文件类型路径处理
|
||||
var uploadFolderPath = Path.Combine(rootPath, StaticData.Folder.SystemDataFolder, templateFolderName);
|
||||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||||
|
||||
|
||||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||||
|
||||
|
||||
var relativePath = $"/{StaticData.Folder.IRaCISDataFolder}/{StaticData.Folder.SystemDataFolder}/{templateFolderName}/{trustedFileNameForFileStorage}";
|
||||
|
||||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||||
|
||||
return (serverFilePath, relativePath);
|
||||
}
|
||||
|
||||
public static (string PhysicalPath, string RelativePath) GetOtherFileUploadPath(IWebHostEnvironment _hostEnvironment, string templateFolderName, string fileName)
|
||||
{
|
||||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||||
|
||||
//文件类型路径处理
|
||||
var uploadFolderPath = Path.Combine(rootPath, StaticData.Folder.OtherDataFolder, templateFolderName);
|
||||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||||
|
||||
|
||||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||||
|
||||
|
||||
var relativePath = $"/{StaticData.Folder.IRaCISDataFolder}/{StaticData.Folder.OtherDataFolder}/{templateFolderName}/{trustedFileNameForFileStorage}";
|
||||
|
||||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||||
|
||||
return (serverFilePath, relativePath);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
|
|
@ -0,0 +1,120 @@
|
|||
using Aliyun.OSS;
|
||||
using IRaCIS.Core.Infrastructure;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NPOI.HPSF;
|
||||
using SharpCompress.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Security.AccessControl;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IRaCIS.Core.Application.Helper
|
||||
{
|
||||
|
||||
public class AliyunOssOptions
|
||||
{
|
||||
public string RegionId { get; set; }
|
||||
public string AccessKeyId { get; set; }
|
||||
public string AccessKeySecret { get; set; }
|
||||
public string EndPoint { get; set; }
|
||||
public string BucketName { get; set; }
|
||||
|
||||
public string RoleArn { get;set; }
|
||||
|
||||
public string Region { get; set; }
|
||||
|
||||
public string ViewEndpoint { get; set; }
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public interface IOSSService
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class OSSService : IOSSService
|
||||
{
|
||||
|
||||
public AliyunOssOptions _OSSConfig { get; set; }
|
||||
|
||||
public OssClient _ossClient { get; set; }
|
||||
|
||||
|
||||
|
||||
public OSSService(IOptionsMonitor<AliyunOssOptions> options)
|
||||
{
|
||||
var ossOptions = options.CurrentValue;
|
||||
|
||||
_OSSConfig = new AliyunOssOptions()
|
||||
{
|
||||
RegionId = ossOptions.RegionId,
|
||||
AccessKeyId = ossOptions.AccessKeyId,
|
||||
AccessKeySecret = ossOptions.AccessKeySecret,
|
||||
EndPoint = ossOptions.EndPoint,
|
||||
BucketName = ossOptions.BucketName,
|
||||
RoleArn = ossOptions.RoleArn,
|
||||
Region = ossOptions.Region,
|
||||
ViewEndpoint = ossOptions.ViewEndpoint
|
||||
};
|
||||
|
||||
_ossClient = new OssClient(_OSSConfig.EndPoint, _OSSConfig.AccessKeyId, _OSSConfig.AccessKeySecret);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public string UploadToOSS(string localFilePath, string oosFolderPath)
|
||||
{
|
||||
var localFileName = Path.GetFileName(localFilePath);
|
||||
|
||||
var ossRelativePath = oosFolderPath + "/" + localFileName;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// 上传文件
|
||||
var result = _ossClient.PutObject(_OSSConfig.BucketName, ossRelativePath, localFilePath);
|
||||
|
||||
return ossRelativePath;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new BusinessValidationFailedException("oss上传失败" + ex.Message);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void DownLoadFromOSS(string ossRelativePath, string localFilePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = _ossClient.GetObject(_OSSConfig.BucketName, ossRelativePath);
|
||||
|
||||
// 将下载的文件流保存到本地文件
|
||||
using (var fs = File.OpenWrite(localFilePath))
|
||||
{
|
||||
result.Content.CopyTo(fs);
|
||||
fs.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
throw new BusinessValidationFailedException("oss下载失败!" + ex.Message);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -87,6 +87,11 @@ public static class StaticData
|
|||
public static readonly string SignDocumentFolder = "SignDocument";
|
||||
|
||||
public static readonly string DataTemplate = "DataTemplate";
|
||||
public static readonly string EmailTemplate = "EmailTemplate";
|
||||
|
||||
public static readonly string CommonFile = "CommonFile";
|
||||
|
||||
public static readonly string TempFile = "TempFile";
|
||||
|
||||
public static readonly string NoticeAttachment = "NoticeAttachment";
|
||||
|
||||
|
|
Loading…
Reference in New Issue