平安校园
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

129 líneas
4.4 KiB

  1. //
  2. using Minio;
  3. using Minio.Exceptions;
  4. namespace SafeCampus.System;
  5. /// <summary>
  6. /// Minio功能类
  7. /// </summary>
  8. public class MinioUtils : ITransient
  9. {
  10. public MinioClient MinioClient;
  11. private string _defaultBucketName;
  12. private string _defaultEndPoint;
  13. private string _defaultPrefix = "http://";
  14. private readonly IConfigService _configService;
  15. public MinioUtils(IConfigService configService)
  16. {
  17. _configService = configService;
  18. InitClient();
  19. }
  20. /// <summary>
  21. /// 初始化操作
  22. /// </summary>
  23. /// <returns></returns>
  24. private async void InitClient()
  25. {
  26. var configs = await _configService.GetConfigsByCategory(CateGoryConst.CONFIG_FILE_MINIO);//获取minio配置
  27. var accessKey = configs.Where(it => it.ConfigKey == SysConfigConst.FILE_MINIO_ACCESS_KEY).FirstOrDefault();//MINIO文件AccessKey
  28. var secretKey = configs.Where(it => it.ConfigKey == SysConfigConst.FILE_MINIO_SECRET_KEY).FirstOrDefault();//MINIO文件SecretKey
  29. var endPoint = configs.Where(it => it.ConfigKey == SysConfigConst.FILE_MINIO_END_POINT).FirstOrDefault();//MINIO文件EndPoint
  30. var bucketName = configs.Where(it => it.ConfigKey == SysConfigConst.FILE_MINIO_DEFAULT_BUCKET_NAME).FirstOrDefault();//MINIO文件默认存储桶
  31. if (accessKey == null || secretKey == null || endPoint == null || bucketName == null)
  32. {
  33. throw Oops.Oh("MINIO客户端未正确配置");
  34. }
  35. try
  36. {
  37. //默认值赋值
  38. _defaultBucketName = bucketName.ConfigValue;
  39. _defaultEndPoint = endPoint.ConfigValue;
  40. if (_defaultEndPoint.ToLower().StartsWith("http"))
  41. {
  42. var point = _defaultEndPoint.Split("//").ToList();//分割、
  43. _defaultPrefix = $"{point[0]}//";
  44. _defaultEndPoint = point[1];
  45. }
  46. MinioClient = new MinioClient().WithEndpoint(_defaultEndPoint).WithCredentials(accessKey.ConfigValue, secretKey.ConfigValue)
  47. .Build();//初始化minio对象
  48. MinioClient.WithTimeout(5000);//超时时间
  49. }
  50. catch (Exception ex)
  51. {
  52. throw Oops.Oh("MINIO客户端启动失败", ex);
  53. }
  54. }
  55. /// <summary>
  56. /// 上传文件 - 返回Minio文件完整Url
  57. /// </summary>
  58. /// <param name="objectName">存储桶里的对象名称,例:/mnt/photos/island.jpg</param>
  59. /// <param name="file">文件</param>
  60. /// <param name="contentType">文件的Content type,默认是"application/octet-stream"</param>
  61. /// <returns></returns>
  62. public async Task<string> PutObjectAsync(string objectName, IFormFile file, string contentType = "application/octet-stream")
  63. {
  64. try
  65. {
  66. using var fileStream = file.OpenReadStream();//获取文件流
  67. var putObjectArgs = new PutObjectArgs().WithBucket(_defaultBucketName).WithObject(objectName).WithStreamData(fileStream)
  68. .WithObjectSize(file.Length).WithContentType(contentType);
  69. await MinioClient.PutObjectAsync(putObjectArgs);
  70. return $"{_defaultPrefix}{_defaultEndPoint}/{_defaultBucketName}/{objectName}";//默认http
  71. }
  72. catch (MinioException e)
  73. {
  74. throw Oops.Oh("上传文件失败!", e);
  75. }
  76. catch (Exception e)
  77. {
  78. throw Oops.Oh("上传文件失败!", e);
  79. }
  80. }
  81. /// <summary>
  82. /// 下载文件
  83. /// </summary>
  84. /// <param name="objectName"></param>
  85. /// <returns></returns>
  86. public async Task<MemoryStream> DownloadFileAsync(string objectName)
  87. {
  88. var stream = new MemoryStream();
  89. try
  90. {
  91. var getObjectArgs = new GetObjectArgs().WithBucket(_defaultBucketName).WithObject(objectName).WithCallbackStream(cb =>
  92. {
  93. cb.CopyTo(stream);
  94. });
  95. await MinioClient.GetObjectAsync(getObjectArgs);
  96. //UserCenter.InvalidOperationException: Response Content-Length mismatch: too few bytes written (0 of 30788)
  97. if (stream.CanSeek)
  98. {
  99. stream.Seek(0, SeekOrigin.Begin);
  100. }
  101. return stream;
  102. }
  103. catch (MinioException e)
  104. {
  105. throw Oops.Oh("下载文件失败!", e);
  106. }
  107. catch (Exception e)
  108. {
  109. throw Oops.Oh("下载文件失败!", e);
  110. }
  111. }
  112. }