bug
jiangqs
2023-07-27 c927b4d36b9e04a8b2ba08c8789f5277aa1c277d
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.ruoyi.file.utils;
 
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.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.InputStream;
import java.util.Calendar;
 
/**
 * @author jqs34
 * @version 1.0
 * @classname OBSUploadUtils
 * @description: TODO
 * @date 2023 2023/5/1 15:40
 */
public class OBSUploadUtils {
 
 
    protected static OSS createOss(){
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tAfKFuhyKFH12CTkXFj";
        String accessKeySecret = "tIBRuonHuQQPdcYrmlCdXlexOSwVXe";
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "hongruitang";
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }
 
    public static String uploadFile (MultipartFile file) throws Exception {
 
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String bucketName = "hongruitang";
        Calendar calendar = Calendar.getInstance();
        // 获取当前年
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        // 获取当前月
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        // 获取当前日
        String day = String.valueOf(calendar.get(Calendar.DATE));
        String filePath = year+"/"+month+"/"+day+"/";
        String uuid = IdUtils.fastSimpleUUID();
        // 创建OSSClient实例。
        OSS ossClient = createOss();
        PutObjectResult result = null;
        try {
 
            String fileName = FileUploadUtils.extractFilename(file);
            System.out.println(fileName + "开始上传");
            String prefix = fileName.substring(fileName.lastIndexOf("."));
            String objectName = filePath + uuid + prefix;
            InputStream inputStream = file.getInputStream();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 设置该属性可以返回response。如果不设置,则返回的response为空。
            putObjectRequest.setProcess("true");
            // 创建PutObject请求。
            result = ossClient.putObject(putObjectRequest);
            // 如果上传成功,则返回200。
            System.out.println(fileName + "上传返回" + result.getResponse().getStatusCode());
            return result.getResponse().getUri();
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return result.getResponse().getErrorResponseAsString();
    }
 
    public static String uploadLocalFile (File file) throws Exception {
 
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String bucketName = "hongruitang";
        Calendar calendar = Calendar.getInstance();
        // 获取当前年
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        // 获取当前月
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        // 获取当前日
        String day = String.valueOf(calendar.get(Calendar.DATE));
        String filePath = year+"/"+month+"/"+day+"/";
        String uuid = IdUtils.fastSimpleUUID();
        // 创建OSSClient实例。
        OSS ossClient = createOss();
        PutObjectResult result = null;
        try {
 
            String fileName =file.getName();
            System.out.println(fileName + "开始上传");
            String prefix = fileName.substring(fileName.lastIndexOf("."));
            String objectName = filePath + uuid + prefix;
            // 创建PutObject请求。
            result = ossClient.putObject(bucketName,objectName,file);
            // 如果上传成功,则返回200。
            System.out.println(fileName + "上传返回" + result.getResponse().getStatusCode());
            return result.getResponse().getUri();
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return result.getResponse().getErrorResponseAsString();
    }
 
    public static void getOSSFile(String key) throws Exception {
 
 
    }
 
}