xiangpei
2025-05-22 e868be6c913a6a99e1491c088d052a5f58e252f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cn.lili.modules.file.plugin.impl;
 
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.modules.file.entity.enums.OssEnum;
import cn.lili.modules.file.plugin.FilePlugin;
import cn.lili.modules.system.entity.dto.OssSetting;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.DeleteObjectsRequest;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.region.Region;
import lombok.extern.slf4j.Slf4j;
 
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 腾讯cos 文件操作
 *
 * @author Bulbasaur
 */
 
@Slf4j
public class TencentFilePlugin implements FilePlugin {
 
    private OssSetting ossSetting;
 
    public TencentFilePlugin(OssSetting ossSetting) {
        this.ossSetting = ossSetting;
    }
 
    @Override
    public OssEnum pluginName() {
        return OssEnum.TENCENT_COS;
    }
 
    /**
     * 获取oss client
     *
     * @return
     */
    private COSClient getCOSClient() {
        // 1 初始化用户身份信息(secretId, secretKey)。
        COSCredentials cred = new BasicCOSCredentials(ossSetting.getTencentCOSSecretId(), ossSetting.getTencentCOSSecretKey());
        // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region(ossSetting.getTencentCOSRegion()));
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        return new COSClient(cred, clientConfig);
    }
 
 
    /**
     * 获取配置前缀
     *
     * @return
     */
    private String getUrlPrefix() {
        return "https://" + ossSetting.getTencentCOSBucket() + ".cos." + ossSetting.getTencentCOSRegion() + ".myqcloud.com/";
    }
 
    @Override
    public String pathUpload(String filePath, String key) {
        COSClient cosClient = getCOSClient();
        try {
            cosClient.putObject(ossSetting.getTencentCOSBucket(), key, new File(filePath));
        } catch (CosServiceException oe) {
            throw new ServiceException(ResultCode.OSS_EXCEPTION_ERROR);
        } catch (CosClientException ce) {
            throw new ServiceException(ResultCode.OSS_EXCEPTION_ERROR);
        } finally {
            cosClient.shutdown();
        }
        return getUrlPrefix() + key;
    }
 
    @Override
    public String inputStreamUpload(InputStream inputStream, String key) {
        COSClient cosClient = getCOSClient();
        try {
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType("image/jpg");
            cosClient.putObject(ossSetting.getTencentCOSBucket(), key, inputStream, meta);
        } catch (CosServiceException oe) {
            throw new ServiceException(ResultCode.OSS_EXCEPTION_ERROR);
        } catch (CosClientException ce) {
            throw new ServiceException(ResultCode.OSS_EXCEPTION_ERROR);
        } finally {
            cosClient.shutdown();
        }
        return getUrlPrefix() + key;
    }
 
    @Override
    public void deleteFile(List<String> keys) {
        COSClient cosClient = getCOSClient();
 
        try {
            List<DeleteObjectsRequest.KeyVersion> delObjects = new ArrayList<>();
            for (String key : keys) {
                delObjects.add(new DeleteObjectsRequest.KeyVersion(key));
            }
            cosClient.deleteObjects(new DeleteObjectsRequest(ossSetting.getTencentCOSBucket()).withKeys(delObjects));
        } catch (CosServiceException oe) {
            throw new ServiceException(ResultCode.OSS_EXCEPTION_ERROR);
        } catch (CosClientException ce) {
            throw new ServiceException(ResultCode.OSS_EXCEPTION_ERROR);
        } finally {
            cosClient.shutdown();
        }
    }
}