QcloudOssStrategy.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.ruoyi.oss.service.impl;
  2. import com.qcloud.cos.COSClient;
  3. import com.qcloud.cos.ClientConfig;
  4. import com.qcloud.cos.auth.BasicCOSCredentials;
  5. import com.qcloud.cos.auth.COSCredentials;
  6. import com.qcloud.cos.http.HttpProtocol;
  7. import com.qcloud.cos.model.*;
  8. import com.qcloud.cos.region.Region;
  9. import com.ruoyi.common.utils.StringUtils;
  10. import com.ruoyi.oss.constant.OssConstant;
  11. import com.ruoyi.oss.entity.UploadResult;
  12. import com.ruoyi.oss.enumd.OssEnumd;
  13. import com.ruoyi.oss.exception.OssException;
  14. import com.ruoyi.oss.properties.OssProperties;
  15. import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
  16. import org.springframework.stereotype.Component;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.InputStream;
  19. /**
  20. * 腾讯云存储策略
  21. *
  22. * @author Lion Li
  23. */
  24. @Component
  25. public class QcloudOssStrategy extends AbstractOssStrategy {
  26. private COSClient client;
  27. @Override
  28. public void init(OssProperties ossProperties) {
  29. super.init(ossProperties);
  30. try {
  31. COSCredentials credentials = new BasicCOSCredentials(
  32. properties.getAccessKey(), properties.getSecretKey());
  33. // 初始化客户端配置
  34. ClientConfig clientConfig = new ClientConfig();
  35. // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
  36. clientConfig.setRegion(new Region(properties.getRegion()));
  37. if (OssConstant.IS_HTTPS.equals(properties.getIsHttps())) {
  38. clientConfig.setHttpProtocol(HttpProtocol.https);
  39. } else {
  40. clientConfig.setHttpProtocol(HttpProtocol.http);
  41. }
  42. client = new COSClient(credentials, clientConfig);
  43. createBucket();
  44. } catch (Exception e) {
  45. throw new OssException("腾讯云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
  46. }
  47. isInit = true;
  48. }
  49. @Override
  50. public void createBucket() {
  51. try {
  52. String bucketName = properties.getBucketName();
  53. if (client.doesBucketExist(bucketName)) {
  54. return;
  55. }
  56. CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
  57. createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
  58. client.createBucket(createBucketRequest);
  59. } catch (Exception e) {
  60. throw new OssException("创建Bucket失败, 请核对腾讯云配置信息:[" + e.getMessage() + "]");
  61. }
  62. }
  63. @Override
  64. public OssEnumd getServiceType() {
  65. return OssEnumd.QCLOUD;
  66. }
  67. @Override
  68. public UploadResult upload(byte[] data, String path, String contentType) {
  69. return upload(new ByteArrayInputStream(data), path, contentType);
  70. }
  71. @Override
  72. public UploadResult upload(InputStream inputStream, String path, String contentType) {
  73. try {
  74. ObjectMetadata metadata = new ObjectMetadata();
  75. metadata.setContentType(contentType);
  76. client.putObject(new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata));
  77. } catch (Exception e) {
  78. throw new OssException("上传文件失败,请检查腾讯云配置信息:[" + e.getMessage() + "]");
  79. }
  80. return UploadResult.builder().url(getEndpointLink() + "/" + path).filename(path).build();
  81. }
  82. @Override
  83. public void delete(String path) {
  84. path = path.replace(getEndpointLink() + "/", "");
  85. try {
  86. client.deleteObject(new DeleteObjectRequest(properties.getBucketName(), path));
  87. } catch (Exception e) {
  88. throw new OssException("上传文件失败,请检腾讯云查配置信息:[" + e.getMessage() + "]");
  89. }
  90. }
  91. @Override
  92. public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
  93. return upload(data, getPath(properties.getPrefix(), suffix), contentType);
  94. }
  95. @Override
  96. public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
  97. return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType);
  98. }
  99. @Override
  100. public String getEndpointLink() {
  101. String endpoint = properties.getEndpoint();
  102. StringBuilder sb = new StringBuilder(endpoint);
  103. if (StringUtils.containsAnyIgnoreCase(endpoint, "http://")) {
  104. sb.insert(7, properties.getBucketName() + ".");
  105. } else if (StringUtils.containsAnyIgnoreCase(endpoint, "https://")) {
  106. sb.insert(8, properties.getBucketName() + ".");
  107. } else {
  108. throw new OssException("Endpoint配置错误");
  109. }
  110. return sb.toString();
  111. }
  112. }