k94314517
2025-07-16 4384ad3db8f10b7add5f287681c069d4efb1e0ee
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.doumee.core.utils.huaweiOBS;
 
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.doumee.core.utils.azure.AzureBlobUtil;
import com.obs.services.BasicObsCredentialsProvider;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
 
/**
 * @date 2020/4/28
 */
public class ObsUtil {
 
    public ObsClient obsClient;
    public     String accessKey;
    public   String accessId;
    public    String endpoint;
 
    public BlobContainerClient blobContainerClient ;
 
    public ObsUtil(String accessId, String accessKey, String endpoint){
        try {
            this.accessId = accessId;
            this.accessKey = accessKey;
            this.endpoint = endpoint;
            this.obsClient = new ObsClient(new BasicObsCredentialsProvider(accessId, accessKey), this.endpoint);
        }catch (Exception e) {
            e.printStackTrace();
        }
 
    }
    private void initClient() {
        this.obsClient = new ObsClient(new BasicObsCredentialsProvider(this.accessId, this.accessKey), this.endpoint);
    }
    private void uploadLocalFile(File file,String bucket,String key) {
        try {
            if(this.obsClient == null){
                initClient();
            }
            // 文件上传
            // localfile 为待上传的本地文件路径,需要指定到具体的文件名
            PutObjectRequest request = new PutObjectRequest();
            request.setBucketName(bucket);
            request.setObjectKey(key);
            request.setFile(file);
            obsClient.putObject(request);
            System.out.println("putObject successfully");
        } catch (ObsException e) {
            System.out.println("putObject failed");
            // 请求失败,打印http状态码
            System.out.println("HTTP Code:" + e.getResponseCode());
            // 请求失败,打印服务端错误码
            System.out.println("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            System.out.println("Error Message:" + e.getErrorMessage());
            // 请求失败,打印请求id
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("putObject failed");
            // 其他异常信息打印
            e.printStackTrace();
        }
    }
 
 
    /**
     * 上传文件
     * @param is
     * @param objectKey
     * @return
     * @throws IOException
     */
    public  boolean uploadFile(String bucketName,InputStream is, String objectKey) throws Exception {
        if(this.obsClient == null){
            initClient();
        }
 
        try {
            // 上传字符串(byte数组)
            obsClient.putObject(bucketName, objectKey, is);
            System.out.println("putObject successfully");
        } catch (ObsException e) {
            System.out.println("putObject failed");
            // 请求失败,打印http状态码
            System.out.println("HTTP Code:" + e.getResponseCode());
            // 请求失败,打印服务端错误码
            System.out.println("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            System.out.println("Error Message:" + e.getErrorMessage());
            // 请求失败,打印请求id
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
           return false;
        } catch (Exception e) {
            System.out.println("putObject failed");
            // 其他异常信息打印
            e.printStackTrace();
            return false;
        }finally {
            if(this.obsClient!=null){
                this.obsClient.close();
            }
        }
 
       return true;
    }
 
    public static void main(String[] args) {
        ObsUtil blobUtil = new ObsUtil("HPUAQVBRXX9A9TLZ3RTA","uHC2uoFh42Z2xgQmCBBtG8rNZ4Caf85qQ2DQqZZf","obs.cn-south-1.myhuaweicloud.com");
        blobUtil.uploadLocalFile(new File("D://static/1.png"),"jinkuai","orders/1.png");
        blobUtil.uploadLocalFile(new File("D://static/2.png"),"jinkuai","orders/2.png");
        blobUtil.uploadLocalFile(new File("D://static/3.png"),"jinkuai","orders/3.png");
        blobUtil.uploadLocalFile(new File("D://static/4.png"),"jinkuai","orders/4.png");
    }
    public static  Integer uploadNetFile(ObsClient obsClient,String bucketName,String url, String objectKey) throws IOException {
        InputStream is = new URL(url).openStream();
        if(is != null){
            Boolean flag = obsClient.doesObjectExist(bucketName, objectKey);
            PutObjectResult result = null;
            result = obsClient.putObject(bucketName, objectKey, is);
            obsClient.close();
            return result.getStatusCode();
        }
        //同名文件可能被覆盖
        return null;
    }
 
    /**
     * 获得所有文件
     * @return
     * @throws IOException
     */
    public List<ObsObject> getAllFileInfo( ObsClient obsClient,String bucketName) throws IOException {
        ObjectListing objectList = obsClient.listObjects(bucketName);
        List<ObsObject> list = objectList.getObjects();
        obsClient.close();
        return list;
    }
 
    /**
     * 删除文件
     * @param objectKey
     * @return
     * @throws IOException
     */
    public Boolean removeFile(ObsClient obsClient,String bucketName,String objectKey) throws IOException {
        boolean exist = obsClient.doesObjectExist(bucketName, objectKey);
        DeleteObjectResult result = null;
        if (exist) {
            result = obsClient.deleteObject(bucketName, objectKey);
        }
        obsClient.close();
        //是否可以被标记为删除
        return result.isDeleteMarker();
    }
 
    /**
     * 获取文件对象-下载
     * @param objectKey
     * @return
     */
    public ObsObject getFile(ObsClient obsClient,String bucketName,String objectKey) {
        boolean exist = obsClient.doesObjectExist(bucketName, objectKey);
        if (exist) {
            ObsObject object = obsClient.getObject(bucketName, objectKey);
            return object;
        }
        return null;
    }
 
    /**
     * 预览授权访问-支持流式文件
     * 如果是流式文件,返回的链接可以在浏览器预览
     * 如果是非流式文件,返回的链接可以在浏览器里下载文件
     * @param objectKey
     * @return
     * @throws IOException
     */
    public String preview(ObsClient obsClient,String bucketName,String objectKey) throws IOException {
        //300有效时间
        TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, 300);
        request.setBucketName(bucketName);
        request.setObjectKey(objectKey);
        TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
        obsClient.close();
        return response.getSignedUrl();
    }
 
}