k94314517
2024-12-25 0843f9b1526b7680f5cd419cf1f2d2fff44562ee
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
package com.doumee.core.oss;
 
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.BucketInfo;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
 
@Service
public class AliOssService {
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    /**
     * oss上传文件流
     *
     * @param fullFileName
     * @param file
     * @return
     */
    public Map uploadFile(String fullFileName, InputStream file) {
        Map res = new HashMap();
        String endpoint = systemDictDataBiz.queryByCode(Constants.OSS,Constants.ENDPOINT).getCode();
        String accessKeyId = systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_ID).getCode();
        String accessKeySecret = systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_KEY).getCode();
        String bucketName = systemDictDataBiz.queryByCode(Constants.OSS,Constants.BUCKETNAME).getCode();
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            ossClient.putObject(bucketName, fullFileName, file);
            String url = "https://" + bucketName + "." + endpoint + "/" + fullFileName;
            ossClient.shutdown();
            res.put("code", "200");
            res.put("url", url);
            res.put("msg", "上传成功");
        } catch (OSSException oe) {
            res.put("code", "500");
            res.put("url", "");
            res.put("msg", "尝试操作OSS失败:(" + oe.getErrorCode() + "," + oe.getMessage() + "),请登录阿里云后台核实BucketName域名空间或其它信息");
        } catch (ClientException ce) {
            res.put("code", "500");
            res.put("url", "");
            res.put("msg", "尝试连接OSS失败:(" + ce.getMessage() + "),请登录阿里云后台核实");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return res;
    }
 
 
 
    /**
     * 空间校验
     *
     * @param endpoint
     * @param accessKeyId
     * @param accessKeySecret
     * @param bucketName
     * @return
     */
    public Map vaid(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
        Map res = new HashMap();
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(endpoint.trim(), accessKeyId.trim(), accessKeySecret.trim());
            BucketInfo info = ossClient.getBucketInfo(bucketName.trim());
            String location = info.getBucket().getLocation();
            if (!endpoint.trim().contains(info.getBucket().getLocation())) {
                res.put("code", "500");
                res.put("msg", "Endpoint校验失败(检测" + bucketName + "结果为" + location + ",与您实际输入的Endpoint不符合,请前去更正)");
                return res;
            }
            if (info != null) {
                res.put("code", "200");
                res.put("msg", "OSS校验成功");
            } else {
                res.put("code", "500");
                res.put("msg", "bucketName空间不存在,请登录阿里云后台进行创建操作");
            }
        } catch (OSSException oe) {
            res.put("code", "500");
            res.put("msg", "尝试操作OSS失败:(" + oe.getErrorCode() + "," + oe.getMessage() + "),请登录阿里云后台核实BucketName域名空间或其它信息");
        } catch (ClientException ce) {
            res.put("code", "500");
            res.put("msg", ce.getMessage());
            res.put("msg", "尝试连接OSS失败:(" + ce.getMessage() + "),请登录阿里云后台核实");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return res;
    }
}