地质所 沉降监测网建设项目
chenhuan
2024-05-16 f992b4e508b358eba4170b1e9b1bb21319f7a3cd
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
package com.javaweb.oss.cloud;
 
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.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.javaweb.common.utils.file.FileUtils;
import com.javaweb.oss.domain.OssException;
import org.apache.commons.io.IOUtils;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * 腾讯云存储
 */
public class QcloudCloudStorageService extends CloudStorageService
{
    private COSClient client;
 
    public QcloudCloudStorageService(CloudStorageConfig config)
    {
        this.config = config;
        // 初始化
        init();
    }
 
    private void init()
    {
        COSCredentials credentials = new BasicCOSCredentials(config.getQcloudSecretId(),
                config.getQcloudSecretKey());
        // 设置bucket所在的区域,最新sdk不再支持简写,请填写完整
        Region region = new Region(config.getQcloudRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        client = new COSClient(credentials, clientConfig);
    }
 
    @Override
    public String upload(byte[] data, String path)
    {
        if (!path.startsWith("/"))
        {
            path = "/" + path;
        }
        File file = null;
        // 创建一个临时目录文件,结束后删除,腾讯云这个操作跟SB没区别,用流上传麻烦更多,权衡以后先这么做
        try
        {
            file = FileUtils.createTempFile(path, data);
            PutObjectRequest putObjectRequest = new PutObjectRequest(config.getQcloudBucketName(), path, file);
            PutObjectResult putObjectResult = client.putObject(putObjectRequest);
        }
        catch (Exception e)
        {
            throw new OssException("文件上传失败," + e.getMessage());
        }
        finally
        {
            if (null != file) file.delete();
        }
        return config.getQcloudDomain() + path;
    }
 
    @Override
    public String upload(InputStream inputStream, String path)
    {
        try
        {
            byte[] data = IOUtils.toByteArray(inputStream);
            return this.upload(data, path);
        }
        catch (IOException e)
        {
            throw new OssException("上传文件失败");
        }
    }
 
    @Override
    public String uploadSuffix(byte[] data, String suffix)
    {
        return upload(data, getPath(config.getQcloudPrefix(), suffix));
    }
 
    @Override
    public String uploadSuffix(InputStream inputStream, String suffix)
    {
        return upload(inputStream, getPath(config.getQcloudPrefix(), suffix));
    }
}