基础服务api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

95 lines
3.8 KiB

  1. using BPA.SAAS.Manage.Comm.Const;
  2. using BPA.SAAS.Manage.Core.Org;
  3. using COSXML.Auth;
  4. using COSXML.Model.Object;
  5. using COSXML.Transfer;
  6. using COSXML;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using BPA.SAAS.Manage.Application.System.Dtos;
  13. namespace BPA.SAAS.Manage.Application.System.Services
  14. {
  15. public class TransferUploadObjectModel
  16. {
  17. private readonly ISqlSugarClient _db;
  18. private CosXml cosXml;
  19. public TransferUploadObjectModel(ISqlSugarClient db)
  20. {
  21. var cosConfig = CosConfig.cosInfoOptions;
  22. _db = db; // 推荐操作
  23. CosXmlConfig config = new CosXmlConfig.Builder()
  24. .SetRegion(cosConfig.Region) // 设置默认的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
  25. .Build();
  26. string secretId = cosConfig.SecretId; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
  27. string secretKey = cosConfig.SecretKey; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
  28. long durationSecond = 6000; //每次请求签名有效时长,单位为秒
  29. QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
  30. secretKey, durationSecond);
  31. cosXml = new CosXmlServer(config, qCloudCredentialProvider);
  32. }
  33. /// 高级接口上传文件
  34. public async Task<string> TransferUploadFile(FileInputInfoDto inputDto, byte[] data)
  35. {
  36. var cosConfig = CosConfig.cosInfoOptions;
  37. // 初始化 TransferConfig
  38. TransferConfig transferConfig = new TransferConfig();
  39. // 手动设置开始分块上传的大小阈值为10MB,默认值为5MB
  40. transferConfig.DivisionForUpload = 10 * 1024 * 1024;
  41. // 手动设置分块上传中每个分块的大小为2MB,默认值为1MB
  42. transferConfig.SliceSizeForUpload = 2 * 1024 * 1024;
  43. string GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  44. string baseUrl = "/Franchisee/";
  45. if (!string.IsNullOrWhiteSpace(GroupId))
  46. {
  47. var Companycheck = _db.Queryable<BPA_Company>().Where(x => x.Id == GroupId).First();
  48. baseUrl = baseUrl + Companycheck.Code + "/";
  49. }
  50. DateTime dt = DateTime.Now;
  51. baseUrl = baseUrl + inputDto.Directory + "/" + dt.ToFileTime().ToString() + "." + inputDto.FileExtension;
  52. try
  53. {
  54. // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
  55. string bucket = cosConfig.Bucket;
  56. string cosPath = baseUrl; //对象在存储桶中的位置标识符,即称对象键
  57. // byte[] data = new byte[1024]; // 二进制数据
  58. PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, data);
  59. // 发起上传
  60. PutObjectResult result = cosXml.PutObject(putObjectRequest);
  61. Console.WriteLine(result.GetResultInfo());
  62. return @"https://" + cosConfig.Bucket + ".cos.ap-chengdu.myqcloud.com/" + cosPath;
  63. }
  64. catch (COSXML.CosException.CosClientException clientEx)
  65. {
  66. //请求失败
  67. Console.WriteLine("CosClientException: " + clientEx);
  68. }
  69. catch (COSXML.CosException.CosServerException serverEx)
  70. {
  71. //请求失败
  72. Console.WriteLine("CosServerException: " + serverEx.GetInfo());
  73. }
  74. return "";
  75. }
  76. }
  77. }