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;
|
}
|
|
}
|