Compare commits

..

No commits in common. "3d304961d328224ffd99d2f914836a32a578dfb4" and "457544adc35c1abf26bb1be6ca4c6c5d606f3ce0" have entirely different histories.

4 changed files with 39 additions and 122 deletions

View File

@ -380,7 +380,6 @@ namespace IRaCIS.Api.Controllers
SessionToken= credentials.SessionToken, SessionToken= credentials.SessionToken,
Expiration=credentials.Expiration, Expiration=credentials.Expiration,
BucketName = awsOptions.BucketName, BucketName = awsOptions.BucketName,
EndPoint = awsOptions.EndPoint, EndPoint = awsOptions.EndPoint,
ViewEndpoint = awsOptions.ViewEndpoint, ViewEndpoint = awsOptions.ViewEndpoint,

View File

@ -37,7 +37,6 @@
"ViewEndpoint": "https://hir-oss.test.extimaging.com/irc-test" "ViewEndpoint": "https://hir-oss.test.extimaging.com/irc-test"
}, },
"AWS": { "AWS": {
"Region": "us-east-1",
"EndPoint": "s3.us-east-1.amazonaws.com", "EndPoint": "s3.us-east-1.amazonaws.com",
"UseSSL": true, "UseSSL": true,
"RoleArn": "arn:aws:iam::471112624751:role/sts_s3_upload", "RoleArn": "arn:aws:iam::471112624751:role/sts_s3_upload",

View File

@ -20,15 +20,9 @@ using System.Reactive.Linq;
using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Domain.Share;
using static MassTransit.ValidationResultExtensions; using static MassTransit.ValidationResultExtensions;
using IRaCIS.Core.Infrastructure.NewtonsoftJson; using IRaCIS.Core.Infrastructure.NewtonsoftJson;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using MassTransit.Caching.Internals;
namespace IRaCIS.Core.Application.Helper namespace IRaCIS.Core.Application.Helper
{ {
#region 绑定和返回模型
[LowerCamelCaseJson] [LowerCamelCaseJson]
public class MinIOOptions : AWSOptions public class MinIOOptions : AWSOptions
{ {
@ -36,7 +30,7 @@ namespace IRaCIS.Core.Application.Helper
} }
public class AWSOptions public class AWSOptions
{ {
public string EndPoint { get; set; } public string EndPoint { get; set; }
@ -47,7 +41,6 @@ namespace IRaCIS.Core.Application.Helper
public string BucketName { get; set; } public string BucketName { get; set; }
public string ViewEndpoint { get; set; } public string ViewEndpoint { get; set; }
public int DurationSeconds { get; set; } public int DurationSeconds { get; set; }
public string Region { get; set; }
} }
public class AliyunOSSOptions public class AliyunOSSOptions
@ -125,7 +118,6 @@ namespace IRaCIS.Core.Application.Helper
[LowerCamelCaseJson] [LowerCamelCaseJson]
public class AWSTempToken public class AWSTempToken
{ {
public string Region { get; set; }
public string SessionToken { get; set; } public string SessionToken { get; set; }
public string EndPoint { get; set; } public string EndPoint { get; set; }
public string AccessKeyId { get; set; } public string AccessKeyId { get; set; }
@ -142,10 +134,6 @@ namespace IRaCIS.Core.Application.Helper
AWS = 2, AWS = 2,
} }
#endregion
// aws 参考链接 https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/S3/S3_Basics
public interface IOSSService public interface IOSSService
{ {
public Task<string> UploadToOSSAsync(Stream fileStream, string oosFolderPath, string fileRealName, bool isFileNameAddGuid = true); public Task<string> UploadToOSSAsync(Stream fileStream, string oosFolderPath, string fileRealName, bool isFileNameAddGuid = true);
@ -182,6 +170,8 @@ namespace IRaCIS.Core.Application.Helper
public async Task<string> UploadToOSSAsync(Stream fileStream, string oosFolderPath, string fileRealName, bool isFileNameAddGuid = true) public async Task<string> UploadToOSSAsync(Stream fileStream, string oosFolderPath, string fileRealName, bool isFileNameAddGuid = true)
{ {
var ossRelativePath = isFileNameAddGuid ? $"{oosFolderPath}/{Guid.NewGuid()}_{fileRealName}" : $"{oosFolderPath}/{fileRealName}"; var ossRelativePath = isFileNameAddGuid ? $"{oosFolderPath}/{Guid.NewGuid()}_{fileRealName}" : $"{oosFolderPath}/{fileRealName}";
//var ossRelativePath = $"{oosFolderPath}/{Guid.NewGuid()}_{fileRealName}";
//var ossRelativePath = oosFolderPath + "/" + fileRealName;
try try
{ {
@ -225,27 +215,20 @@ namespace IRaCIS.Core.Application.Helper
} }
else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS") else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS")
{ {
var awsConfig = ObjectStoreServiceOptions.AWS; var minIOConfig = ObjectStoreServiceOptions.AWS;
// 提供awsAccessKeyId和awsSecretAccessKey构造凭证
var credentials = new BasicAWSCredentials(awsConfig.AccessKeyId, awsConfig.SecretAccessKey);
//提供awsEndPoint域名进行访问配置 var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.EndPoint}")
var clientConfig = new AmazonS3Config .WithCredentials(minIOConfig.AccessKeyId, minIOConfig.SecretAccessKey).WithSSL(minIOConfig.UseSSL)
{ .Build();
ServiceURL = awsConfig.EndPoint
};
var amazonS3Client = new AmazonS3Client(credentials, clientConfig); var putObjectArgs = new PutObjectArgs()
.WithBucket(minIOConfig.BucketName)
.WithObject(ossRelativePath)
.WithStreamData(memoryStream)
.WithObjectSize(memoryStream.Length);
var putObjectRequest = new Amazon.S3.Model.PutObjectRequest() await minioClient.PutObjectAsync(putObjectArgs);
{
BucketName = awsConfig.BucketName,
InputStream = memoryStream,
Key = ossRelativePath,
};
await amazonS3Client.PutObjectAsync(putObjectRequest);
} }
else else
{ {
@ -273,7 +256,6 @@ namespace IRaCIS.Core.Application.Helper
/// </summary> /// </summary>
/// <param name="localFilePath"></param> /// <param name="localFilePath"></param>
/// <param name="oosFolderPath"></param> /// <param name="oosFolderPath"></param>
/// <param name="isFileNameAddGuid"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="BusinessValidationFailedException"></exception> /// <exception cref="BusinessValidationFailedException"></exception>
public async Task<string> UploadToOSSAsync(string localFilePath, string oosFolderPath, bool isFileNameAddGuid = true) public async Task<string> UploadToOSSAsync(string localFilePath, string oosFolderPath, bool isFileNameAddGuid = true)
@ -283,6 +265,9 @@ namespace IRaCIS.Core.Application.Helper
var ossRelativePath = isFileNameAddGuid ? $"{oosFolderPath}/{Guid.NewGuid()}_{localFileName}" : $"{oosFolderPath}/{localFileName}"; var ossRelativePath = isFileNameAddGuid ? $"{oosFolderPath}/{Guid.NewGuid()}_{localFileName}" : $"{oosFolderPath}/{localFileName}";
//var ossRelativePath = oosFolderPath + "/" + localFileName;
if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS") if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS")
{ {
var aliConfig = ObjectStoreServiceOptions.AliyunOSS; var aliConfig = ObjectStoreServiceOptions.AliyunOSS;
@ -311,28 +296,19 @@ namespace IRaCIS.Core.Application.Helper
} }
else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS") else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS")
{ {
var awsConfig = ObjectStoreServiceOptions.AWS; var minIOConfig = ObjectStoreServiceOptions.AWS;
// 提供awsAccessKeyId和awsSecretAccessKey构造凭证
var credentials = new BasicAWSCredentials(awsConfig.AccessKeyId, awsConfig.SecretAccessKey);
//提供awsEndPoint域名进行访问配置 var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.EndPoint}")
var clientConfig = new AmazonS3Config .WithCredentials(minIOConfig.AccessKeyId, minIOConfig.SecretAccessKey).WithSSL(minIOConfig.UseSSL)
{ .Build();
ServiceURL = awsConfig.EndPoint
};
var amazonS3Client = new AmazonS3Client(credentials, clientConfig); var putObjectArgs = new PutObjectArgs()
.WithBucket(minIOConfig.BucketName)
var putObjectRequest = new Amazon.S3.Model.PutObjectRequest() .WithObject(ossRelativePath)
{ .WithFileName(localFilePath);
BucketName = awsConfig.BucketName,
FilePath = localFilePath,
Key = ossRelativePath,
};
await amazonS3Client.PutObjectAsync(putObjectRequest);
await minioClient.PutObjectAsync(putObjectArgs);
} }
else else
{ {
@ -345,7 +321,7 @@ namespace IRaCIS.Core.Application.Helper
public async Task DownLoadFromOSSAsync(string ossRelativePath, string localFilePath) public async Task DownLoadFromOSSAsync(string ossRelativePath, string localFilePath)
{ {
ossRelativePath = ossRelativePath.TrimStart('/'); ossRelativePath = ossRelativePath.TrimStart('/');
try try
{ {
@ -386,29 +362,18 @@ namespace IRaCIS.Core.Application.Helper
} }
else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS") else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS")
{ {
var awsConfig = ObjectStoreServiceOptions.AWS; var minIOConfig = ObjectStoreServiceOptions.AWS;
// 提供awsAccessKeyId和awsSecretAccessKey构造凭证 var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.EndPoint}")
var credentials = new BasicAWSCredentials(awsConfig.AccessKeyId, awsConfig.SecretAccessKey); .WithCredentials(minIOConfig.AccessKeyId, minIOConfig.SecretAccessKey).WithSSL(minIOConfig.UseSSL)
.Build();
//提供awsEndPoint域名进行访问配置
var clientConfig = new AmazonS3Config
{
ServiceURL = awsConfig.EndPoint
};
var amazonS3Client = new AmazonS3Client(credentials, clientConfig);
var getObjectArgs = new Amazon.S3.Model.GetObjectRequest()
{
BucketName = awsConfig.BucketName,
Key = ossRelativePath,
};
await (await amazonS3Client.GetObjectAsync(getObjectArgs)).WriteResponseStreamToFileAsync(localFilePath, true, CancellationToken.None);
var getObjectArgs = new GetObjectArgs()
.WithBucket(minIOConfig.BucketName)
.WithObject(ossRelativePath)
.WithFile(localFilePath);
await minioClient.GetObjectAsync(getObjectArgs);
} }
else else
{ {
@ -532,7 +497,7 @@ namespace IRaCIS.Core.Application.Helper
do do
{ {
// 使用 prefix 模拟目录结构,设置 MaxKeys 和 NextMarker // 使用 prefix 模拟目录结构,设置 MaxKeys 和 NextMarker
objectListing = _ossClient.ListObjects(new Aliyun.OSS.ListObjectsRequest(aliConfig.BucketName) objectListing = _ossClient.ListObjects(new ListObjectsRequest(aliConfig.BucketName)
{ {
Prefix = prefix, Prefix = prefix,
MaxKeys = 1000, MaxKeys = 1000,
@ -544,7 +509,7 @@ namespace IRaCIS.Core.Application.Helper
// 删除获取到的文件 // 删除获取到的文件
if (keys.Count > 0) if (keys.Count > 0)
{ {
_ossClient.DeleteObjects(new Aliyun.OSS.DeleteObjectsRequest(aliConfig.BucketName, keys, false)); _ossClient.DeleteObjects(new DeleteObjectsRequest(aliConfig.BucketName, keys, false));
} }
// 设置 NextMarker 以获取下一页的数据 // 设置 NextMarker 以获取下一页的数据
@ -573,7 +538,7 @@ namespace IRaCIS.Core.Application.Helper
var objects = minioClient.ListObjectsAsync(listArgs).ToListObservable().Select(t => t.Key).ToList(); var objects = minioClient.ListObjectsAsync(listArgs).ToListObservable().Select(t => t.Key).ToList();
if (objects.Count > 0) if (objects.Count > 0)
{ {
@ -590,52 +555,7 @@ namespace IRaCIS.Core.Application.Helper
} }
else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS") else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS")
{ {
throw new BusinessValidationFailedException("未定义的存储介质类型");
var awsConfig = ObjectStoreServiceOptions.AWS;
// 提供awsAccessKeyId和awsSecretAccessKey构造凭证
var credentials = new BasicAWSCredentials(awsConfig.AccessKeyId, awsConfig.SecretAccessKey);
//提供awsEndPoint域名进行访问配置
var clientConfig = new AmazonS3Config
{
ServiceURL = awsConfig.EndPoint
};
var amazonS3Client = new AmazonS3Client(credentials, clientConfig);
try
{
ListObjectsResponse objectListing = null;
string nextMarker = null;
do
{
// 使用 prefix 模拟目录结构,设置 MaxKeys 和 NextMarker
objectListing = await amazonS3Client.ListObjectsAsync(new Amazon.S3.Model.ListObjectsRequest()
{
Prefix = prefix,
MaxKeys = 1000,
Marker = nextMarker
});
//List<string> keys = objectListing.CommonPrefixes.Select(t => t.Key).ToList();
//// 删除获取到的文件
//if (keys.Count > 0)
//{
// //await amazonS3Client.DeleteObjectsAsync(new Amazon.S3.Model.DeleteObjectsRequest() { BucketName=awsConfig.BucketName,k}awsConfig.BucketName, keys, false));
//}
// 设置 NextMarker 以获取下一页的数据
nextMarker = objectListing.NextMarker;
} while (objectListing.IsTruncated);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
} }
else else
{ {

View File

@ -59,7 +59,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" /> <PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
<PackageReference Include="AWSSDK.S3" Version="3.7.402.4" />
<PackageReference Include="DocX" Version="3.0.1" /> <PackageReference Include="DocX" Version="3.0.1" />
<PackageReference Include="FreeSpire.Doc" Version="12.2.0" /> <PackageReference Include="FreeSpire.Doc" Version="12.2.0" />
<PackageReference Include="Hangfire.Core" Version="1.8.14" /> <PackageReference Include="Hangfire.Core" Version="1.8.14" />