using Aliyun.OSS; using IRaCIS.Core.Application.Contracts; 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.IO; using System.Linq; using System.Security.AccessControl; using System.Text; using System.Threading.Tasks; namespace IRaCIS.Core.Application.Helper { public class MinIOOptions { public string Endpoint { get; set; } public string Port { get; set; } public bool UseSSL { get; set; } public string AccessKey { get; set; } public string SecretKey { get; set; } public string BucketName { get; set; } } 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 class ObjectStoreServiceOptions { public string ObjectStoreUse { get; set; } public AliyunOSSOptions AliyunOSS { get; set; } public MinIOOptions MinIO { get; set; } } public class ObjectStoreDTO { public string ObjectStoreUse { get; set; } public AliyunOSSTempToken AliyunOSS { get; set; } public MinIOOptions MinIO { get; set; } } public class AliyunOSSTempToken { public string AccessKeyId { get; set; } public string AccessKeySecret { get; set; } public string SecurityToken { get; set; } public string Expiration { get; set; } public string Region { get; set; } public string BucketName { get; set; } public string ViewEndpoint { get; set; } } public enum ObjectStoreUse { AliyunOSS = 0, MinIO = 1, AWS = 2, } public interface IOSSService { public string UploadToOSS(Stream fileStream, string oosFolderPath, string fileRealName); public string UploadToOSS(string localFilePath, string oosFolderPath); public void DownLoadFromOSS(string ossRelativePath, string localFilePath); } public class OSSService : IOSSService { public AliyunOSSOptions _OSSConfig { get; set; } public OssClient _ossClient { get; set; } public OSSService(IOptionsMonitor options) { var ossOptions = options.CurrentValue; _OSSConfig = ossOptions; _ossClient = new OssClient(_OSSConfig.EndPoint, _OSSConfig.AccessKeyId, _OSSConfig.AccessKeySecret); } /// /// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder /// /// /// /// /// /// public string UploadToOSS(Stream fileStream, string oosFolderPath,string fileRealName) { var ossRelativePath = oosFolderPath + "/" + fileRealName; try { using (var memoryStream = new MemoryStream()) { fileStream.Seek(0, SeekOrigin.Begin); fileStream.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); // 上传文件 var result = _ossClient.PutObject(_OSSConfig.BucketName, ossRelativePath, memoryStream); } return "/" + ossRelativePath; } catch (Exception ex) { throw new BusinessValidationFailedException("oss上传失败" + ex.Message); } } /// /// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder /// /// /// /// /// 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); } } } }