疯狂的狮子li
2021-07-20 a4c019ace2841faa6af14b5373a041464c3dbbde
ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QcloudCloudStorageServiceImpl.java
@@ -5,6 +5,7 @@
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import com.ruoyi.oss.entity.UploadResult;
import com.ruoyi.oss.enumd.CloudServiceEnumd;
import com.ruoyi.oss.factory.OssFactory;
import com.ruoyi.oss.properties.CloudStorageProperties;
@@ -32,14 +33,18 @@
   @Autowired
   public QcloudCloudStorageServiceImpl(CloudStorageProperties properties) {
      this.properties = properties.getQcloud();
        COSCredentials credentials = new BasicCOSCredentials(
         this.properties.getSecretId(),
         this.properties.getSecretKey());
        // 初始化客户端配置
        ClientConfig clientConfig = new ClientConfig();
        // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
        clientConfig.setRegion(new Region(this.properties.getRegion()));
        client = new COSClient(credentials, clientConfig);
      try {
         COSCredentials credentials = new BasicCOSCredentials(
            this.properties.getSecretId(),
            this.properties.getSecretKey());
         // 初始化客户端配置
         ClientConfig clientConfig = new ClientConfig();
         // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
         clientConfig.setRegion(new Region(this.properties.getRegion()));
         client = new COSClient(credentials, clientConfig);
      } catch (Exception e) {
         throw new IllegalArgumentException("腾讯云存储配置错误! 请检查系统配置!");
      }
   }
   @Override
@@ -48,7 +53,7 @@
   }
   @Override
   public String upload(byte[] data, String path) {
   public UploadResult upload(byte[] data, String path, String contentType) {
      // 腾讯云必需要以"/"开头
      if (!path.startsWith("/")) {
         path = "/" + path;
@@ -60,7 +65,7 @@
//        if (Convert.toInt(jsonObject.get("code")) != 0) {
//            throw new OssException("文件上传失败," + Convert.toStr(jsonObject.get("message")));
//        }
      return this.properties.getDomain() + path;
      return new UploadResult().setUrl(properties.getDomain() + "/" + path).setFilename(path);
   }
   @Override
@@ -75,13 +80,13 @@
   }
   @Override
   public String uploadSuffix(byte[] data, String suffix) {
      return upload(data, getPath(this.properties.getPrefix(), suffix));
   public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
      return upload(data, getPath(this.properties.getPrefix(), suffix), contentType);
   }
   @Override
   public String uploadSuffix(InputStream inputStream, String suffix) {
      return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
   public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
      return upload(inputStream, getPath(this.properties.getPrefix(), suffix), contentType);
   }
   @Override