xin
2025-06-03 9fbd156fa0eb3091dd033c3eebf5fbc53c6ab6de
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
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) {
        try {
            // 创建ObsClient实例
            ObsClient obsClient = getObsClient(properties);
            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);
        }
    }
 
    /**
     * 上传对象
     *
     * @param properties    obs配置
     * @param file      文件
     * @param objectKey 对象名
     * @return PutObjectResult
     */
    public static PutObjectResult putObject(ObsProperties properties, File file, String objectKey) {
        try {
            // 创建ObsClient实例
            ObsClient obsClient = getObsClient(properties);
            // 为待上传的本地文件路径,需要指定到具体的文件名
            return obsClient.putObject(properties.getBucket(), objectKey, file);
        } catch (Exception e) {
            throw new BadRequestException("上传对象失败" + e.getMessage());
        } finally {
            FileUtil.del(file);
        }
    }
 
    /**
     * 下载对象
     *
     * @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);
            }
        }
        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);
    }
}