From 6be8702898b98e1f3ea0c939d7dd8be78708fc6d Mon Sep 17 00:00:00 2001 From: jlt Date: Fri, 1 Apr 2022 20:56:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90=E4=B8=83=E7=89=9B=E4=BA=91?= =?UTF-8?q?=EF=BC=8C=E4=BD=9C=E4=B8=BA=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E7=94=A8=E6=9D=A5=E4=BA=8B=E5=90=8E=E5=8A=A0=E9=80=9F=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 14 +++ private-farm/pom.xml | 13 +++ .../com/jlt/csa/task/QiniuConfiguration.java | 62 ++++++++++++ .../java/com/jlt/csa/task/QiniuOssTask.java | 97 +++++++++++++++++++ .../com/jlt/csa/task/SimpleFunctionTask.java | 9 ++ .../src/main/resources/application.yml | 10 ++ 6 files changed, 205 insertions(+) create mode 100644 private-farm/src/main/java/com/jlt/csa/task/QiniuConfiguration.java create mode 100644 private-farm/src/main/java/com/jlt/csa/task/QiniuOssTask.java create mode 100644 private-farm/src/main/java/com/jlt/csa/task/SimpleFunctionTask.java diff --git a/pom.xml b/pom.xml index 8e6360e3e..ab460d4b1 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,8 @@ 4.1.2 2.3 0.9.1 + 7.9.0 + 2.8.5 @@ -206,6 +208,18 @@ ${private-farm.version} + + com.qiniu + qiniu-java-sdk + ${qiniu.version} + + + + com.google.code.gson + gson + ${gson.version} + + diff --git a/private-farm/pom.xml b/private-farm/pom.xml index 6e3d8e6d2..f600abe7b 100644 --- a/private-farm/pom.xml +++ b/private-farm/pom.xml @@ -19,6 +19,19 @@ com.ruoyi ruoyi-common + + + + com.qiniu + qiniu-java-sdk + + + + + com.google.code.gson + gson + + \ No newline at end of file diff --git a/private-farm/src/main/java/com/jlt/csa/task/QiniuConfiguration.java b/private-farm/src/main/java/com/jlt/csa/task/QiniuConfiguration.java new file mode 100644 index 000000000..a43b87793 --- /dev/null +++ b/private-farm/src/main/java/com/jlt/csa/task/QiniuConfiguration.java @@ -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; + } +} diff --git a/private-farm/src/main/java/com/jlt/csa/task/QiniuOssTask.java b/private-farm/src/main/java/com/jlt/csa/task/QiniuOssTask.java new file mode 100644 index 000000000..865faa8d0 --- /dev/null +++ b/private-farm/src/main/java/com/jlt/csa/task/QiniuOssTask.java @@ -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); + } + + /** + * 使用指定的UploadManager、upToken、key和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; + } +} diff --git a/private-farm/src/main/java/com/jlt/csa/task/SimpleFunctionTask.java b/private-farm/src/main/java/com/jlt/csa/task/SimpleFunctionTask.java new file mode 100644 index 000000000..9e7b99df4 --- /dev/null +++ b/private-farm/src/main/java/com/jlt/csa/task/SimpleFunctionTask.java @@ -0,0 +1,9 @@ +package com.jlt.csa.task; + +public class SimpleFunctionTask { + public static int amount = 0; + public void ryNoParams() + { + System.out.println("[方法任务] ==> 执行无参方法,第 " + (++amount) + " 次."); + } +} diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index ed00cd045..cf1ce2d72 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -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 \ No newline at end of file