集成七牛云,作为定时任务用来事后加速图片。

This commit is contained in:
jlt 2022-04-01 20:56:58 +08:00
parent 9f1d942078
commit 6be8702898
6 changed files with 205 additions and 0 deletions

14
pom.xml
View File

@ -33,6 +33,8 @@
<poi.version>4.1.2</poi.version>
<velocity.version>2.3</velocity.version>
<jwt.version>0.9.1</jwt.version>
<qiniu.version>7.9.0</qiniu.version>
<gson.version>2.8.5</gson.version>
</properties>
<!-- 依赖声明 -->
@ -206,6 +208,18 @@
<version>${private-farm.version}</version>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>${qiniu.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -19,6 +19,19 @@
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common</artifactId>
</dependency>
<!-- 七牛云存储 -->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
</dependency>
<!-- gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,62 @@
package com.jlt.csa.task;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "qiniu")
public class QiniuConfiguration {
private Boolean enabled;
private Boolean isUse;
private String accessKey;
private String secretKey;
private String bucket;
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public Boolean getIsUse() {
return isUse;
}
public void setIsUse(Boolean use) {
isUse = use;
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public Boolean getUse() {
return isUse;
}
public void setUse(Boolean use) {
isUse = use;
}
public String getBucket() {
return bucket;
}
public void setBucket(String bucket) {
this.bucket = bucket;
}
}

View File

@ -0,0 +1,97 @@
package com.jlt.csa.task;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("qiniuTask")
public class QiniuOssTask {
private QiniuConfiguration config;
private UploadManager uploadManager;
private String upToken;
public int amount = 0;
@Autowired
public void setConfig(QiniuConfiguration config) {
this.config = config;
}
/**
* 无参任务测试
*/
public void ryNoParams()
{
System.out.println("[实例任务] ==> 执行无参方法,第 " + (++amount) + " 次.");
}
/**
* 供定时任务调用的Bean方法
*/
public void testUpload() {
getUploadManager();
makeToken();
DefaultPutRet putRet = uploadFile(null, "d:/time.jpg");
System.out.println(putRet.hash);
}
/**
* 使用类Bean的上传管理器upToken上传指定key和路径的文件
* @param key key作为上传文件的云存储文件名如果为null则使用hash作为文件名
* @param filePath 上传的文件路径
* @return 七牛云上传结果类
*/
public DefaultPutRet uploadFile(String key, String filePath) {
return uploadFile(this.uploadManager, this.upToken, key, filePath);
}
/**
* 使用指定的UploadManagerupTokenkey和filePath上传文件
* @param uploadManager 上传管理器
* @param token 上传token
* @param key key作为上传文件的云存储文件名如果为null则使用hash作为文件名
* @param filePath 上传的文件路径
* @return 七牛云上传结果类
*/
public DefaultPutRet uploadFile(UploadManager uploadManager, String token, String key, String filePath) {
try {
Response response = uploadManager.put(filePath, key, token);
String bodyStr = response.bodyString();
DefaultPutRet putRet = Json.decode(bodyStr, DefaultPutRet.class);
return putRet;
} catch (QiniuException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 获取上传管理器并保存在类成员中
*/
public UploadManager getUploadManager() {
Configuration cfg = new Configuration(Region.huabei());
cfg.useHttpsDomains = false;
uploadManager = new UploadManager(cfg);
return uploadManager;
}
/**
* 获取七牛云上传Token并保存在类成员中
* @return
*/
public String makeToken() {
Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
upToken = auth.uploadToken(config.getBucket());
return upToken;
}
}

View File

@ -0,0 +1,9 @@
package com.jlt.csa.task;
public class SimpleFunctionTask {
public static int amount = 0;
public void ryNoParams()
{
System.out.println("[方法任务] ==> 执行无参方法,第 " + (++amount) + " 次.");
}
}

View File

@ -124,3 +124,13 @@ xss:
excludes: /system/notice
# 匹配链接
urlPatterns: /system/*,/monitor/*,/tool/*
# 七牛云对象存储
qiniu:
# 是否启用云存储,针对上传
enabled: true
# 是否读取云存储
isUse: false
accessKey: 8Qmhci_2fkwFOp2dV74TiVTG2k3IubxOd20X-KJk
secretKey: 3DsZ_Oq02QOp2iJbpEKvIFd4Ul3ZBs0tMPXvGUcI
bucket: jlt-test