bug
jiangqs
2023-09-11 3adbcc42d5f84c3c83d220543189361add81e04d
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
202
203
204
205
206
207
208
209
210
211
212
package com.ruoyi.file.utils;
 
import cn.hutool.extra.qrcode.QrCodeUtil;
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.OSSObject;
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.FileInputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
 
/**
 * @author jqs34
 * @version 1.0
 * @classname OBSUploadUtils
 * @description: TODO
 * @date 2023 2023/5/1 15:40
 */
public class OBSUploadUtils {
 
 
 
    public static void main(String[] args) throws Exception {
        String fileUrl = null;
        File qrCodeFile = new File("/home/image/qrcode.png");
        // 二维码内容
        String text = "https://wxapp.hhhrt.cn/mini/coupon";
        // 生成二维码
        QrCodeUtil.generate(text, 100, 100, qrCodeFile);
        InputStream codeStream = new FileInputStream(qrCodeFile);
        fileUrl =  OBSUploadUtils.uploadInputStream(codeStream,"couponCode");
        System.out.println(fileUrl);
    }
 
    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 + "开始上传" + new Date());
            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 + "上传返回"+ new Date() + result.getResponse().getStatusCode());
            inputStream.close();
            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 uploadInputStream (InputStream inputStream,String fileName) 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 {
 
            System.out.println(fileName + "开始上传"+ new Date());
            String objectName = filePath + fileName + ".jpg";
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 设置该属性可以返回response。如果不设置,则返回的response为空。
            putObjectRequest.setProcess("true");
            // 创建PutObject请求。
            result = ossClient.putObject(putObjectRequest);
            // 如果上传成功,则返回200。
            System.out.println(fileName + "上传返回"+ new Date() + 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 InputStream getOSSInputStream(String key) throws Exception {
        String bucketName = "hongruitang";
        // 创建OSSClient实例。
        OSS ossClient = createOss();
        OSSObject ossObject = ossClient.getObject(bucketName,key);
        InputStream inputStream = ossObject.getObjectContent();
        return inputStream;
    }
 
}