QiniuOssStrategy.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.ruoyi.oss.service.impl;
  2. import cn.hutool.core.util.ArrayUtil;
  3. import com.qiniu.http.Response;
  4. import com.qiniu.storage.BucketManager;
  5. import com.qiniu.storage.Configuration;
  6. import com.qiniu.storage.Region;
  7. import com.qiniu.storage.UploadManager;
  8. import com.qiniu.util.Auth;
  9. import com.ruoyi.oss.constant.OssConstant;
  10. import com.ruoyi.oss.entity.UploadResult;
  11. import com.ruoyi.oss.enumd.OssEnumd;
  12. import com.ruoyi.oss.exception.OssException;
  13. import com.ruoyi.oss.properties.OssProperties;
  14. import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
  15. import org.springframework.stereotype.Component;
  16. import java.io.InputStream;
  17. /**
  18. * 七牛云存储策略
  19. *
  20. * @author Lion Li
  21. */
  22. @Component
  23. public class QiniuOssStrategy extends AbstractOssStrategy {
  24. private UploadManager uploadManager;
  25. private BucketManager bucketManager;
  26. private Auth auth;
  27. @Override
  28. public void init(OssProperties ossProperties) {
  29. super.init(ossProperties);
  30. try {
  31. Configuration config = new Configuration(getRegion(properties.getRegion()));
  32. // https设置
  33. config.useHttpsDomains = OssConstant.IS_HTTPS.equals(properties.getIsHttps());
  34. uploadManager = new UploadManager(config);
  35. auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
  36. bucketManager = new BucketManager(auth, config);
  37. createBucket();
  38. } catch (Exception e) {
  39. throw new OssException("七牛云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
  40. }
  41. isInit = true;
  42. }
  43. @Override
  44. public void createBucket() {
  45. try {
  46. String bucketName = properties.getBucketName();
  47. if (ArrayUtil.contains(bucketManager.buckets(), bucketName)) {
  48. return;
  49. }
  50. bucketManager.createBucket(bucketName, properties.getRegion());
  51. } catch (Exception e) {
  52. throw new OssException("创建Bucket失败, 请核对七牛云配置信息:[" + e.getMessage() + "]");
  53. }
  54. }
  55. @Override
  56. public OssEnumd getServiceType() {
  57. return OssEnumd.QINIU;
  58. }
  59. @Override
  60. public UploadResult upload(byte[] data, String path, String contentType) {
  61. try {
  62. String token = auth.uploadToken(properties.getBucketName());
  63. Response res = uploadManager.put(data, path, token, null, contentType, false);
  64. if (!res.isOK()) {
  65. throw new RuntimeException("上传七牛出错:" + res.error);
  66. }
  67. } catch (Exception e) {
  68. throw new OssException("上传文件失败,请核对七牛配置信息:[" + e.getMessage() + "]");
  69. }
  70. return UploadResult.builder().url(getEndpointLink() + "/" + path).filename(path).build();
  71. }
  72. @Override
  73. public void delete(String path) {
  74. try {
  75. path = path.replace(getEndpointLink() + "/", "");
  76. Response res = bucketManager.delete(properties.getBucketName(), path);
  77. if (!res.isOK()) {
  78. throw new RuntimeException("删除七牛文件出错:" + res.error);
  79. }
  80. } catch (Exception e) {
  81. throw new OssException(e.getMessage());
  82. }
  83. }
  84. @Override
  85. public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
  86. return upload(data, getPath(properties.getPrefix(), suffix), contentType);
  87. }
  88. @Override
  89. public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
  90. return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
  91. }
  92. @Override
  93. public String getEndpointLink() {
  94. return properties.getEndpoint();
  95. }
  96. private Region getRegion(String region) {
  97. switch (region) {
  98. case "z0":
  99. return Region.region0();
  100. case "z1":
  101. return Region.region1();
  102. case "z2":
  103. return Region.region2();
  104. case "na0":
  105. return Region.regionNa0();
  106. case "as0":
  107. return Region.regionAs0();
  108. default:
  109. return Region.autoRegion();
  110. }
  111. }
  112. }