feat[litemall-core]: 支持七牛云对象存储
This commit is contained in:
@@ -54,6 +54,11 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.qiniu</groupId>
|
||||
<artifactId>qiniu-java-sdk</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>weixin-java-miniapp</artifactId>
|
||||
|
||||
@@ -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<Path> 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
bucketName: litemall
|
||||
# 七牛云对象存储配置信息
|
||||
qiniu:
|
||||
endpoint: http://pd5cb6ulu.bkt.clouddn.com
|
||||
accessKey: 111111
|
||||
secretKey: xxxxxx
|
||||
bucketName: litemall
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
6
pom.xml
6
pom.xml
@@ -112,6 +112,12 @@
|
||||
<version>2.5.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.qiniu</groupId>
|
||||
<artifactId>qiniu-java-sdk</artifactId>
|
||||
<version>[7.2.0, 7.2.99]</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-json</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user