彭雪彬
2025-07-14 c1d20b425b10e8ba59f102dd1ab413055883eed0
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.oying.utils;
 
import com.obs.services.ObsClient;
import com.obs.services.model.AccessControlList;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectRequest;
import com.obs.services.model.PutObjectResult;
import com.oying.exception.BadRequestException;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
 
public class ObsUtils {
 
 
 
    /**
     * 创建ObsClient实例
     *
     * @param properties obs配置
     * @return ObsClient实例
     */
    public static ObsClient getObsClient(ObsProperties properties) {
        // 创建ObsClient实例
        return new ObsClient(properties.getAccessKeyId(), properties.getAccessKeySecret(), properties.getEndpoint());
    }
 
    /**
     * 上传对象权限
     *
     * @param properties    obs配置
     * @param file      文件
     * @param objectKey 对象名
     * @param acl       对象权限
     * @return PutObjectResult
     */
    public static PutObjectResult putObject(ObsProperties properties, File file, String objectKey, AccessControlList acl) {
        // 创建ObsClient实例
        ObsClient obsClient = getObsClient(properties);
        try {
            PutObjectRequest request = new PutObjectRequest();
            request.setBucketName(properties.getBucket());
            request.setObjectKey(objectKey);
            request.setFile(file);
            // 对象权限
            request.setAcl(acl);
            // 为待上传的本地文件路径,需要指定到具体的文件名
            return obsClient.putObject(request);
        } catch (Exception e) {
            throw new BadRequestException("上传对象失败" + e.getMessage());
        } finally {
            FileUtil.del(file);
            CloseUtil.close(obsClient);
        }
    }
 
    /**
     * 上传对象
     *
     * @param properties    obs配置
     * @param file      文件
     * @param objectKey 对象名
     * @return PutObjectResult
     */
    public static PutObjectResult putObject(ObsProperties properties, File file, String objectKey) {
        // 创建ObsClient实例
        ObsClient obsClient = getObsClient(properties);
        try {
            // 为待上传的本地文件路径,需要指定到具体的文件名
            return obsClient.putObject(properties.getBucket(), objectKey, file);
        } catch (Exception e) {
            throw new BadRequestException("上传对象失败" + e.getMessage());
        } finally {
            FileUtil.del(file);
            CloseUtil.close(obsClient);
        }
    }
 
    /**
     * 下载对象
     *
     * @param properties    obs配置
     * @param objectKey 对象名
     * @param path      保存路径
     */
    public static String getObject(ObsProperties properties, String objectKey, String path) {
        // 创建ObsClient实例
        ObsClient obsClient = getObsClient(properties);
        ObsObject obsObject = obsClient.getObject(properties.getBucket(), objectKey);
        InputStream in = obsObject.getObjectContent();
        if (in != null) {
            FileOutputStream out = null;
            try {
                // getCanonicalFile 可解析正确各种路径
                File dest = new File(path).getCanonicalFile();
                // 检测是否存在目录
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                out = new FileOutputStream(path);
                byte[] b = new byte[1024];
                int length;
                while ((length = in.read(b)) > 0) {
                    out.write(b, 0, length);
                }
            } catch (Exception e) {
                throw new BadRequestException("流输出异常");
            } finally {
                CloseUtil.close(in);
                CloseUtil.close(out);
                CloseUtil.close(obsClient);
            }
        }
        return path;
    }
 
    /**
     * 删除对象
     *
     * @param properties    obs配置
     * @param objectKey 对象名
     */
    public static void deleteObject(ObsProperties properties, String objectKey) {
        // 创建ObsClient实例
        ObsClient obsClient = getObsClient(properties);
        // 删除指定的对象
        obsClient.deleteObject(properties.getBucket(), objectKey);
        CloseUtil.close(obsClient);
    }
 
    public static String getPublicObjectUrl(ObsProperties properties, String objectKey) {
        return getPublicObjectUrl(properties.getBucket(), properties.getEndpoint(), objectKey);
    }
 
    public static String getPublicObjectUrl(String bucketName, String endpoint, String objectKey) {
        // 移除objectKey开头可能存在的文件分割符('/')
        String cleanObjectKey = objectKey.startsWith("/") ? objectKey.substring(1) : objectKey;
        // 标准URL格式
        return String.format("https://%s.%s/%s", bucketName, endpoint.replaceAll("^https?://", ""), cleanObjectKey);
    }
}