平安校园
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MinioUtils.cs 4.4 KiB

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