From 4fc62cd286db464c92c5a31c2aaf2fc18f824767 Mon Sep 17 00:00:00 2001 From: Junling Bu Date: Thu, 9 Aug 2018 14:50:52 +0800 Subject: [PATCH] =?UTF-8?q?feat[litemall-core]:=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=83=E7=89=9B=E4=BA=91=E5=AF=B9=E8=B1=A1=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- litemall-core/pom.xml | 5 + .../litemall/core/storage/QiniuStorage.java | 122 ++++++++++++++++++ .../config/StorageAutoConfiguration.java | 14 ++ .../storage/config/StorageProperties.java | 48 +++++++ .../src/main/resources/application-core.yml | 12 +- .../litemall/core/QiniuStorageTest.java | 36 ++++++ pom.xml | 6 + 7 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 litemall-core/src/main/java/org/linlinjava/litemall/core/storage/QiniuStorage.java create mode 100644 litemall-core/src/test/java/org/linlinjava/litemall/core/QiniuStorageTest.java diff --git a/litemall-core/pom.xml b/litemall-core/pom.xml index cb4ab9b0..256f1eb8 100644 --- a/litemall-core/pom.xml +++ b/litemall-core/pom.xml @@ -54,6 +54,11 @@ + + com.qiniu + qiniu-java-sdk + + com.github.binarywang weixin-java-miniapp diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/QiniuStorage.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/QiniuStorage.java new file mode 100644 index 00000000..47c95983 --- /dev/null +++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/QiniuStorage.java @@ -0,0 +1,122 @@ +package org.linlinjava.litemall.core.storage; + +import com.qiniu.common.QiniuException; +import com.qiniu.http.Response; +import com.qiniu.storage.BucketManager; +import com.qiniu.storage.Configuration; +import com.qiniu.storage.UploadManager; +import com.qiniu.util.Auth; +import org.springframework.core.io.Resource; +import org.springframework.core.io.UrlResource; + +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Path; +import java.util.stream.Stream; + +public class QiniuStorage implements Storage { + + private String endpoint; + private String accessKey; + private String secretKey; + private String bucketName; + private String upToken; + private UploadManager uploadManager; + private BucketManager bucketManager; + + public String getEndpoint() { + return endpoint; + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + 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 String getBucketName() { + return bucketName; + } + + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } + + /** + * 阿里云OSS对象存储简单上传实现 + */ + @Override + public void store(InputStream inputStream, long contentLength, String contentType, String keyName) { + if(uploadManager == null){ + uploadManager = new UploadManager(new Configuration()); + Auth auth = Auth.create(accessKey, secretKey); + upToken = auth.uploadToken(bucketName); + } + + try { + Response response = uploadManager.put(inputStream, keyName, upToken, null, contentType); + } catch (QiniuException ex) { + ex.printStackTrace(); + } + } + + @Override + public Stream loadAll() { + return null; + } + + @Override + public Path load(String keyName) { + return null; + } + + @Override + public Resource loadAsResource(String keyName) { + try { + URL url = new URL(generateUrl(keyName)); + Resource resource = new UrlResource(url); + if (resource.exists() || resource.isReadable()) { + return resource; + } else { + return null; + } + } catch (MalformedURLException e) { + e.printStackTrace(); + return null; + } + } + + @Override + public void delete(String keyName) { + if(bucketManager == null){ + Auth auth = Auth.create(accessKey, secretKey); + bucketManager = new BucketManager(auth, new Configuration() ); + } + + try { + bucketManager.delete(bucketName, keyName); + }catch (Exception e){ + e.printStackTrace(); + } + } + + @Override + public String generateUrl(String keyName) { + return endpoint + "/" + keyName; + } +} diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/config/StorageAutoConfiguration.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/config/StorageAutoConfiguration.java index 514a9b1e..52cc0f09 100644 --- a/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/config/StorageAutoConfiguration.java +++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/config/StorageAutoConfiguration.java @@ -33,6 +33,9 @@ public class StorageAutoConfiguration { else if(active.equals("tencent")){ storageService.setStorage(tencentStorage()); } + else if(active.equals("qiniu")){ + storageService.setStorage(tencentStorage()); + } else{ throw new RuntimeException("当前存储模式 " + active + " 不支持"); } @@ -70,4 +73,15 @@ public class StorageAutoConfiguration { tencentStorage.setRegion(tencent.getRegion()); return tencentStorage; } + + @Bean + public QiniuStorage qiniuStorage() { + QiniuStorage qiniuStorage = new QiniuStorage(); + StorageProperties.Qiniu qiniu = this.properties.getQiniu(); + qiniuStorage.setAccessKey(qiniu.getAccessKey()); + qiniuStorage.setSecretKey(qiniu.getSecretKey()); + qiniuStorage.setBucketName(qiniu.getBucketName()); + qiniuStorage.setEndpoint(qiniu.getEndpoint()); + return qiniuStorage; + } } diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/config/StorageProperties.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/config/StorageProperties.java index 1844f4ac..b2f7d9b7 100644 --- a/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/config/StorageProperties.java +++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/storage/config/StorageProperties.java @@ -8,6 +8,7 @@ public class StorageProperties { private Local local; private Aliyun aliyun; private Tencent tencent; + private Qiniu qiniu; public String getActive() { return active; @@ -41,6 +42,14 @@ public class StorageProperties { this.tencent = tencent; } + public Qiniu getQiniu() { + return qiniu; + } + + public void setQiniu(Qiniu qiniu) { + this.qiniu = qiniu; + } + public static class Local { private String address; private String storagePath; @@ -139,4 +148,43 @@ public class StorageProperties { this.bucketName = bucketName; } } + + public static class Qiniu { + private String endpoint; + private String accessKey; + private String secretKey; + private String bucketName; + + public String getEndpoint() { + return endpoint; + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + 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 String getBucketName() { + return bucketName; + } + + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } + } } diff --git a/litemall-core/src/main/resources/application-core.yml b/litemall-core/src/main/resources/application-core.yml index c88468b6..5cb66de1 100644 --- a/litemall-core/src/main/resources/application-core.yml +++ b/litemall-core/src/main/resources/application-core.yml @@ -83,7 +83,7 @@ litemall: # 对象存储配置 storage: - # 当前工作的对象存储模式,分别是local、aliyun、tencent + # 当前工作的对象存储模式,分别是local、aliyun、tencent、qiniu active: local # 本地对象存储配置信息 local: @@ -95,11 +95,17 @@ litemall: endpoint: oss-cn-shenzhen.aliyuncs.com accessKeyId: 111111 accessKeySecret: xxxxxx - bucketName: xxxxxx + bucketName: litemall # 腾讯对象存储配置信息 # 请参考 https://cloud.tencent.com/document/product/436/6249 tencent: secretId: 111111 secretKey: xxxxxx region: xxxxxx - bucketName: xxxxxx \ No newline at end of file + bucketName: litemall + # 七牛云对象存储配置信息 + qiniu: + endpoint: http://pd5cb6ulu.bkt.clouddn.com + accessKey: 111111 + secretKey: xxxxxx + bucketName: litemall \ No newline at end of file diff --git a/litemall-core/src/test/java/org/linlinjava/litemall/core/QiniuStorageTest.java b/litemall-core/src/test/java/org/linlinjava/litemall/core/QiniuStorageTest.java new file mode 100644 index 00000000..8f81bafe --- /dev/null +++ b/litemall-core/src/test/java/org/linlinjava/litemall/core/QiniuStorageTest.java @@ -0,0 +1,36 @@ +package org.linlinjava.litemall.core; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.linlinjava.litemall.core.storage.QiniuStorage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.io.Resource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +@WebAppConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class QiniuStorageTest { + @Autowired + private QiniuStorage qiniuStorage; + + @Test + public void test() throws IOException { + String test = getClass().getClassLoader().getResource("litemall.png").getFile(); + File testFile = new File(test); + qiniuStorage.store(new FileInputStream(test), testFile.length(), "image/png", "litemall.png"); + Resource resource = qiniuStorage.loadAsResource("litemall.png"); + String url = qiniuStorage.generateUrl("litemall.png"); + System.out.println("test file " + test); + System.out.println("store file " + resource.getURI()); + System.out.println("generate url " + url); +// qiniuStorage.delete("litemall.png"); + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index cfcef2a4..c53d53a6 100644 --- a/pom.xml +++ b/pom.xml @@ -112,6 +112,12 @@ 2.5.0 + + com.qiniu + qiniu-java-sdk + [7.2.0, 7.2.99] + + org.springframework.boot spring-boot-starter-json